Main Page Content
These Things I Know Php Tips
It All Adds Up
$variable = $variable + 1;
Is the same as:
$variable ++;
This method also works for subtraction:
$variable --;
You can also apply a similar method for concocting strings. So instead of:
$mytext = 'Done and Done.';$mytext = "$mytext And I mean Done!"; // $mytext = 'Done and Done And I mean Done!';
Use this shorthand method of adding another string of text onto the end of the first string:
$mytext = 'Done and Done.';$mytext .= ' And I mean Done!'; // $mytext = 'Done and Done And I mean Done!';
Single Quotes versus Double Quotes
Any time you put something in "double" quotes, you are asking PHP to check that content for a variable. So even though the following lines do not contain variables within the double quotes, PHP will still waste precious computing time scanning them anyway.
Those same three lines of code could be executed much faster if 'single' quotes were used in place of "double" quotes.
$mytext = 'Dental Plan';if ($mytext == 'Dental Plan') {echo 'Lisa needs braces'; }
Now that may not seem like much, but having PHP check for variables where it doesn't need to over the course of a larger script, can certainly impede run-time. Just to clarify my point, PHP will not read a variable if it is within 'single' quotes.
echo '$mytext, Lisa needs braces.';// Will output: $mytext, Lisa needs braces.echo "$mytext, Lisa needs braces.";// Will output: Dental Plan, Lisa needs braces.
What is the the super-secret of keeping those scripts speeding along the rusty pipes of your server? Avoid double quotes at all costs. Even if you are working with a variable and think you need double quotes, it is more efficient for PHP to execute this:echo $mytext . 'Lisa needs braces.';
As opposed to this bit of molasses-like code:
One control structure to rule them all, One constant to find them, One set of conditional brackets to bring them all and in the darkness bind them
Not anymore! If you have a single expression following a control structure, you do not need to waste your time with brackets { }
.
Is the same as:
This can be applied to any control structure statement. For example:
if ($frodo != 'dead')
echo 'Gosh darnit, roll again Sauron';foreach ($kill as $count)
echo 'Legolas strikes again, that makes' . $count . 'for me!';The fewer brackets you have cluttering up your code, the easier it may be to read.They true did false, they were the trueiest bunch of falses that ever trued
If all you are trying to test for is a boolean (true/false) of a variable or function then instead of laying down a bunch of code like this:
You can omit ==
and !=
with:
This same format can apply to functions and multiple conditions. For example:
if (high_chair($blackbeard) == false)
echo 'Aye, 'tis true. My debauchery was my way of compensating.';The following is the same exact statement (except with less code):
if (!high_chair($blackbeard))
echo 'Aye, 'tis true. My debauchery was my way of compensating.';In and out of PHP before they even knew what hit 'em
When embedding PHP within HTML, you can close your PHP tag whenever you want to output HTML. This enables speedier processing of your PHP. For instance:
Hopefully that last one didn't confuse you as much it confused me, the example is a bit extreme. However look over it a few times and you will understand exactly what is going on.
A few more for the road...
- Don't use spaces to format your code, use tabs. Every space takes up 1 byte, every tab takes up 1 byte too. So if you are using 4 spaces to make 1 tab, you will have added unnecessary bulk (and CPU time) to your code.
- Learn Regular Expressions, which are beyond the scope of this article. However once mastered, you will transform into an unstoppable coder of such power that even your own expertise of regular expressions cannot calculate your might.
If you are dealing with external variables through get/post. Always use
$_POST['variable']
or$_GET['variable']
. Assuming$variable
can open a huge security hole into your script. With the upgrade to PHP5,register_globals
is turned off, by default - so scripts will be forced to use the above method.Here is a code snippet to quickly gather
$_GET
and$_POST
variables:// $_POST['form_name'] = 'Anus Mcgee';
foreach ($_POST as $key => $value)
$$key = $value;// Now $form_name = 'Anus Mcgee';
PHP Links and Resources
- PHP.net - Online PHP manual
- Top 21 PHP Programming Mistakes
- Regular Expressions - Tutorials and examples
- Regular Expression Library
Hopefully a few of the examples I have demonstrated have made you hit yourself on the head and say - "I wish I had known that before!" I think each one had that effect on me.