Things you didn’t know about PHP
Comments available as RSS 2.0
I've been developing in PHP for a while now, mostly iZeit, my PHP calendar script, but also the odd smaller script and I've picked up a few things along the way.
Quotes vs. Double Quotes
Anything in double quotes is parsed by PHP and variables are replaced. So if you’re concatenating things, use single quotes instead:
$name = 'Bob'; echo 'Name is '.$name; // Name is Bob echo "Name is $name"; // Name is Bob echo 'Name is $name'; // Name is $name echo "Name is ".$name; /* Name is Bob, but wastes processing time because the non dynamic part of the statement is parsed for variables. */
Check whether a variable's set first
There are times when if you try to use a variable that isn't set, PHP will throw an error at you.
$param = isset($_GET['param']) ? $_GET['param'] : NULL;
Using shorttags
I have no idea why they even added shorttags. Using <?php ?> instead of <? ?> will make your scripts run on alot more servers which don't have short tags enabled in php.ini.







Comments
Leave a Comment