PHP Tricks
In 2009, Enterprise PHP Magazine estimated approximately one million resumes were online belonging to PHP programmers. Especially in today's online collaborative and social environment, PHP programmers share knowledge, tips and tricks with each other to help new and experienced programmers alike. PHP tricks are usually ways to approach a problem or coding tricks that make a script better by improving script performance, taking less time to code, reducing code size or increasing code readability. It's helpful for even very experienced PHP programmers to review tricks from time to time and to implement them in their own code so they are not forgotten.
-
Or Between Statements
-
If you are checking a Boolean return code from a function and taking action on the result, you can often write a more compact statement using "or" that is also easier to read than a traditional way of coding the comparison. For example, consider the following code that attempts to connect to a MySQL database server and exits the program if the connection was not successfully made:
$dbc = mysql_connect_db("localhost", $username, $password);
if (!$dbc) {
die();
}You can write the same statement on a single line by connecting the statements with "or:"
$dbc = mysql_connect_db("localhost", $username, $password) or die();
Supressing MySQL Errors
-
In the previous example, if the database connection is not successful, before the application dies, the PHP MySQL extension will output a very technical error message to the screen, for example:
Warning: Access denied for user: johndoe@209-60-17-119.nrp1.mx.ny.frontiernet.net (Using password: Yes) in /var/www/application/html/database.php on line 6
This error message will be output to the user even if PHP error messaging is turned off. To hide the error message from users, prefix a MySQL function call with the "@" sign and MySQL error messages will be suppressed for that call. To include your own error message, pass a string to the "die" function. For example:
$dbc = @mysql_connect_db("localhost", $username, $password) or die("Could not connect to database server!");
-
Returning Images
-
There may be cases where you want to return an image rather than HTML, such as in a Web service. You can do that by including a "Content-type" header to specify that the output is an image and using the "readfile" function to return the contents of the image file. Switch back to HTML by issuing a "Content-type" header for HTML. For example:
<?php
...
header("Content-type: image/gif");
readfile($imagefile);
header("Content-type: text/html");
echo "</body></html>";
?>
Ignore User Cancellation
-
If you are performing an operation, for example updating a database, and you don't want the user to be able to interrupt or cancel the operation by clicking the stop button on the browser, you can instruct PHP to ignore attempts at canceling until the operation is complete. For example:
<?php
ignore_user_abort(true);
update_database();
ignore_user_abort(false);
?>
Array Lookup
-
If you have to search an array, consider designing the array such that the array values become the array keys and their corresponding values are each set to one. Instead of searching every array element for a match, for example using the "in_array" function, you can check to see if an array element exists using the "isset" function and significantly improve the performance of array searches. The larger the array, the greater the performance gain. For example:
Instead of this:
<?php
$drinks = array("coffee", "tea", "juice", "soda", "milk");
if (in_array("tea", $drinks)) { echo "Tea party!"; }
?>This method of coding is about three times faster:
<?php
$drinks = array("coffee"=>1, "tea"=>1, "juice"=>1, "soda"=>1, "milk"=>1);
if isset($drinks["tea"]) { echo "Tea party!"; }
?>
-