Miscellaneous functionsMisc.
These functions were placed here because none of the other
categories seemed to fit.
connection_abortedReturns true if client disconnectedDescriptionint connection_abortedvoid
Returns true if client disconnected. See the Connection Handling
description in the Features
chapter for a complete explanation.
connection_statusReturns connection status bitfieldDescriptionint connection_statusvoid
Returns the connection status bitfield. See the Connection
Handling description in the Features chapter for a complete
explanation.
connection_timeoutReturn true if script timed outDescriptionint connection_timeoutvoid
Returns true if script timed out. See the Connection Handling
description in the Features
chapter for a complete explanation.
evalEvaluate a string as PHP codeDescriptionvoid evalstring code_streval evaluates the string given in
code_str as PHP code. Among other things,
this can be useful for storing code in a database text field for
later execution.
There are some factors to keep in mind when using
eval. Remember that the string passed must
be valid PHP code, including things like terminating statements
with a semicolon so the parser doesn't die on the line after the
eval, and properly escaping things in
code_str.
Also remember that variables given values under
eval will retain these values in the main
script afterwards.
eval() example - simple text merge
<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.<br>';
echo $str;
eval( "\$str = \"$str\";" );
echo $str;
?>
The above example will show:
This is a $string with my $name in it.
This is a cup with my coffee in it.
dieOutput a message and terminate the current scriptDescriptionvoid diestring message
This language construct outputs a message and terminates parsing
of the script. It does not return.
die example
<?php
$filename = '/path/to/data-file';
$file = fopen($filename, 'r')
or die "unable to open file ($filename)";
?>
exitTerminate current scriptDescriptionvoid exit
This language construct terminates parsing of the script. It
does not return.
function_existsReturn true if the given function has been definedDescriptionint function_existsstring function_name
Checks the list of defined functions for function_name.
Returns true if the given function name was found, false otherwise.
ignore_user_abortSet whether a client disconnect should abort script executionDescriptionint ignore_user_abortint setting
This function sets whether a client disconnect should cause a
script to be aborted. It will return the previous setting and
can be called without an argument to not change the current
setting and only return the current setting. See the Connection
Handling section in the Features chapter for a complete
description of connection handling in PHP.
iptcparseParse a binary IPTC
http://www.xe.net/iptc/ block into single tags.
Descriptionarray iptcparsestring iptcblock
This function parses a binary IPTC block into its single tags. It
returns an array using the tagmarker as an index and the value as
the value. It returns false on error or if no IPTC data was
found. See GetImageSize for a sample.
leakLeak memoryDescriptionvoid leakint bytesLeak leaks the specified amount of memory.
This is useful when debugging the memory manager, which automatically
cleans up "leaked" memory when each request is completed.
packpack data into binary stringDescriptionstring packstring formatmixed args...
Pack given arguments into binary string according to
format. Returns binary string containing data.
The idea to this function was taken from Perl and all formatting
codes work the same as there. The format string consists of
format codes followed by an optional repeater argument. The
repeater argument can be either an integer value or * for
repeating to the end of the input data. For a, A, h, H the repeat
count specifies how many characters of one data argument are
taken, for @ it is the absolute position where to put the next
data, for everything else the repeat count specifies how many
data arguments are consumed and packed into the resulting binary
string. Currently implemented are
a NUL-padded stringA SPACE-padded stringh Hex string, low nibble firstH Hex string, high nibble firstc signed charC unsigned chars signed short (always 16 bit, machine byte order)S unsigned short (always 16 bit, machine byte order)n unsigned short (always 16 bit, big endian
byte order)v unsigned short (always 16 bit, little
endian byte order)i signed integer (machine dependant size and
byte order)I unsigned integer (machine dependant size
and byte order)l signed long (always 32 bit, machine byte order)L unsigned long (always 32 bit, machine byte order)N unsigned long (always 32 bit, big endian
byte order)V unsigned long (always 32 bit, little endian
byte order)f float (machine dependent size and representation)d double (machine dependent size and representation)x NUL byteX Back up one byte@ NUL-fill to absolute positionpack format string
$binarydata = pack("nvc*", 0x1234, 0x5678, 65, 66);
The resulting binary string will be 6 bytes long and contain
the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.
Note that the distinction between signed and unsigned values only
affects the function unpack, where as
function pack gives the same result for
signed and unsigned format codes.
Also note that PHP internally stores integral values as signed
values of a machine dependant size. If you give it an unsigned
integral value too large to be stored that way it is converted to
a double which often yields an undesired result.
register_shutdown_functionRegister a function for execution on shutdown.Descriptionint register_shutdown_functionstring func
Registers the function named by func to be
executed when script processing is complete.
Common Pitfalls:
Since no output is allowed to the browser in this function, you will be
unable to debug it using statements such as print or echo.
serializegenerates a storable representation of a valueDescriptionstring serializemixed valueserialize returns a string containing a
byte-stream representation of value that
can be stored anywhere.
This is useful for storing or passing PHP values around without
losing their type and structure.
To make the serialized string into a PHP value again, use
unserialize. serialize
handles the types integer, double,
string, array (multidimensional) and
object (object properties will be serialized, but
methods are lost).
serialize example
// $session_data contains a multi-dimensional array with session
// information for the current user. We use serialize() to store
// it in a database at the end of the request.
$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn,
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array(serialize($session_data), $PHP_AUTH_USER);
if (!odbc_execute($stmt, &$sqldata)) {
$stmt = odbc_prepare($conn,
"INSERT INTO sessions (id, data) VALUES(?, ?)");
if (!odbc_execute($stmt, &$sqldata)) {
/* Something went wrong. Bitch, whine and moan. */
}
}
sleepDelay executionDescriptionvoid sleepint seconds
The sleep function delays program execution for the given number
of seconds.
See also usleep.
unpackunpack data from binary stringDescriptionarray unpackstring formatstring data
Unpack from binary string into array according to
format. Returns array containing unpacked
elements of binary string.
Unpack works slightly different from Perl as the unpacked data is
stored in an associative array. To accomplish this you have to
name the different format codes and separate them by a slash /.
unpack format string
$array = unpack("c2chars/nint", $binarydata);
The resulting array will contain the entries "chars1",
"chars2" and "int".
For an explanation of the format codes see also:
pack
Note that PHP internally stores integral values as signed. If you
unpack a large unsigned long and it is of the same size as PHP
internally stored values the result will be a negative number
even though unsigned unpacking was specified.
unserializecreates a PHP value from a stored representationDescriptionmixed unserializestring strunserialize takes a single serialized
variable (see serialize) and converts it
back into a PHP value. The converted value is returned, and can
be an integer, double,
string, array or object.
If an object was serialized, its methods are not preserved in the
returned value.
unserialize example
// Here, we use unserialize() to load session data from a database
// into $session_data. This example complements the one described
// with serialize.
$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn, "SELECT data FROM sessions WHERE id = ?");
$sqldata = array($PHP_AUTH_USER);
if (!odbc_execute($stmt, &$sqldata) || !odbc_fetch_into($stmt, &$tmp)) {
// if the execute or fetch fails, initialize to empty array
$session_data = array();
} else {
// we should now have the serialized data in $tmp[0].
$session_data = unserialize($tmp[0]);
if (!is_array($session_data)) {
// something went wrong, initialize to empty array
$session_data = array();
}
}
uniqidgenerate a unique idDescriptionint uniqidstring prefixboolean lcguniqid returns a prefixed unique identifier
based on current time in microseconds. The prefix can be useful
for instance if you generate identifiers simultaneously on
several hosts that might happen to generate the identifier at the
same microsecond. prefix can be up to 114
characters long.
If the optional lcg parameter is true,
uniqid will add additional "combined LCG"
entropy at the end of the return value, which should make the
results more unique.
Without an empty prefix, the returned string will be 13
characters long. If lcg is true, it will
be 23 characters.
If you need a unique identifier or token and you intend to give
out that token to the user via the network (i.e. session cookies),
it is recommended that you use something along the lines of
$token = md5(uniqid("")); // no random portion
$better_token = md5(uniqid(rand())); // better, difficult to guess
This will create a 32 character identifier (a 128 bit hex number)
that is extremely difficult to predict.
usleepDelay execution in microsecondsDescriptionvoid usleepint micro_seconds
The sleep function delays program execution for the given number
of micro_seconds.
See also sleep.