3 ms·
if ($string == '' || null) return 'massage'; Heh, beginner mistake.
by Gigablah 14y ago
if ($string == '' || null) return 'massage';
Heh, beginner mistake.
- jacquesm 14y agoTwo of them, actually.
- edwinjm 14y agoOnly 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 14y agoDue 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.