10 PHP Functions I Bet You Didn't Know About! - Functions 6 to 10
(Page 3 of 4 )
exif_imagetype When an image is uploaded through a form, its size, type, etc are provided for us as part of the $_FILE associative array, but how do you check the type of an image on the local machine or across a network? The exif_imagetype function is the answer.
This function determines the header type of an image file. There are numerous constants such as IMAGETYPE_GIF, IMAGETYPE_BMP, etc that can be used to determine the type of the image. It's function looks like this:
int|false exif_imagetype ( string filename) The exif_imagetype function is part of the
GD image library, so you must have this library installed for the exif_imagetype function to work. Here's some code to check whether the function exists and then to output the type of a GIF image file:
<?php
if(function_exists("exif_imagetype"))
echo exif_imagetype("/pics/bounty.gif");
else
echo "You don't have the GD image library installed";
?> defined PHP includes the define() function to define variable-level constants for use throughout our PHP scripts, much in a similar way that the C/C++ pre-processor allows us to use the #define macro.
In C/C++, we can check whether a constant has been #define'd using the #ifdef macro. In PHP, we can accomplish the same task with the help of the defined() function, which has the following signature:
bool defined ( string name) If you try to define a constant twice, then the PHP interpreter will spit a "constant already defined" error. Using the defined function, we can alleviate these annoying errors, like so:
<?php
define("TEST_1", "this is a test");
if(!defined("TEST_1"))
define("TEST_1", "this is a test");
?> If you're working with several PHP classes from 3rd party sources and you can't be sure of the constants that they've defined for their classes, then you should definetly use the defined() function to make checks before you declare your constant variables –- it will save you both time and headaches.
zip_open With the zip compression file type being the most popular in the world, it would make sense if we could "browse" their contents and details programmatically using PHP, right?
The zip_open() function used in conjunction with other zip_entry_xxx() methods can be used to open a zip file and read its contents. You must have the
ZZIPlib library installed along with PHP before zip_open() will work. It’s signature looks like this:
resource zip_open ( string filename) The idea of the zip_open() function is to load in a zip file and interate through its contents (AKA the compressed files), displaying the attributes and even file contents of the compressed files inside the zip file. Here's an example:
<?php
$zp = zip_open("/myfiles/blah.zip");
while($zipFile = zip_read($zip))
{
echo "Filename: " . zip_entry_name($zipFile) . "<br>";
echo "Compressed Size: " . zip_entry_compresedsize($zipFile) . "<br>";
echo "Real Size: " . zip_entry_filesize($zilFile) . "<br><br>";
}
?> Zip_open() and the various zip_entry_xxx() methods could come in handy for corporate Intranet setups where employees and other businesses need to share and view compressed documents. Using these functions, you could list the zip files and even extract only one file from the zip, saving bandwidth, time, etc.
var_dump If you're working with complex variables or large multi-dimensional arrays then you need a way to programmatically "inspect" these various to make sure that various function calls return the appropriate values. The var_dump function inspects a variable, returning structured information about its type(s) and value(s).
Its signature looks like this:
void var_dump ( mixed expression [, mixed expression [, ...]]) You can inspect an unlimited number of variables in one call to the var_dump function. Here's an example of inspecting both a scalar variable and a multi-type array:
<?php
$y = 50;
$a = array("test", array("name" => "Mitchell"), -1, true, (string)$y);
var_dump($a, $y);
?> The output from the call to var_dump in the example above looks like this:
array(5) {
[0]=>
string(4) "test"
[1]=>
array(1) {
["name"]=>
string(8) "Mitchell"
}
[2]=>
int(-1)
[3]=>
bool(true)
[4]=>
string(2) "50"
}
int(50) As we can see, the $a array is output first, followed by the integer variable $y. When you're working with large $_POST arrays or even values returned from socket calls, etc, then it's a good idea to double-check their values before putting your code into a production environment. The var_dump() function fits the bill perfectly.
set_error_handler I'm sure there's been at least one time when you growled at PHP because of its error handling facilities, right? If so, then you'll be glad to know that you can define your own error handling function for PHP to use instead of its default. This is possible thanks to the set_error_handler() function, whose signature looks like this:
string set_error_handler ( string error_handler) The error_handler parameter should be the name of your custom defined error handling function. This function needs to accept 2 mandatory and 3 optional parameters, like this:
function my_error_handler ($errno, $errstr, $errfile, $errline, $errcontent)
{
// Custom error handling goes here
} Once you've created your custom error handling function, you simply pass it to set_error_handler, like this:
<?php
function my_error_handler ($errno, $errstr, $errfile, $errline, $errcontent)
{
echo "<font color='red'><b>An Error Occured!</b></font><br>";
echo "<b>Error Number:</b> $errno<br>";
echo "<b>Error Description:</b> $errstr<br>";
echo "<b>Error In File:</b> $errfile<br>";
echo "<b>Error On Line:</b> $errline<br>";
}
set_error_handler("my_error_handler");
$x = 5/0;
?> As you can see on the second last line, I've raised a divide by zero error. Here's how to error appeared in my browser:
Set_error_handler() is ideal for situations where a task must be performed when an error occurs, such as deleting temporary files, freeing memory, closing database connections, etc. It's also a handy way to track and log errors on a development server when performing desktop checks, etc.
Next: Conclusion >>
More PHP Articles
More By Mitchell Harper