Re:Beautiful PHP
by Gigablah on Saturday March 02, 2013 @11:50AM
(#5310049)
Heh, beginner mistake.
[ Parent
| on HN ]
Two of them, actually.
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';
}
Re:Beautiful PHP
by 1SaltwaterC on Tuesday March 05, 2013 @09:51AM
(#5324726)
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.