WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Ensure that a specific value lies within a specific range.
Categories : PHP, Beginner Guides, Data Validation Click here to Update Your Picture
Alix Axel
Date : Jul 21st 2008
Grade : 4 of 5 (graded 2 times)
Viewed : 2027
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Alix Axel
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

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