PHP
  Home arrow PHP arrow Page 4 - Using Filters in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
PHP

Using Filters in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2009-07-15


    Table of Contents:
  • Using Filters in PHP 5
  • Filters available in PHP 5
  • Checking for integer values with the filter PHP extension
  • Using the FILTER_VALIDATE_INT filter to check for minimal and maximal values

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Using Filters in PHP 5 - Using the FILTER_VALIDATE_INT filter to check for minimal and maximal values
    ( Page 4 of 4 )

    Since you learned in the previous segment how to use the FILTER_VALIDATE_INT filter to validate integers, I’m now going to explain another way to use it. This time we'll check not only if a given value is an integer, but if it’s within a specified range. How can this be done? It’s simple, really; adding an optional array to the “filter_var()” function allows you to specify minimal and maximal boundaries for a determined value.

    The following code samples show in a nutshell how to accomplish this task in a truly intuitive way. Take a look at them:

     

    $min = 1;

    $max = 99;

    $input = 101;

    if(filter_var($input, FILTER_VALIDATE_INT, array("options" => array("min_range" => $min, "max_range"=> $max ))) === FALSE) // displays Error: Input must be a value between 1 and 99 (Correct specification for the min and max options)

    {

    echo 'Error: Input must be a value between 1 and 99.';

    }

    else

    {

    echo 'Input is correct';

    }

     

    $min = 1;

    $max = 99;

    $input = 101;

    if(filter_var($input, FILTER_VALIDATE_INT, array("options" => array("max_range"=> $max ))) === FALSE) // displays Error: Input must be a value between 1 and 99 (Specifies only the max option)

    {

    echo 'Error: Input must be a value between 1 and 99.';

    }

    else

    {

    echo 'Input is correct';

    }

    As I said before, by adding an “options” array to the “filter_var()” function it’s possible to check not only if the inputted value is an integer, but if it belongs to a specified range. In the first case, both minimal and maximal boundaries have been specified, while in the second example, only the upper limit has been set.

    From the previous code samples, it’s easy to see how useful the FILTER_VALIDATE_INT filter can be for validating integers. However, I’m only scratching the surface of the PHP 5 filter library; as you saw at the beginning, it comes packaged with many other handy filters. These will be discussed in successive articles of this series.

    Final thoughts

    In this initial chapter of the series, I introduced you to using the filter extension that comes included with PHP 5. As you were able to see in all of the code samples included previously, checking to see whether or not a specified value is an integer is an extremely straightforward process thanks to the help of the FILTER_VALIDATE_INT filter.

    In the forthcoming article, I’m going to explain how to use the filter extension to validate array elements and to check both octal and hexadecimal integers. Don’t miss the next part!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek