function is_odd($x) { return ($x & 1); //integer }
function is_even($x) { return (!($x & 1)); //integer }
if(is_even(10) === TRUE)
// NO
function is_odd($x) { return (bool) ($x & 1); //boolean }
function is_even($x) { return (bool) (!($x & 1)); //boolean }
if(is_even(10) === TRUE)
// YES
$str = 'Hello World!';
if($str === TRUE)
// ecetera
@+
Booleans
Dies ist der einfachste Typ. Ein boolean Ausdruck ist ein Wahrheitswert der entweder TRUE (wahr) oder FALSE (falsch) sein kann.
Hinweis: Der boolean Typ wurde in PHP 4 eingeführt.
Syntax
Ein boolean Wert wird über die Schlüsselworte TRUE und FALSE spezifiziert, Groß- und Kleinschreibung ist dabei nicht von Bedeutung.
<?php
$foo = True; // weist $foo den Wert TRUE zu
?>
Normalerweise wird ein boolean von einem Operator zurückgegeben und an eine Kontrollstruktur weitergegeben.
<?php
// == ist ein Operator der auf Gleichheit prüft
// und ein boolean Ergebnis zurückgibt
if ($action == "show_version") {
echo "Die Version ist 1.23";
}
// die Angabe von '== TRUE' ist nicht nötig...
if ($show_separators == TRUE) {
echo "<hr>\n";
}
// ... weil folgendes *genau* die selbe Bedeutung hat:
if ($show_separators) {
echo "<hr>\n";
}
?>
Converting to boolean
To explicitly convert a value to boolean, use the (bool) or (boolean) 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 boolean argument.
Siehe auch Typumwandlungen.
Bei der Konvertierung zum Typ boolean gelten die folgenden Werte als FALSE:
- boolean FALSE selbst
- integer 0 (zero)
- float 0.0 (zero)
- Der leere string, und der string "0"
- Ein array ohne Elemente
- Ein object ohne Eigenschaftsvariablen (nur PHP 4)
- Der spezielle Typ NULL (inklusive nicht gesetzter Variablen)
- SimpleXML Objekte die aus leeren Tags erzeugt wurden.
Jeder andere Wert wird als TRUE angenommen (inklusive jeglicher resource Werte).
-1 gilt als TRUE wie jeder andere Integerwert ungleich 0 (egal ob positiv oder negativ)!
<?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)
?>
Booleans
13-Jul-2007 05:22
23-May-2007 07:03
Re: andy at txtnation dot com
<quote> The braces are of course optional </quote>
Nothing optional about the 'braces' here.
'( )' are parentheses. '{ }' are braces. But we get the point.
<?php
$num = 10;
$isEven = !($num % 2);
echo ($isEven) ? 'Even' : 'Odd';
//outputs : Even
$isEven = !$num % 2;
echo ($isEven) ? 'Even' : 'Odd';
//outputs : Odd (with ANY number != 0 !!)
?>
Operator precedence and implicit casts at work:
$num = 10;
!$num => (implicit cast to bool) $num: (bool) 10 = true
!true => negate true : false
false % 2 => (implicit cast to int) false : (int) false = 0
0 % 2 => remainder of 0 intdiv 2 : 0
$isEven = 0 => integer assignment : 0
($isEven) ? => (implicit cast to bool) 0 : (bool) 0 = false
echo (false) ? 'Even' : 'Odd' => condition false : 'Odd'
Wether or not PHP actually performs the (bool) casts under the hood is irrelevant to the outcome here.
29-Apr-2007 11:21
Beware that "0.00" converts to boolean TRUE !
You may get such a string from your database, if you have columns of type DECIMAL or CURRENCY. In such cases you have to explicitly check if the value is != 0 or to explicitly convert the value to int also, not only to boolean.
Jasper probably meant:
$a = 2;
$b = 3;
$aBiggerThanB = $a > $b;
25-Feb-2007 06:31
Re: comment from jasper at jtey dot com
It is better to not explicitly test for default values. PHP knows the default values, and so should any programmer worth her/his salt.
Same example rewritten:
<?php
$num = 10;
$isEven = !($num % 2);
?>
The braces are off course optional.
05-Jun-2006 09:51
The following expressions are equivalent:
<?php
// setting true
$flag = true;
$flag = True;
$flag = TRUE;
$flag = 1==1;
// setting false
$flag = false;
$flag = False;
$flag = FALSE;
$flag = 1==2;
?>
The moral of the story is that boolean operators return a boolean value, i.e., "1==1" returns a boolean value of true. Someone who is not aware of this may write a block of code such as:
<?php
// even number?
$num = 10;
if($num % 2 == 0){
$isEven = true;
}
else{
$isEven = false;
}
?>
when all that is needed is:
<?php
$num = 10;
$isEven = $num % 2 == 0;
?>
Other examples, for illustrative purposes:
<?php
// two numbers
$a = 2;
$b = 3;
$aBiggerThanB = 2 > 3; // $aBiggerThanB is set to false
// lower case vowel check (corrected)
$c = "u";
$isVowel = $c == "a"|| $c == "e"|| $c == "i"|| $c == "o"|| $c == "u";
?>