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 172 173 174 175 176 177 178 179 180 181 182 183
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 288721 $ -->
<sect1 xml:id="language.types.boolean">
<title>Booleans</title>
<simpara>
This is the simplest type. A <type>boolean</type> expresses a truth value. It
can be either &true; or &false;.
</simpara>
<note>
<simpara>
The <type>boolean</type> type was introduced in PHP 4.
</simpara>
</note>
<sect2 xml:id="language.types.boolean.syntax">
<title>Syntax</title>
<para>
To specify a <type>boolean</type> literal, use the keywords &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, some kind of <link linkend="language.operators">operator</link>
which returns a <type>boolean</type> value, and the 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 test
// 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 instead, this can be used:
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>boolean</type>, use the
<literal>(bool)</literal> or <literal>(boolean)</literal> casts. However, in
most cases the cast is unncecessary, since a value will be automatically
converted if an operator, function or control structure requires a
<type>boolean</type> argument.
</simpara>
<simpara>
See also <link linkend="language.types.type-juggling">Type Juggling</link>.
</simpara>
<para>
When converting to <type>boolean</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> 0 (zero)
</simpara>
</listitem>
<listitem>
<simpara>
the <link linkend="language.types.float">float</link> 0.0 (zero)
</simpara>
</listitem>
<listitem>
<simpara>
the empty <link linkend="language.types.string">string</link>, and the
<link linkend="language.types.string">string</link> "0"
</simpara>
</listitem>
<listitem>
<simpara>
an <link linkend="language.types.array">array</link> with zero elements
</simpara>
</listitem>
<listitem>
<simpara>
an <link linkend="language.types.object">object</link> with zero member
variables (PHP 4 only)
</simpara>
</listitem>
<listitem>
<simpara>
the special type <link linkend="language.types.null">NULL</link> (including
unset variables)
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="ref.simplexml">SimpleXML</link> objects created from empty
tags
</simpara>
</listitem>
</itemizedlist>
<para>
Every other value is considered &true; (including any
<link linkend="language.types.resource">resource</link>).
</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) 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
-->
|