$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!';
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'; }
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:
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.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.';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.
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';
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.