Specifications
PHP has a useful capability to automatically or magically add and strip slashes for you. With
two settings in your php.ini file, you can turn on or off magic quoting for GET, POST, cookie
data, and for other sources.
The value of the magic_quotes_gpc directive controls whether magic quoting is used for GET,
POST, and Cookie operations.
With magic_quotes_gpc on, if somebody typed “Bob’s Auto Parts” into a form on your site,
your script would receive “Bob\’s Auto Parts” because the quote will be escaped for you.
The function get_magic_quotes_gpc() returns either 1 or 0, telling you the current value of
magic_quotes_gpc. This is most useful for testing if you need to stripslashes() from data
received from the user.
The value of
magic_quotes_runtime, controls whether magic quoting is used by functions that
get data from databases and files.
To get the value of
magic_quotes_runtime, use the function get_magic_quotes_runtime().
This function returns either 1 or 0. Magic quoting can be turned on for a particular script using
the function set_magic_quotes_runtime().
Evaluating Strings: eval()
The function eval() will evaluate a string as a PHP code.
For example,
eval ( “echo ‘Hello World’;” );
will take the contents of the string and execute it. This line will produce the same output as
echo ‘Hello World’;
There are a variety of cases in which eval() can be useful. You might want to store blocks of
code in a database, and retrieve and
eval() them at a later point. You might want to generate
code in a loop, and then use
eval() to execute it.
You can usefully use eval() to update or correct existing code. If you had a large collection of
scripts that needed a predictable change, it would be possible (but inefficient) to write a script
that loads an old script into a string, runs a regexp to make changes, and then uses eval() to
execute the modified script.
It is even conceivable that a very trusting person somewhere might want to allow PHP code to
be entered in a browser and executed on her server.
Other Useful Features
C
HAPTER 21
21
OTHER USEFUL
FEATURES
449
26 7842 CH21 3/6/01 3:41 PM Page 449










