PHP
  Home arrow PHP arrow Page 3 - 10 PHP Functions I Bet You Didn't Know...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

10 PHP Functions I Bet You Didn't Know About!
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 23
    2002-08-09

    Table of Contents:
  • 10 PHP Functions I Bet You Didn't Know About!
  • Functions 1 to 5
  • Functions 6 to 10
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    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.

    More PHP Articles
    More By Mitchell Harper


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway