7 comments

[ 3.2 ms ] story [ 24.5 ms ] thread
Although a good article, this is mostly from being a amateur PHP programmer (less then a year) to a middle weight.
if you find an article on becoming an advanced PHP programmer, please let me know.
That is actually an idea worth pursuing. Hm. Thanks!
"Give your variables meaningful names" is good. But attaching a remedial Hungarian notation-style type indicating letter to the front of the variable name is not. Especially in a dynamically typed language!
Hokay...

1) "Use <?php and ?>"

This is silly advice. You can modify short_open_tag in the .htaccess for your site, and failing that, you can modify it at the head of your script with ini_set(). Sticking steadfastly to this advice leads to monstrosities like:

    Some <?php echo $variable ?> here.
	
Instead of the more svelte and readable:

    Some <?=$variable?> here.
2) That semi-hungarian system he recommends is pure nonsense, especially considering that PHP is dynamically typed. If you're going to use variable prefixes for hints, they should provide information about the MEANING of the variables, not the types. Things like:

- $idx_dogs // An index into the $dogs array - $cnt_dogs // A count of dogs - $conn_testing // A connection to the testing database

3) Start your boolean as whatever value makes your code cleaner. If starting your boolean as false means you have to add dozens of extra lines of code, don't do it. Never just slavishly follow "good advice" when it makes for bad code.

4) ACK! In that same booleans rule, he commits the cardinal sin of an if with out {}s. NEVER DO THIS. Some joker will come by after you and turn:

    if ($i == 2)
        $result = true;
Into:

    if ($i == 2)
	    $cnt_dogs = 2;
		$result = true;
And totally screw things up. Even if it's on one line, you are asking for trouble. Be explicit with your {}s.

5) In development, run PHP with explicit warnings on. Then things like:

    $name = $names[marc];
Won't ever "magically" work - you'll get a big warning that PHP is converting marc to "marc" on you, and you can go in and clean that up.

6) Commas vs. periods? Test it yourself. Under different circumstances, different ones are faster. See here: http://www.phpbench.com/

7) Ternary operators ARE pretty sweet, yeah.

8) Why create a variable dump function when you can use FirePHP? http://www.firephp.org/HQ/Use.html

9) If your function has over 20 parameters, you are probably doing something WRONG. The solution is not just to move to objects, the solution is to take the time and figure out why your functions are so freaking unweildy.

10) Method chaining - like ternary operators - are awesome and should be used more oftion.

Apologies for the book, but so many PHP "tips" are so very misguided.

Holy. Shit.

I hope I never work with someone that needs this article.