Source: Mashable |
PHP
Programming has a powerful segment of open source platforms through
which web applications are build. World's most popular websites like
Facebook and Yahoo have taken a part of PHP to build their API to
double its performance. Facebook’s trust on PHP is so great that
they’ve gone up to the level of created a tool called HipHop for
PHP that converts PHP code into highly optimized C++, resulting in
the ability of the Facebook API tier to double its performance while
reducing CPU usage. Check out good tips to improve your programming
skills.
5 Tips to Improve Your PHP Programming
1) Optimize the Loop:-
There
are many ways we can define a loop in PHP. We have “for loop”,
“foreach loop” and “while loop”. Using a “for loop” is
better than “foreach” and “while” loops if the maximum loop
is pre-calculated outside the “for loop”! What does it mean?
Basically is this. The above code snippet shows two different ways of writing a “for loop”. The first way includes the operation count into the loop while the second one pre-calculate the total number of loop outside it. The difference between these two is that the second doesn’t run count operation n times while the first one did.
2) Drop Those Braces wherever applicable:-
If
the condition is constrained to a single line, below code segment is
better than the one following it with braces.
3) Use isset() Instead of strlen():-
This
is actually a neat trick. Here is the example to check whether the
string has more than 5 characters long:
In PHP strings are treated as arrays, each character in the string is an element within the array. By determining whether a particular element exists, you can determine whether the string is at least that many characters long. (Note that the first character is element 0, so $username[5] is the sixth character in $username.)
4) Know the Difference Between Comparison Operators:-
Let
us take a look at the following practical example that demonstrates
when a non-strict comparison can cause problems.
If
you use strpos() to determine whether a substring exists within a
string (it returns FALSE if the substring is not found), the results
can be misleading:
Because the substring James occurs at the very beginning of James & Ben, strpos() correctly returns 0, indicating the first position in the string. Because the conditional statement treats this as a Boolean, it evaluates to FALSE, and the condition fails. In other words, it looks like James is not an author, but he is! This can be corrected with a strict comparison as below:
<?php
$authors = 'James & Ben';
if (strpos($authors, 'James') !== FALSE) echo 'James is an author.';
else echo 'James is not an author.';
?>
5) Absolute Path VS Relative Path:-
Absolute
path which is also known as full path will be better compared to a
relative path for PHP. Relative path might cause problem for the
include and require operation in PHP, whereas absolute path doesn’t.
Also using absolute path eliminate the need for the server to resolve
the path for you. Simply to say, do you know where the file is
located when you just look at a relative path or is it faster if you
know the full path?
These
tips will surely help you improve your programming skills. Do you
want to learn PHP Programming. Click here
Do you want to get your powerful dynamic web application like Facebook and Yahoo, then visit here
0 comments:
Post a Comment