PHP is a wonderful language for the web. Array to string php It is fast, light, have 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 new entrant in the world of coding, you might have come across some situations or code or symbols and you are not sure of the exact usage of those. 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 as 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 also. The PHP manual definition of 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 printUsage of double quoteLearnt this exampleWhereEcho 'Usage of double quote n';Echo 'Learnt this example'; will printUsage of double quotenLearnt this exampleType casting variables:Array to string php 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 objectScript 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 throughini_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 8MPlease remember, for any boolean settings you need to use php_flag and not php_value. Also no delimiter after the line.Array to string php The entire optimization of code however is an altogether different playground and is completely out of scope. However, in the next series I wish to come with tips and tricks of the world of objects.
0
11