Home arrow PHP arrow Page 3 - 10 PHP Functions I Bet You Didn't Know About!

10 PHP Functions I Bet You Didn't Know About!

PHP has hundreds of functions. In this article Mitchell looks at 10 uncommon yet useful PHP functions and shows you examples of how to use each of them to save time when coding.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 27
August 09, 2002
  1. · 10 PHP Functions I Bet You Didn't Know About!
  2. · Functions 1 to 5
  3. · Functions 6 to 10
  4. · Conclusion

print this article

10 PHP Functions I Bet You Didn't Know About! - Functions 6 to 10
(Page 3 of 4 )

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:


echo exif_imagetype("/pics/bounty.gif");
echo "You don't have the GD image library installed";


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:


define("TEST_1", "this is a test");

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.

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. Its 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:


$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.

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:


$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) {
string(4) "test"
array(1) {
string(8) "Mitchell"
string(2) "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.

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:


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>";


$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:

Our custom error handling function in action

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.
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials