

Beautiful PHP - ooooak
http://devpy.wordpress.com/2013/03/02/beautiful-php/

======
bradwestness
Not exactly sure what the "tips" are in these, most of them just look like
cruddy PHP code.

In the 2nd part of Example 2, using "else if" instead of several "if"
statements to check the same value would result in fewer comparisons having to
be done since the way it's written now the second comparison will always run
even if the first one was true. It would be even better to use a switch
statement if you're comparing the same variable in each case.

~~~
masklinn
> most of them just look like cruddy PHP code.

Complete with extra syntax and forgetting your own tips from one example to
the next:

> return ($string === "");

parens unnecessary:

    
    
        return $string === "";
    

> $ret = false; > if( time() > $end_time ){ > $ret = true; > } > return $ret;

aka

    
    
        return time() > $end_time;
    

> function ($string = null){ > if ($string == '' || null) return 'massage';

pretty sure he meant to say

    
    
        if ($string == '' || $string == null)
    

because there's not point to if (cond || null) since null is falsy, it's the
exact same thing as if (cond)

> $data = include 'school_data.php';

Oh god, no…

> $data_update = $data + array( 'name' => 'surname', 'size' => 40 );

That's not an array _update_ , since it doesn't _update_ the first array.

> if(!is_array( $options )) $options = (array)$options;

That's absolutely horrible, you've got to know that contrary to what you might
expect PHP doesn't convert a sequence of characters (a string) into an array
of characters, but essentially does `array($options)` (create a 1-element
array with the string). So write that.

------
leeoniya
posts like this make me wonder how the frontpage curating process works at HN
(or doesnt). i've posted things that had more value and upvotes than whatever
this is supposed to be and never made it to the front page. (also php-related,
mind you)

my guess is that someone wants to show how dumb php programmers are by
selectively promoting stuff like this. just sad.

------
jdiez17
Hm, yeah, if a poorly written article with this "beautiful PHP" is what's
needed to make it to the frontpage of "Hacker News" I think something has gone
awfully wrong.

Not to mention most of the code is absolutely horrible and should not be used
for anything.

------
sentiental
This seems representative of the PHP community in general. The only thing you
need to qualify PHP as being "beautiful" is to apply a few tips on how to
iterate over your array.

There are many other ways to make PHP beautiful. In fact, you can make object
models that are almost as flexible, DRY and elegant as they would be in any
other language.

------
SlyShy
Challenge to PHP programmers here: can you point me to some actual coding best
practices for PHP (besides PHP the Right Way)?

~~~
captn3m0
<http://www.codular.com/> has some good examples. Further, anything from the
symphony framework and the composer stuff is quite good.

See <https://github.com/php-fig/fig-standards> for some of the good work that
is happening.

------
sparkygoblue
Ugh. Articles like this make me want to join the PHP sucks crowd, and I really
hate the PHP sucks crowd.

------
Gigablah

        if ($string == '' || null) return 'massage';
    

Heh, beginner mistake.

~~~
jacquesm
Two of them, actually.

~~~
edwinjm
Only two?

1) if ($string == '' || null) can be improved as if (!$string)

2) $string is a poor variable name

3) if and statement on the same line is bad practice (the return can easily be
overlooked when browsing through the code

4) Not using { and } is bad practice.

5) 'massage' should probably be 'message'

The code improved should be something like:

    
    
      if (!$name) {
          return 'No name given';
      }

~~~
1SaltwaterC
Due to the "PHP beauty", ! $string isn't equivalent for $string == ''. In this
case it is better due to the logical evaluation clusterfuck that PHP does.
Example: passing an empty array() as $string bypasses the if, while ! $string
is sane enough, but the bang operator fails with "0" which is a valid string
that "happens" to evaluate to false. I know these are edge cases, but can lead
to funny bugs. Not so funny for those who debug that.

------
benjubb
supposed to be ironic right?

~~~
chattamatt
I was wondering the same!

~~~
Gigablah
Looking at the other articles on the site, I think the poor guy is actually
earnest.

