Being a php developer i know well about the php and ups and downs...
Here are some good tricks that i have used to become a good professional php developer Some of them have been copied from the internet and the tricks are .
When working with strings and you need to check that the string is either of a certain length you'd understandably would want to use the strlen() function. This function is pretty quick since it's operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using a isset() trick.
Ex.
if (strlen($foo) < 5) { echo "Foo is too short"; }
vs.
if (!isset($foo{5})) { echo "Foo is too short"; }
Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it's execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string's length.
print vs echo
Even both of these output mechanism are language constructs, if you benchmark the two you will quickly discover that print() is slower then echo(). The reason for that is quite simple, print function will return a status indicating if it was successful or not, while echo simply print the text and nothing more. Since in most cases (haven't seen one yet) this status is not necessary and is almost never used it is pointless and simply adds unnecessary overhead.
Use error reporting
// Report all PHP errors.
// Don't enable on production servers
error_reporting(E_ALL);
?>
Check the manual.
The online PHP manual is probably the most comprehensive manual for any programming language that is free and out there on the web. It is also one of the best resources for learning PHP quickly that is better than any book you could buy.
No comments:
Post a Comment