Miscellaneous functions Misc. These functions were placed here because none of the other categories seemed to fit. connection_aborted Return true if client disconnected Description int connection_aborted void Returns true if client disconnected. See the Connection Handling description in the Feature chapter for a complete explanation. connection_status Returns connection status bitfield Description int connection_status void Returns the connection status bitfield. See the Connection Handling description in the Feature chapter for a complete explanation. connection_timeout Return true if script timed out Description int connection_timeout void Returns true if script timed out. See the Connection Handling description in the Feature chapter for a complete explanation. eval Evaluate a string as PHP code Description void eval string code_str eval 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. die Output a message and terminate the current script Description void die string 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)"; ?> exit Terminate current script Description void exit This language construct terminates parsing of the script. It does not return. function_exists Return true if the given function has been defined Description int function_exists string function_name Checks the list of defined functions for function_name. Returns true if the given function name was found, false otherwise. ignore_user_abort Set whether a client disconnect should abort script execution Description int ignore_user_abort int 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. iptcparse Parse a binary IPTC http://www.xe.net/iptc/ block into single tags. Description array iptcparse string 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. leak Leak memory Description void leak int bytes Leak 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. pack pack data into binary string Description string pack string format mixed 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 string A SPACE-padded string h Hex string, low nibble first H Hex string, high nibble first c signed char C unsigned char s 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 byte X Back up one byte @ NUL-fill to absolute position pack 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_function Register a function for execution on shutdown. Description int register_shutdown_function string 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. serialize generates a storable representation of a value Description string serialize mixed value serialize 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. */ } } sleep Delay execution Description void sleep int seconds The sleep function delays program execution for the given number of seconds. See also usleep. unpack unpack data from binary string Description array unpack string format string 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. unserialize creates a PHP value from a stored representation Description mixed unserialize string str unserialize 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(); } } uniqid generate a unique id Description int uniqid string prefix uniqid 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. The prefix can be up to 114 characters long. 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. usleep Delay execution in microseconds Description void usleep int micro_seconds The sleep function delays program execution for the given number of micro_seconds. See also sleep.