1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect1 xml:id="language.types.boolean">
<title>Booleans</title>
<simpara>
The <type>bool</type> type only has two values, and is used to express
a truth value. It can be either &true; or &false;.
</simpara>
<sect2 xml:id="language.types.boolean.syntax">
<title>Syntax</title>
<para>
To specify a <type>bool</type> literal, use the constants &true; or
&false;. Both are case-insensitive.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = True; // assign the value TRUE to $foo
?>
]]>
</programlisting>
</informalexample>
<para>
Typically, the result of an <link linkend="language.operators">operator</link>
which returns a <type>bool</type> value is passed on to a
<link linkend="language.control-structures">control structure</link>.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
echo "The version is 1.23";
}
// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}
// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "<hr>\n";
}
?>
]]>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="language.types.boolean.casting">
<title>Converting to boolean</title>
<simpara>
To explicitly convert a value to <type>bool</type>, use the
<literal>(bool)</literal> cast. Generally this is not necessary because when
a value is used in a logical context it will be automatically interpreted
as a value of type <type>bool</type>. For more information see the
<link linkend="language.types.type-juggling">Type Juggling</link> page.
</simpara>
<para>
When converting to <type>bool</type>, the following values are considered
&false;:
</para>
<itemizedlist>
<listitem>
<simpara>
the <link linkend="language.types.boolean">boolean</link> &false; itself
</simpara>
</listitem>
<listitem>
<simpara>
the <link linkend="language.types.integer">integer</link>
<literal>0</literal> (zero)
</simpara>
</listitem>
<listitem>
<simpara>
the <link linkend="language.types.float">float</link>s
<literal>0.0</literal> and <literal>-0.0</literal> (zero)
</simpara>
</listitem>
<listitem>
<simpara>
the empty <link linkend="language.types.string">string</link> <literal>""</literal>,
and the <link linkend="language.types.string">string</link> <literal>"0"</literal>
</simpara>
</listitem>
<listitem>
<simpara>
an <link linkend="language.types.array">array</link> with zero elements
</simpara>
</listitem>
<listitem>
<simpara>
the unit type <link linkend="language.types.null">NULL</link> (including
unset variables)
</simpara>
</listitem>
<listitem>
<simpara>
Internal objects that overload their casting behaviour to bool.
For example: <link linkend="ref.simplexml">SimpleXML</link> objects
created from empty elements without attributes.
</simpara>
</listitem>
</itemizedlist>
<para>
Every other value is considered &true;
(including <link linkend="language.types.resource">resource</link>
and <constant>NAN</constant>).
</para>
<warning>
<simpara>
<literal>-1</literal> is considered &true;, like any other non-zero
(whether negative or positive) number!
</simpara>
</warning>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
var_dump((bool) ""); // bool(false)
var_dump((bool) "0"); // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
?>
]]>
</programlisting>
</informalexample>
</sect2>
</sect1>
<!-- 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
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/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
-->
|