note 68823 deleted from function.define by bobby
| From: | bobby@php.net | Date: | Mon, 02 Oct 2006 17:43:37 +0000 |
| Subject: | note 68823 deleted from function.define by bobby | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-117951@lists.php.net to get a copy of this message | ||
Note Submitter: kramdar at gmail dot com
----
I wrote this iffdefine function and I find that I use it all
over the place.
bool iffdefine ( string name, mixed value [, bool case_insensitive] )
It defines a constant if and only if (hense iffdefine) the
constant has not already been defined, otherwise do not
trigger an error or notice.
--kramdar
<?php
//------------------------------------------------------------------//
// bool iffdefine
// ( string name, mixed value [, bool case_insensitive] )
//
// Defines a constant only if the constant has not already
// been defined, otherwise do not trigger an error or notice.
//
// string name
// The name of a constant follows the same rules as any
// label in PHP. A valid constant name starts with a letter*
// or underscore, followed by any number of letters,
// numbers, or underscores:
//
// *NOTE: a letter is a-z, A-Z, and the ASCII characters
// from 127 through 255 (0x7f-0xff).
//
// mixed value
// Only scalar data (boolean, integer, float and string) can
// be contained in constants. A scalar is a variable or field
// that can hold only one value at a time; as opposed to
// composite variables like array, list, record, etc..
//
// case_insensitive
// The optional third parameter case_insensitive is also
// available. If the value TRUE is given, then the constant
// will be defined case-insensitive. The default behaviour is
// case-sensitive; i.e. CONSTANT and Constant represent
// different values.
//
// Returns TRUE on success or FALSE on failure.
//
//
// Usage: iffdefine( 'MAX_LENGTH', 12345, true );
//
// See also: define()
// http://us3.php.net/manual/en/function.define.php
//------------------------------------------------------------------//
if ( !defined( 'iffdefine' ) )
{
function iffdefine( $name, $value, $case_insensitive = FALSE )
{
// Although define will do its own data checking, ensure that the
// function was passed the correct arguments.
// String name match
$nameMatch = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/';
// Valid scalar types:
$scalars = array( "boolean", "integer", "float",
"string");
if ( !preg_match( $nameMatch, $name ) )
{
trigger_error(
"iffdefine: Invalid constant name: {$name}"
. "\nA valid constant name starts with a letter or"
. " underscore, followed by any number of letters,"
. " numbers, or underscores.",
E_USER_ERROR );
return FALSE;
}
// Ensure value is a scalar.
else if ( !in_array( getType( $value ), $scalars ) )
{
trigger_error(
"iffdefine: Invalid value: "
. var_export( $value, true )
."\nThe value provided is not of scalar type: boolean,"
. " integer, float or string.",
E_USER_ERROR );
return FALSE;
}
// If the constant has already been defined
else if( defined( $name ) )
{
//trigger_error(
// "iffdefine: A constant with that name ($name)"
// . " is already defined with value: "
// . var_export( $name, true )
// . "",
// E_USER_ERROR );
return FALSE;
}
else if( !defined( $name ) )
{
return define( $name, $value, $case_insensitive );
}
}
}
?>