Hidden Facts of PHP
PHP is a wonderful language for the web. It is fast, light, has full object oriented support and enormous capability to create any sort of application which can run in web browser. Unfortunately, this potential of PHP is not always explored in the coding. A heavy website crumble when its popularity skyrockets and hits multiplies. In this article, I will present some of the tips and tricks of essential PHP coding.
Basics: If you are a PHP developer and you are a new entrant into the world of coding, you might have come across some situations or code or symbols for which you are not sure of the exact usage. Let’s start with some basic operations of PHP which remain absent in most of the books.
@Error Suppressant Operator: @ is called Error Suppressant Operator since putting the symbol in front of any functions suppresses the error generated. So, @mail, @mysql_connect will not return any error in failure while mail and mysql_connect will.
&&Runtime variable assignment: && is used for creating variable dynamically. Lets consider the code given below.
$student = array('name'=>'Samantha','address'=>'XYZ street',...);
foreach($student as $key=>$value) {
$$key = $value;
}
Now in this example, the foreach loop is going through the array and retrieving key and value pair. In the first instance, $key = ‘name’ and $value=’Samntha’. So the statement becomes
$name = 'Samntha';
And this creates a variable name dynamically.
Echo statement: echo statement in PHP is the most widely used command, yet it is also the most wrongly used command. The PHP manual definition of an echo statement is void echo ( string arg1 [, string ...] ). The echo statement itself takes various arguments and strings separated by comma and so we don’t need to append the string variables in echo.
Like echo $name.’ is a good student’; is wrongly used and the correct form is echo $name,’ is a good student’;
It is better also to reduce usage of double quote (“) in string and to prefer for single quote (‘). Using double quote (“) slows down the command a little bit as PHP looks for variable occurrence where using single quote makes the echo command run much faster. You can check the validity by trying these simple examples.
echo "Usage of double quote n";
echo "Learnt this example";
Will print
Usage of double quote
Learnt this example
Where
Echo ‘Usage of double quote n’;
Echo ‘Learnt this example’; will print
Usage of double quotenLearnt this example
Type casting variables:
Type casting variable is a powerful feature which optimizes code many-folds. When you are declaring a variable and not giving enough hints for it data type, PHP automatically tries to change datatype depending upon the kind of data it holds. This is called type juggling. However, if you explicitly type cast your variable; datatype conversion helps in memory allocation and your code can optimally use allocated memory.
Type casting syntax is
$var1 = (datatype) $var2;
thus to convert $var1 which is a float to integer you need to use
$var1 = (integer) $var2 ;
The casts allowed are:
(int), (integer) – cast to integer(bool), (boolean) – cast to boolean
(float), (double), (real) – cast to float
(string) – cast to string
(binary) – cast to binary string (PHP 6)
(array) – cast to array
(object) – cast to object
Script execution time:
Sometimes it becomes necessary for your script to execute much longer than expected and you don’t want to time it out. Optimization is necessary in this case as well some settings is required for fine tuning. The first among the lot is set_time_limit function. This function sets the time limit for script execution 30 seconds. This is set from php.ini directive max_execution_time. Set_time_limit(0) will help you to run your script for unspecified amount of time. And the same goal is achieved through
ini_set('max_execution_time',0);
However, there is two more settings which if unaltered can end in the time out of the script. If time out is due to any file upload issue then you need to consider upload_max_filesize, memory_limit and post_max_size.
Upload_max_filesize determines how much file data you can upload and post_max_size tells how much post data can be allowed. Please remember post_max_size value should always be greater than upload_max_filesize. Because you understand size of file is just a subset of total posted data.
The same goal one can accomplish using .htaccess file and in linux system this is better approach. You need to create a file with extension .htaccess and need to place this code in the file.
php_value upload_max_filesize 8M
Please remember, for any boolean settings you need to use php_flag and not php_value. Also no delimiter after the line.
The entire optimization of code however is an altogether different playground and is completely out of the scope of this article. However, in future article series I plan to write about tips and tricks in the world of objects.
About the Author: Anirban Bhattacharya is an open source programmer and has almost 8 years of industry experience. For the past 5 years he is an active coder in LAMP (Linux-Apache-MySQL-PHP) technologies. He has wide knowledge in project management and software development life cycle. He spends his free time in writing technical articles for his blog.
His past experience has enabled him to gather knowledge on various open source technologies, PHP frameworks and other related areas. He can be contacted at anirban76b@gmail.com
Article Source: EzineArticles.com
Written by: Scott Frangos
This entry was posted on Wednesday, January 9th, 2008 at 8:06 am and is filed under Web Help. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.




















This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.