note 68823 added to function.define
| From: | kramdar at gmail dot com | Date: | Sat, 12 Aug 2006 00:41:10 +0000 |
| Subject: | note 68823 added to function.define | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115817@lists.php.net to get a copy of this message | ||
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 );
}
}
}
?>
----
Server IP: 216.194.113.175
Probable Submitter: 65.197.216.2 (proxied: 10.0.1.248)
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.define.php
Edit -- https://master.php.net/note/edit/68823
Del: integrated -- https://master.php.net/note/delete/68823/integrated
Del: useless -- https://master.php.net/note/delete/68823/useless
Del: bad code -- https://master.php.net/note/delete/68823/bad+code
Del: spam -- https://master.php.net/note/delete/68823/spam
Del: non-english -- https://master.php.net/note/delete/68823/non-english
Del: in docs -- https://master.php.net/note/delete/68823/in+docs
Del: other reasons-- https://master.php.net/note/delete/68823
Reject -- https://master.php.net/note/reject/68823
Search -- https://master.php.net/manage/user-notes.php