php-doc-en/functions/var.xml
Jeroen van Wolffelaar d00d946cb5 4.0.7 -> 4.1.0
4.0.8 -> 4.2.0


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@60091 c90b9560-bf6c-de11-be94-00142212c4b1
2001-10-17 16:24:01 +00:00

1346 lines
38 KiB
XML

<?xml encoding="iso-8859-1"?>
<!-- $Revision: 1.73 $ -->
<reference id="ref.var">
<title>Variable Functions</title>
<titleabbrev>Variables</titleabbrev>
<refentry id="function.doubleval">
<refnamediv>
<refname>doubleval</refname>
<refpurpose>Alias of <function>floatval</function></refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<para>
This function is an alias of <function>floatval</function>.
</para>
<note>
<para>
This alias is a left-over from a function-renaming. In older versions of
PHP you'll need to use this alias of the <function>floatval</function>
function, because <function>floatval</function> wasn't yet available in
that version.
</para>
</note>
</refsect1>
</refentry>
<refentry id="function.empty">
<refnamediv>
<refname>empty</refname>
<refpurpose>Determine whether a variable is set</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>empty</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<note>
<para>
<function>empty</function> is a language construct.
</para>
</note>
<para>
This is the opposite of
<literal>(boolean) <parameter>var</parameter></literal>,
except that no warning is generated when the variable is not set.
See <link linkend="language.types.boolean.casting">converting
to boolean</link> for more information.
</para>
<!-- Returns &false; if <parameter>var</parameter> is set and has a
non-empty or non-zero value; &true; otherwise. -->
<informalexample>
<programlisting role="php">
$var = 0;
if (empty($var)) { // evaluates true
echo '$var is either 0 or not set at all';
}
if (!isset($var)) { // evaluates false
echo '$var is not set at all';
}
</programlisting>
</informalexample>
<simpara>
Note that this is meaningless when used on anything which isn't a
variable; i.e. <command>empty (addslashes ($name))</command> has
no meaning since it would be checking whether something which
isn't a variable is a variable with a &false; value.
<!-- will even result in parse error (at least in PHP 4) -->
</simpara>
<simpara>
See also <function>isset</function> and
<function>unset</function>.
</simpara>
</refsect1>
</refentry>
<refentry id="function.floatval">
<refnamediv>
<refname>floatval</refname>
<refpurpose>Get float value of a variable</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>float <function>floatval</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<simpara>
Returns the <type>float</type> value of <parameter>var</parameter>.
</simpara>
<para>
<parameter>Var</parameter> may be any scalar type. You cannot use
<function>floatval</function> on arrays or objects.
<informalexample>
<programlisting role="php">
$var = '122.34343The';
$float_value_of_var = floatval ($var);
print $float_value_of_var; // prints 122.34343
</programlisting>
</informalexample>
</para>
<simpara>
See also <function>intval</function>,
<function>strval</function>, <function>settype</function> and
<link linkend="language.types.type-juggling">Type
juggling</link>.
</simpara>
</refsect1>
</refentry>
<refentry id="function.gettype">
<refnamediv>
<refname>gettype</refname>
<refpurpose>Get the type of a variable</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>string <function>gettype</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
Returns the type of the PHP variable
<parameter>var</parameter>.
</para>
<warning>
<simpara>
Never use <function>gettype</function> to test for a certain type,
since the returned string may be subject to change in a future version.
In addition, it is slow too, as it involves string comparision <!--
where's my dictionary? -->.
</simpara>
<simpara>
Instead, use the <literal>is_*</literal> functions.
</simpara>
</warning>
<para>
Possibles values for the returned string are:
<itemizedlist>
<listitem>
<simpara>"<type>boolean</type>" (since PHP 4)</simpara>
</listitem>
<listitem>
<simpara>"<type>integer</type>"</simpara>
</listitem>
<listitem>
<simpara>"<type>double</type>" (for historical reasons "double" is
returned in case of a <type>float</type>, and not simply
"float")</simpara>
</listitem>
<listitem>
<simpara>"<type>string</type>"</simpara>
</listitem>
<listitem>
<simpara>"<type>array</type>"</simpara>
</listitem>
<listitem>
<simpara>"<type>object</type>"</simpara>
</listitem>
<listitem>
<simpara>"<type>resource</type>" (since PHP 4)</simpara>
</listitem>
<listitem>
<simpara>"<type>null</type>" (since PHP 4)</simpara>
</listitem>
<listitem>
<simpara>"user function" (PHP 3 only, deprecated)</simpara>
</listitem>
<listitem>
<simpara>"unknown type"<!-- someone's joking? --></simpara>
</listitem>
</itemizedlist>
</para>
<para>
For PHP 4, you should use <function>function_exists</function> and
<function>method_exists</function> to replace the prior usage of
<function>gettype</function> on a function.
</para>
<para>
See also
<function>settype</function>,
<function>is_array</function>,
<function>is_bool</function>,
<function>is_float</function>,
<function>is_integer</function>,
<function>is_null</function>,
<function>is_numeric</function>,
<function>is_object</function>,
<function>is_resource</function>,
<function>is_scalar</function> and
<function>is_string</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.get-defined-vars">
<refnamediv>
<refname>get_defined_vars</refname>
<refpurpose>
Returns an array of all defined variables
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>array <function>get_defined_vars</function></funcdef>
<void/>
</funcprototype>
</funcsynopsis>
<para>
This function returns an multidimensional array containing a list of
all defined variables, be them environment, server or user-defined
variables.
<informalexample>
<programlisting role="php">
$b = array(1,1,2,3,5,8);
$arr = get_defined_vars();
// print $b
print_r($arr["b"]);
// print path to the PHP interpreter (if used as a CGI)
// e.g. /usr/local/bin/php
echo $arr["_"];
// print the command-line paramaters if any
print_r($arr["argv"]);
// print all the server vars
print_r($arr["HTTP_SERVER_VARS"]);
// print all the available keys for the arrays of variables
print_r(array_keys(get_defined_vars()));
</programlisting>
</informalexample>
</para>
<para>
See also <function>get_defined_functions</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.get-resource-type">
<refnamediv>
<refname>get_resource_type</refname>
<refpurpose>
Returns the resource type
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>string <function>get_resource_type</function></funcdef>
<paramdef>resource <parameter>$handle</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
This function returns a string representing the type of the
<type>resource</type> passed to it. If the paramater is not a
valid <type>resource</type>, it
generates an error.
<informalexample>
<programlisting role="php">
$c = mysql_connect();
echo get_resource_type($c)."\n";
// prints: mysql link
$fp = fopen("foo","w");
echo get_resource_type($fp)."\n";
// prints: file
$doc = new_xmldoc("1.0");
echo get_resource_type($doc->doc)."\n";
// prints: domxml document
</programlisting>
</informalexample>
</para>
</refsect1>
</refentry>
<refentry id="function.intval">
<refnamediv>
<refname>intval</refname>
<refpurpose>Get integer value of a variable</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>int <function>intval</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
<paramdef>int
<parameter><optional>base</optional></parameter>
</paramdef>
</funcprototype>
</funcsynopsis>
<simpara>
Returns the <type>integer</type> value of <parameter>var</parameter>,
using the specified base for the conversion (the default is
base 10).
</simpara>
<simpara>
<parameter>var</parameter> may be any scalar type. You cannot use
<function>intval</function> on <type>array</type>s or <type>object</type>s.
</simpara>
<note>
<para>
The <parameter>base</parameter> argument for
<function>intval</function> has no effect unless the
<parameter>var</parameter> argument is a string.
</para>
</note>
<simpara>
See also <function>floatval</function>,
<function>strval</function>, <function>settype</function> and
<link linkend="language.types.type-juggling">Type
juggling</link>.
</simpara>
</refsect1>
</refentry>
<refentry id="function.is-array">
<refnamediv>
<refname>is_array</refname>
<refpurpose>Finds whether a variable is an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>bool <function>is_array</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
Returns &true; if <parameter>var</parameter> is an <type>array</type>, &false;
otherwise.
</para>
<para>
See also
<function>is_float</function>,
<function>is_int</function>,
<function>is_integer</function>,
<function>is_string</function>, and
<function>is_object</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.is-bool">
<refnamediv>
<refname>is_bool</refname>
<refpurpose>
Finds out whether a variable is a boolean
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>bool
<function>is_bool</function>
</funcdef>
<paramdef>mixed
<parameter>var</parameter>
</paramdef>
</funcprototype>
</funcsynopsis>
<para>
Returns &true; if the <parameter>var</parameter> parameter is
a <type>boolean</type>.
</para>
<para>
See also
<function>is_array</function>,
<function>is_float</function>,
<function>is_int</function>,
<function>is_integer</function>,
<function>is_string</function>, and
<function>is_object</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.is-double">
<refnamediv>
<refname>is_double</refname>
<refpurpose>Alias of <function>is_float</function></refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<para>
This function is an alias of <function>is_float</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.is-float">
<refnamediv>
<refname>is_float</refname>
<refpurpose>Finds whether a variable is a float</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>bool <function>is_float</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<simpara>
Returns &true; if <parameter>var</parameter> is a <type>float</type>,
&false; otherwise.
</simpara>
<simpara>
See also
<function>is_bool</function>,
<function>is_int</function>,
<function>is_integer</function>,
<function>is_string</function>,
<function>is_array</function>, and
<function>is_object</function>,
</simpara>
</refsect1>
</refentry>
<refentry id="function.is-int">
<refnamediv>
<refname>is_int</refname>
<refpurpose>Find whether a variable is an integer</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>bool <function>is_int</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<simpara>
Returns &true; if <parameter>var</parameter> is an <type>integer</type>
&false; otherwise.
</simpara>
<simpara>
See also <function>is_bool</function>,
<function>is_float</function>,
<function>is_integer</function>,
<function>is_string</function>,
<function>is_array</function>, and
<function>is_object</function>.
</simpara>
</refsect1>
</refentry>
<refentry id="function.is-integer">
<refnamediv>
<refname>is_integer</refname>
<refpurpose>Alias of <function>is_int</function></refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<para>
This function is an alias of <function>is_int</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.is-long">
<refnamediv>
<refname>is_long</refname>
<refpurpose>Alias of <function>is_int</function></refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<para>
This function is an alias of <function>is_int</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.is-null">
<refnamediv>
<refname>is_null</refname>
<refpurpose>
Finds whether a variable is &null;
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>bool <function>is_null</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
Returns &true; if <parameter>var</parameter> is <type>null</type>,
&false; otherwise.
</para>
<para>
See also <function>is_bool</function>,
<function>is_numeric</function>,
<function>is_float</function>,
<function>is_int</function>,
<function>is_string</function>,
<function>is_object</function>,
<function>is_array</function>, and
</para>
</refsect1>
</refentry>
<refentry id="function.is-numeric">
<refnamediv>
<refname>is_numeric</refname>
<refpurpose>
Finds whether a variable is a number or a numeric string
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>bool <function>is_numeric</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
Returns &true; if <parameter>var</parameter> is a number or a
numeric string, &false; otherwise.
</para>
<para>
See also <function>is_bool</function>,
<function>is_float</function>,
<function>is_int</function>,
<function>is_string</function>,
<function>is_object</function>,
<function>is_array</function>, and
<function>is_integer</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.is-object">
<refnamediv>
<refname>is_object</refname>
<refpurpose>Finds whether a variable is an object</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>bool <function>is_object</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
Returns &true; if <parameter>var</parameter> is an <type>object</type>,
&false; otherwise.
</para>
<para>
See also <function>is_bool</function>,
<function>is_int</function>,
<function>is_integer</function>,
<function>is_float</function>,
<function>is_string</function>, and
<function>is_array</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.is-real">
<refnamediv>
<refname>is_real</refname>
<refpurpose>Alias of <function>is_float</function></refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<para>
This function is an alias of <function>is_float</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.is-resource">
<refnamediv>
<refname>is_resource</refname>
<refpurpose>
Finds whether a variable is a resource
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>bool
<function>is_resource</function>
</funcdef>
<paramdef>mixed
<parameter>var</parameter>
</paramdef>
</funcprototype>
</funcsynopsis>
<para>
<function>is_resource</function> returns &true; if the variable
given by the <parameter>var</parameter> parameter is a
<type>resource</type>, otherwise it returns &false;.
</para>
<para>
See the documentation on the <type>resource</type>-type for
more information.
</para>
</refsect1>
</refentry>
<refentry id="function.is-scalar">
<refnamediv>
<refname>is_scalar</refname>
<refpurpose>
Finds whether a variable is a scalar
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>bool
<function>is_scalar</function>
</funcdef>
<paramdef>mixed
<parameter>var</parameter>
</paramdef>
</funcprototype>
</funcsynopsis>
<para>
<function>is_scalar</function> returns &true; if the variable
given by the <parameter>var</parameter> parameter is a scalar,
otherwise it returns &false;.
</para>
<para>
Scalar variables are those containing an <type>integer</type>,
<type>float</type>, <type>string</type> or <type>boolean</type>.
For example:
<informalexample>
<programlisting role="php">
<!-- TODO: better example, this one can be quite misleading for unexperienced
programmers -->
function show_var($var) {
if (is_scalar($var))
echo $var;
else
var_dump($var);
}
$pi = 3.1416;
$proteins = array("hemoglobin", "cytochrome c oxidase", "ferredoxin");
show_var($pi);
// prints: 3.1416
show_var($proteins)
// prints:
// array(3) {
// [0]=>
// string(10) "hemoglobin"
// [1]=>
// string(20) "cytochrome c oxidase"
// [2]=>
// string(10) "ferredoxin"
// }
</programlisting>
</informalexample>
</para>
<note>
<para>
<function>is_scalar</function> does not consider <type>resource</type>
type values to be scalar values. This behavior is intentional:
Resources are abstract datatypes which are currently based on
integers. This implementation detail should not be relied upon,
as it may change.
</para>
</note>
<note>
<para>
Since 4.0.5
</para>
</note>
<para>
See also <function>is_bool</function>,
<function>is_numeric</function>,
<function>is_float</function>,
<function>is_int</function>,
<function>is_real</function>,
<function>is_string</function>,
<function>is_object</function>,
<function>is_array</function>, and
<function>is_integer</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.is-string">
<refnamediv>
<refname>is_string</refname>
<refpurpose>Finds whether a variable is a string</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>bool <function>is_string</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
Returns &true; if <parameter>var</parameter> is a <type>string</type>,
&false; otherwise.
</para>
<para>
See also <function>is_bool</function>,
<function>is_int</function>,
<function>is_integer</function>,
<function>is_float</function>,
<function>is_real</function>,
<function>is_object</function>, and
<function>is_array</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.isset">
<refnamediv>
<refname>isset</refname>
<refpurpose>Determine whether a variable is set</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>isset</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<simpara>
Returns &true; if <parameter>var</parameter>
exists; &false; otherwise.
</simpara>
<para>
If a variable has been unset with <function>unset</function>,
it will no longer be <function>isset</function>. <function>isset</function>
will return &false; if testing a variable that has been
set to &null;. Also note that a &null; byte (<literal>"\0"</literal>)
is not equivalent to the PHP &null; constant.
<informalexample>
<programlisting role="php">
$a = "test";
echo isset ($a); // TRUE
unset ($a);
echo isset ($a); // FALSE
$foo = NULL;
print isset ($foo); // FALSE
</programlisting>
</informalexample>
</para>
<simpara>
See also <function>empty</function> and
<function>unset</function>.
</simpara>
</refsect1>
</refentry>
<refentry id="function.print-r">
<refnamediv>
<refname>print_r</refname>
<refpurpose>
Prints human-readable information about a variable
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>print_r</function></funcdef>
<paramdef>mixed <parameter>expression</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<simpara>
This function displays information about the values of variables
in a way that's readable by humans. If given a <type>string</type>,
<type>integer</type> or <type>float</type>, the value itself will be
printed. If given an <type>array</type>,
values will be presented in a format that shows keys and
elements. Similar notation is used for <type>object</type>s.
</simpara>
<simpara>
Remember that <function>print_r</function> will move the array
pointer to the end. Use <function>reset</function> to bring
it back to beginning.
</simpara>
&tip.ob-capture;
<simpara>
Compare <function>print_r</function> to
<function>var_dump</function>.
</simpara>
<para>
<informalexample>
<programlisting role="php">
&lt;?php
$a = array (1, 2, array ("a", "b", "c"));
print_r ($a);
?>
</programlisting>
</informalexample>
</para>
<warning>
<para>
This function will continue forever if given an <type>array</type>
or <type>object</type>
that contains a direct or indirect reference to itself or that
contains an <type>array</type> or <type>object</type> on a deeper
level that does so.
This is especially &true; for <literal>print_r($GLOBALS)</literal>,
as <literal>$GLOBALS</literal> is itself a global variable and
contains a reference to itself as such.
</para>
</warning>
</refsect1>
</refentry>
<refentry id="function.serialize">
<refnamediv>
<refname>serialize</refname>
<refpurpose>
Generates a storable representation of a value
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>string <function>serialize</function></funcdef>
<paramdef>mixed <parameter>value</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<simpara>
<function>serialize</function> returns a string containing a
byte-stream representation of <parameter>value</parameter> that
can be stored anywhere.
</simpara>
<simpara>
This is useful for storing or passing PHP values around without
losing their type and structure.
</simpara>
<simpara>
To make the serialized string into a PHP value again, use
<function>unserialize</function>. <function>serialize</function>
handles all types, except the <type>resource</type>-type.
You can even <function>serialize</function> arrays that contain
references to itself. References inside the array/object you
are <function>serialize</function>ing will also be stored.
</simpara>
<!-- TODO
in 4.0.4pl1 this didn't work properly, however, there
been some fixes. I don't know whether this all
works correctly now, and if so, whether it is since 405
or 406
<note>
<simpara>
This didn't work correctly until 4.0.?
</simpara>
</note>
</simpara>
-->
<note>
<para>
In PHP 3, object properties will be serialized, but methods are
lost. PHP 4 removes that limitation and restores both properties
and methods. Please see the <link
linkend="language.oop.serialization">Serializing Objects</link>
section of <link linkend="language.oop">Classes and
Objects</link> for more information.
</para>
</note>
<para>
<example>
<title><function>serialize</function> example</title>
<programlisting role="php">
// $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, &amp;$sqldata)) {
$stmt = odbc_prepare($conn,
"INSERT INTO sessions (id, data) VALUES(?, ?)");
if (!odbc_execute($stmt, &amp;$sqldata)) {
/* Something went wrong. Bitch, whine and moan. */
}
}
</programlisting>
</example>
</para>
<para>
See Also: <function>unserialize</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.settype">
<refnamediv>
<refname>settype</refname>
<refpurpose>Set the type of a variable</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>boolean <function>settype</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
<paramdef>string <parameter>type</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
Set the type of variable <parameter>var</parameter> to
<parameter>type</parameter>.
</para>
<para>
Possibles values of <parameter>type</parameter> are:
<itemizedlist>
<listitem>
<simpara>
"boolean" (or, since PHP 4.2.0, "bool")
</simpara>
</listitem>
<listitem>
<simpara>
"integer" (or, since PHP 4.2.0, "int")
</simpara>
</listitem>
<listitem>
<simpara>
"float" (only possible since PHP 4.2.0, for older versions use the
deprecated variant "double")
</simpara>
</listitem>
<listitem>
<simpara>
"string"
</simpara>
</listitem>
<listitem>
<simpara>
"array"
</simpara>
</listitem>
<listitem>
<simpara>
"object"
</simpara>
</listitem>
<listitem>
<simpara>
"null" (since PHP 4.0.8)
</simpara>
</listitem>
</itemizedlist>
</para>
<para>
Returns &true; if successful; otherwise returns
&false;.
</para>
<para>
<example>
<title><function>settype</function> example</title>
<programlisting role="php">
$foo = "5bar"; // string
$bar = true; // boolean
settype($foo, "integer"); // $foo is now 5 (integer)
settype($bar, "string"); // $bar is now "1" (string)
</programlisting>
</example>
</para>
<para>
See also <function>gettype</function>,
<link linkend="language.types.typecasting">type-casting</link> and
<link linkend="language.types.type-juggling">type-juggling</link>.
</para>
</refsect1>
</refentry>
<refentry id="function.strval">
<refnamediv>
<refname>strval</refname>
<refpurpose>Get string value of a variable</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>string <function>strval</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<simpara>
Returns the <type>string</type> value of <parameter>var</parameter>.
See the documentation on <type>string</type> for more information
on converting to string.
</simpara>
<simpara>
<parameter>var</parameter> may be any scalar type. You cannot use
<function>strval</function> on arrays or objects.
</simpara>
<simpara>
See also <function>floatval</function>,
<function>intval</function>, <function>settype</function> and
<link linkend="language.types.type-juggling">Type
juggling</link>.
</simpara>
</refsect1>
</refentry>
<refentry id="function.unserialize">
<refnamediv>
<refname>unserialize</refname>
<refpurpose>
Creates a PHP value from a stored representation
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>mixed <function>unserialize</function></funcdef>
<paramdef>string <parameter>str</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<simpara>
<function>unserialize</function> takes a single serialized
variable (see <function>serialize</function>) and converts it
back into a PHP value. The converted value is returned, and can
be an <type>integer</type>, <type>float</type>,
<type>string</type>, <type>array</type> or <type>object</type>.
If an object was serialized, its methods are not preserved in the
returned value.
</simpara>
<note>
<para>
In PHP 3, methods are not preserved when unserializing a
serialized object. PHP 4 removes that limitation and restores
both properties and methods. Please see the <link
linkend="language.oop.serialization">Serializing Objects</link>
section of <link linkend="language.oop">Classes and
Objects</link> or more information.
</para>
</note>
<para>
<example>
<title><function>unserialize</function> example</title>
<programlisting role="php">
// Here, we use unserialize() to load session data from a database
// into $session_data. This example complements the one described
// with <function>serialize</function>.
$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, &amp;$sqldata) || !odbc_fetch_into ($stmt, &amp;$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();
}
}
</programlisting>
</example>
</para>
<para>
See Also: <function>serialize</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.unset">
<refnamediv>
<refname>unset</refname>
<refpurpose>Unset a given variable</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>unset</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
<paramdef>mixed <parameter><optional>var</optional></parameter></paramdef>
<paramdef><parameter><optional>...</optional></parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
<function>unset</function> destroys the specified variables. Note
that in PHP 3, <function>unset</function> will always return &true;
(actually, the integer value 1). In PHP 4, however,
<function>unset</function> is no longer a &true; function: it is
now a statement. As such no value is returned, and attempting to
take the value of <function>unset</function> results in a parse
error.
</para>
<para>
<example>
<title><function>unset</function> example</title>
<programlisting role="php">
// destroy a single variable
unset ($foo);
// destroy a single element of an array
unset ($bar['quux']);
// destroy more than one variable
unset ($foo1, $foo2, $foo3);
</programlisting>
</example>
</para>
<para>
The behavior of <function>unset</function> inside of a function
can vary depending on what type of variable you are attempting to
destroy.
</para>
<para>
If a globalized variable is <function>unset</function> inside of
a function, only the local variable is destroyed. The variable
in the calling environment will retain the same value as before
<function>unset</function> was called.
<informalexample>
<programlisting role="php">
function destroy_foo() {
global $foo;
unset($foo);
}
$foo = 'bar';
destroy_foo();
echo $foo;
</programlisting>
</informalexample>
The above example would output:
<informalexample>
<programlisting>
bar
</programlisting>
</informalexample>
</para>
<para>
If a variable that is PASSED BY REFERENCE is
<function>unset</function> inside of a function, only the local
variable is destroyed. The variable in the calling environment
will retain the same value as before <function>unset</function>
was called.
<informalexample>
<programlisting role="php">
function foo(&amp;$bar) {
unset($bar);
$bar = "blah";
}
$bar = 'something';
echo "$bar\n";
foo($bar);
echo "$bar\n";
</programlisting>
</informalexample>
The above example would output:
<informalexample>
<programlisting>
something
something
</programlisting>
</informalexample>
</para>
<para>
If a static variable is <function>unset</function> inside of a
function, <function>unset</function> unsets the reference to the
static variable, rather than the static variable itself.
<informalexample>
<programlisting role="php">
function foo() {
static $a;
$a++;
echo "$a\n";
unset($a);
}
foo();
foo();
foo();
</programlisting>
</informalexample>
The above example would output:
<informalexample>
<programlisting>
1
2
3
</programlisting>
</informalexample>
</para>
<para>
If you would like to <function>unset</function> a global variable inside of a function, you can use the <parameter>$GLOBALS</parameter> array to do so:
<informalexample>
<programlisting role="php">
function foo() {
unset($GLOBALS['bar']);
}
$bar = "something";
foo();
</programlisting>
</informalexample>
</para>
<note>
<para>
<function>unset</function> is a language construct.
</para>
</note>
<para>
See also <function>isset</function> and
<function>empty</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.var-dump">
<refnamediv>
<refname>var_dump</refname>
<refpurpose>Dumps information about a variable</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>var_dump</function></funcdef>
<paramdef>mixed <parameter>expression</parameter></paramdef>
<paramdef>mixed <parameter><optional>expression</optional></parameter></paramdef>
<paramdef><parameter><optional>...</optional></parameter></paramdef>
</funcprototype>
</funcsynopsis>
<simpara>
This function returns structured information about one or more expressions
that includes its type and value. Arrays are explored
recursively with values indented to show structure.
</simpara>
&tip.ob-capture;
<simpara>
Compare <function>var_dump</function> to
<function>print_r</function>.
</simpara>
<para>
<informalexample>
<programlisting role="php">
&lt;pre&gt;
&lt;?php
$a = array (1, 2, array ("a", "b", "c"));
var_dump ($a);
/* output:
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
*/
$b = 3.1; $c = TRUE;
var_dump($b,$c);
/* output:
float(3.1)
bool(true)
*/
?&gt;
&lt;/pre&gt;
</programlisting>
</informalexample>
</para>
</refsect1>
</refentry>
</reference>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->