|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
Ensure that a specific value lies within a specific range.
|
<?php
/*
Ever had to code something that relies on a numeric value being inside a specific range of minimum and maximum values?
I was coding some image functions and I found myself writing code like this:
if ($color['red'] < 0)
{
$color['red'] = 0;
}
else if ($color['red'] > 255)
{
$color['red'] = 255;
}
if ($color['green'] < 0)
{
$color['green'] = 0;
}
else if ($color['green'] > 255)
{
$color['green'] = 255;
}
if ($color['blue'] < 0)
{
$color['blue'] = 0;
}
else if ($color['blue'] > 255)
{
$color['blue'] = 255;
}
Seems like tremendous amount of code for something that simpler so I quickly made a simple (but handy!) function to handle this mess for me.
*/
function Between($number, $minimum = null, $maximum = null)
{
if (is_null($min) === false)
{
$min = floatval($min);
if ($value < $min)
{
$value = $min;
}
}
if (is_null($max) === false)
{
$max = floatval($max);
if ($value > $max)
{
$value = $max;
}
}
return $value;
}
// You can use it like this:
$color['red'] = Between($color['red'], 0, 255);
$color['green'] = Between($color['green'], 0, 255);
$color['blue'] = Between($color['blue'], 0, 255);
?> | | |
|
| Find the day of the week for any given year/month/day. Categories : PHP, Date Time, Data Validation, Algorithms, Beginner Guides | | | Find if a year is leap. Categories : PHP, Date Time, Beginner Guides, Data Validation | | | Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP, Form Processing, PHP Classes, Data Validation, Beginner Guides | | | Validating a URL with preg_match Categories : PHP, Regexps, Beginner Guides, Data Validation | | | PHP Image Validation Class - test if a specific file is of a certain image type without relying on the said file extension. Categories : PHP, PHP Classes, Data Validation, Graphics, Beginner Guides | | | Form Validation Using PHP to highlight non valid fields Categories : PHP, Form Processing, Data Validation, Beginner Guides | | | Newbie Notes #5 - To double quote, or single quote, that is the question Categories : PHP, Beginner Guides, Variables | | | for each record, do this to the first record, and do that to any subsequent record Categories : PHP, Databases, MySQL, Beginner Guides | | | Convert a File database into MySQL Categories : PHP, Filesystem, Databases, MySQL, Beginner Guides | | | PHP Function for Random Digits Categories : PHP, Beginner Guides | | | Newbie Notes #4 - Trapping dumb MySQL query errors Categories : PHP, Databases, MySQL, Debugging, Beginner Guides | | | Introduction to Language Files Categories : PHP, Filesystem, Beginner Guides | | | News management class Categories : PHP, PHP Classes, Beginner Guides | | | A flat file counter Categories : PHP, Cookies, Filesystem, Beginner Guides | | | SQLite PHP Database Wrapper Categories : PHP, PHP Classes, Databases, SQLite, Beginner Guides | |
|
|