Miscellaneous Functions

These functions were placed here because none of the other categories seemed to fit.

Miscellaneous Functions

eval

Name

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.

Example 1. 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.

exit

Name

exit — Terminate current script

Description

void exit(void);

This language construct terminates parsing of the script. It does not return.

iptcparse

Name

iptcparse — Parse a binary IPTC http://www.xe.net/iptc/ block into single tags.

Description

array (string iptcblock);

leak

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. See GetImageSize for a sample.

Name

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.

register_shutdown_function

Name

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.

serialize

Name

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).

Example 1. 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

Name

sleep — Delay execution

Description

void sleep(int seconds);

The sleep function delays program execution for the given number of seconds. See also usleep.

unserialize

Name

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.

Example 1. 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

Name

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.

usleep

Name

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.