Just a side note, doesn't really matters, the reason -1 is true and not false is because boolean type is treated as unsigned, so -1 would be for example, if it's unsigned int32 translate to hex: 0xFFFFFFFF and back to decimal: 4294967295 which is non-zero. there isn't really a "negative boolean". it's a binary thing. :o (since it used to be a bit and then there was only 0 and 1 as an option)
boolean
En basit türdür. Bir mantıksal ifadenin sonucu TRUE veya FALSE olan bir doğruluk değeridir.
Bilginize: boolean türü PHP 4'ten beri vardır.
Sözdizimi
Bir boolean sayılını belirtmek için TRUE veya FALSE anahtar sözcüğü kullanılır. Her ikisi de harf büyüklüğüne duyarsızdır.
<?php
$foo = True; // $foo değişkenine TRUE değeri atanır.
?>
Genelde, boolean türünde bir değer döndüren bir çeşit işleç vardır ve bu değer bir denetim yapısına aktarılır.
<?php
// == işleci, eşitliği sınar ve
// boolean türünde bir değer döndürür
if ($eylem == "sürümü göster") {
echo "Sürüm numarası 1.23'tür.";
}
// Bu gereksizdir...
if ($ayırıcı_göster == TRUE) {
echo "<hr>\n";
}
// ... böylesi daha iyidir:
if ($ayırıcı_göster) {
echo "<hr>\n";
}
?>
boolean türüne dönüşüm
Bir değeri doğrudan boolean türüne dönüştürmek için (bool) veya (boolean) çarpıtmalarını kullanabilirsiniz. Ancak, bir boolean değiştirge gerektiren bir işleç, işlev veya denetim yapısı dönüşümün özdevinimli olarak yapılmasını sağladığından çoğu durumda tür çarpıtma gereksizdir.
Ayrıca, Tür Dönüşümü bölümüne de bakınız.
Aşağıdaki değerler boolean türüne dönüşümde FALSE olarak ele alınırlar:
- boolean FALSE (kendisi)
- integer 0 (sıfır)
- the float 0.0 (sıfır)
- boş string, ve string "0"
- hiç elemanı olmayan array
- hiç üye değişkeni olmayan object (sadece PHP 4)
- NULL türü (tanımsız değişkenler dahil)
- boş etiketlerle oluşturulmuş SimpleXML nesneleri
Bunların dışında kalan tüm değerler TRUE olarak ele alınır (resource türler dahil).
-1 ve sıfırdan farklı diğer değerler (pozitif veya negatif) TRUE olarak ele alınır.
<?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)
?>
boolean
11-Apr-2009 08:35
02-Apr-2009 02:27
PHP is very fussy converting strings to booleans. The only ones it recognizes are '0' or '', everything else evaluates to TRUE, even 'false' and '0.0' are evaluated as true! I suppose this can't be fixed without breaking a lot of existing code.
Example:
<?php
print 'yes'."\t".((bool)'yes'? 1: 0)."\n";
print 'true'."\t".((bool)'true'? 1: 0)."\n";
print 'no'."\t".((bool)'no'? 1: 0)."\n";
print 'false'."\t".((bool)'false'? 1: 0)."\n";
print '1'."\t".((bool)'1'? 1: 0)."\n";
print '0'."\t".((bool)'0'? 1: 0)."\n";
print '0.0'."\t".((bool)'0.0'? 1: 0)."\n";
print ''."\t".((bool)''? 1: 0)."\n";
?>
Output:
yes 1
true 1
no 1
false 1
1 1
0 0
0.0 1
0
26-Feb-2009 03:59
A note when working with PostgreSQL - if you select a boolean field from the database, it returns 't' or 'f'. If you directly evaluate a variable storing a boolean from a PostgreSQL database, it will always return true.
For example...
<?php
$x = pg_query("SELECT someBool FROM atable");
$x = pg_fetch_array($x);
$x = $x['someBool'];
if ($x) echo "true";
else echo "false";
?>
...ALWAYS outputs true
04-Nov-2008 08:27
Beware of certain control behavior with boolean and non boolean values :
<?php
// Consider that the 0 could by any parameters including itself
var_dump(0 == 1); // false
var_dump(0 == (bool)'all'); // false
var_dump(0 == 'all'); // TRUE, take care
var_dump(0 === 'all'); // false
// To avoid this behavior, you need to cast your parameter as string like that :
var_dump((string)0 == 'all'); // false
?>
06-Oct-2008 06:59
CODING PRACTICE...
Much of the confusion about booleans (but not limited to booleans) is the fact that PHP itself automatically makes a type cast or conversion for you, which may NOT be what you want or expect. In most cases, it's better to provide functions that give your program the exact behavior you want.
<?php
function boolNumber($bValue = false) { // returns integer
return ($bValue ? 1 : 0);
}
function boolString($bValue = false) { // returns string
return ($bValue ? 'true' : 'false');
}
$a = true; // boolean value
echo 'boolean $a AS string = ' . boolString($a) . '<br>'; // boolean as a string
echo 'boolean $a AS number = ' . boolNumber($a) . '<br>'; // boolean as a number
echo '<br>';
$b = (45 > 90); // boolean value
echo 'boolean $b AS string = ' . boolString($b) . '<br>'; // boolean as a string
echo 'boolean $b AS number = ' . boolNumber($b) . '<br>'; // boolean as a number
echo '<br>';
$c = boolNumber(10 > 8) + boolNumber(!(5 > 10)); // adding booleans
echo 'integer $c = ' . $c .'<br>';
?>
Results in the following being printed...
boolean $a AS string = true
boolean $a AS number = 1
boolean $b AS string = false
boolean $b AS number = 0
integer $c = 2
In other words, if we know what we want out of our program, we can create functions to accommodate. Here, we just wanted 'manual control' over numbers and strings, so that PHP doesn't confuse us.
27-Jan-2008 02:39
It is correct that TRUE or FALSE should not be used as constants for the numbers 0 and 1. But there may be times when it might be helpful to see the value of the Boolean as a 1 or 0. Here's how to do it.
<?php
$var1 = TRUE;
$var2 = FALSE;
echo $var1; // Will display the number 1
echo $var2; //Will display nothing
/* To get it to display the number 0 for
a false value you have to typecast it: */
echo (int)$var2; //This will display the number 0 for false.
?>
15-Jan-2008 11:00
PHP does not break any rules with the values of true and false. The value false is not a constant for the number 0, it is a boolean value that indicates false. The value true is also not a constant for 1, it is a special boolean value that indicates true. It just happens to cast to integer 1 when you print it or use it in an expression, but it's not the same as a constant for the integer value 1 and you shouldn't use it as one. Notice what it says at the top of the page:
A boolean expresses a truth value.
It does not say "a boolean expresses a 0 or 1".
It's true that symbolic constants are specifically designed to always and only reference their constant value. But booleans are not symbolic constants, they are values. If you're trying to add 2 boolean values you might have other problems in your application.
06-Jan-2008 03:05
Note that the symbolic constants TRUE and FALSE are treated differently. I was told that this is a feature, not a bug.
echo false ;
echo (false) ;
echo false+false ;
echo (false+false) ;
echo intval(false) ;
echo '"'.false.'"' ;
echo true ;
echo (true) ;
echo true+true ;
echo (true+true) ;
echo intval(true) ;
echo '"'.true.'"' ;
should produce
00000"0"11221"1"
but instead produces
000""11221"1"
In other words, the only way to output the underlying zero or use it in a string is to use 'false+false' or pass it through intval(). No such tricks are required to get at the 1 that underlies true.
The whole idea of symbolic constants is that the underlying value *always* replaces them during translation, and thus anywhere you would otherwise have to use some obscure "magic number" such as 191, you can use a symbolic constant that makes sense, such as TOTAL_NATIONS.
Exactly what php gets out of breaking this rule was not explained to me.
27-Sep-2007 04:37
Note you can also use the '!' to convert a number to a boolean, as if it was an explicit (bool) cast then NOT.
So you can do something like:
<?php
$t = !0; // This will === true;
$f = !1; // This will === false;
?>
And non-integers are casted as if to bool, then NOT.
Example:
<?php
$a = !array(); // This will === true;
$a = !array('a'); // This will === false;
$s = !""; // This will === true;
$s = !"hello"; // This will === false;
?>
To cast as if using a (bool) you can NOT the NOT with "!!" (double '!'), then you are casting to the correct (bool).
Example:
<?php
$a = !!array(); // This will === false; (as expected)
/*
This can be a substitute for count($array) > 0 or !(empty($array)) to check to see if an array is empty or not (you would use: !!$array).
*/
$status = (!!$array ? 'complete' : 'incomplete');
$s = !!"testing"; // This will === true; (as expected)
/*
Note: normal casting rules apply so a !!"0" would evaluate to an === false
*/
?>
29-Apr-2007 09: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.
