- Welcome to Geeksww.com
PHP Useful functions (Part 1) - The inRange function
Developing websites, social applications, and scripts in PHP for more than 6 years now, I found a number of utility functions helpful in development. I am going to write a series of tutorials writing such functions and describing their use. Using these functions I have created a library of dynamically loadable classes but you are free to use them as you wish.
The inRange Function:
I am going to discuss the inRange function here. Here is the function header:
function inRange($low, $high, $val)
As can be seen the function takes three arguments a low value, high value, and the value to be compared, as input.
Examples:
I mostly use this function when I do string length verification for user input through forms and input validation. For example, you have a select box with countries with IDs from 1 to 172, you can use the function in an IF block as follows:
if(inRange(1, 172, $input)) { // do something }
You can also check the string length. For example, name field in the database allows up to 30 characters, hence you'll use the following If condition to verify the length of name input by the user.
if(inRange(1, 30, strlen($input))) { // do something }
which is obviously clearer than,
if(1 <= strlen($input) && 30 >= strlen($input)) { // do something }
OR
$len = strlen($input) if(1 <= $len && 30 >= $len) { // do something }
Function Definition:
Now, here is the definition of the function,
function isInRange($low, $high, $val) { return $val >= $low && $val <= $high; }
Did this tutorial help a little? How about buy me a cup of coffee?
Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.
IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.
tags cloud
popular searches
free download for mysql database server 5.1.5, bison, gearman, source code, php, install cairo, java, laptop, mysql, linux, install mysql, mysql mysql, mysql initialization, tools, ubuntu
Similar Tutorials:
- Generating prime nos in PHP using multiprocessing - Geaman with PHP client Part I
- Simple Coding Style for PHP - Part 1/2
- Installing Symfony using PEAR
- MySQL GUI Tools - Query Browser
- PHP Useful functions (Part 2) - The currentURL function
Tutorials in 'Web Development > PHP' (more):
- Generating prime nos in PHP using multiprocessing - Geaman with PHP client Part I
- PHP Useful functions (Part 2) - The currentURL function
- Generating Unique IDs in PHP
- How to check PHP version number?
- Installing Symfony using PEAR
Comments (write a comment):
0 comments so far. Be the first one to leave a comment on this article.
leave a comment