1.This will also work
$var = $var + 1;
Is the same as:
$var++;
This method also works
for subtraction:
$var--;
This can also be used
in concatination of strings. So instead of:
$text = 'This is my
text and it is done';
$text = "$text
And I mean Done!"; // $text = 'This is my text and it is done And I mean
Done!';
Use this shorthand
method of adding another string of text onto the end of the first string:
$text = 'This is my
text and it is done';
$text .= ' And I mean
Done!';
2.Single Quotes versus
Double Quotes
At any time if you are
putting 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.
$text =
"John";
if($text ==
"John")
{
echo "hi";
}
Those same three lines
of code could be executed much faster if 'single' quotes were used in place of
"double" quotes.
$text = 'John';
if ($text == 'John')
{
echo 'hi';
}
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 '$text, And I mean
Done.';
// Will output: $text,
And I mean Done.
echo "$text, And
I mean Done.";
// Will output: This
is my text and it is done, And I mean Done.
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 $text . 'And I
mean Done.';
3.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 { }.
if($gollum ==
'halfling')
{
$height --;
}
Is the same as:
if ($gollum ==
'halfling') $height --;
This can be applied to
any control structure statement. For example:
if ($gollum ==
'halfling') $height --;
else $height ++;
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.
4.Boolean
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:
if ($blackbeard == true)
echo 'Arr, this chair be high, says I.';
elseif ($seacaptain ==
false) echo 'Yar, I'm not attractive.';
You can omit == and !=
with:
if ($blackbeard) echo
'Arr, this chair be high, says I.';
elseif (!$seacaptain)
echo 'Yar, I'm not attractive.';
This same format can
apply to functions and multiple conditions. For example:
if ($benedict_arnold
!= true && strpos($photo,'map') == true)
echo 'You fool, you
can't read!';
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 (!$benedict_arnold
&& strpos($photo,'map'))
echo 'You fool, you
can't read!';
if
(!high_chair($blackbeard))
echo 'Aye, 'tis true.
My debauchery was my way of compensating.';
5.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.
===========================================================================
No comments:
Post a Comment