PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

is_buffer> <is_binary
Last updated: Fri, 11 Apr 2008

view this page in

is_bool

(PHP 4, PHP 5)

is_bool — Détermine si une variable est un booléen

Description

bool is_bool ( mixed $var )

is_bool() détermine si la variable donnée est un booléen.

Liste de paramètres

var

La variable à évaluer.

Valeurs de retour

Retourne TRUE si var est un booléen, FALSE sinon.

Exemples

Exemple #1 Exemple avec is_bool()

<?php
$a 
false;
$b 0;

// Si $a est un booléen, ceci sera vrai
if (is_bool($a)) {
    echo 
"Oui, c'est un booléen.";
}

// Si $b n'est pas un booléen, ceci sera faux
if (is_bool($b)) {
    echo 
"Oui, c'est un booléen.";
}
?>



is_buffer> <is_binary
Last updated: Fri, 11 Apr 2008
 
add a note add a note User Contributed Notes
is_bool
bb at kingdom dot cjb dot net
27-Jun-2007 11:57
@ emmanuel del grande

You don't need to break when you return...

function bool($var) {
    switch (strtolower($var)) {
        case ("true"): return true;
        case ("false"): return false;
        default: die("whatever you want it to tell");
    }
}
kevin at metalaxe dot com
05-Jun-2007 10:32
@ emanueledelgrande at email dot it

http://us.php.net/manual/en/language.types.type-juggling.php

(bool) or (boolean) is allowed for type casting a variable. No function like intval, etc but the functionality exists.
emanueledelgrande at email dot it
22-May-2007 06:12
I think there's a hole in the PHP typecasting methods:
you have the (int) function, the (float) function and the (string) function, but no function to force a string variable into the boolean type.

It's obvious that forcing unconditionally the type of variables into arrays and objects is inappropriate, but boolean type is the most basic one for each programming language, that's why I guessed that a (bool) function already existed.

Moreover, with the increasing trend of RSS data streaming, the parsing of an XML string into an object often requires to typecast as boolean values the content of XML tags, normally returned as string by the object method get_content().

I wrote the following function, which also uses a "native PHP style" error message:

<?php
// strings tyecasting as boolean values:
function bool($var) {
    switch (
strtolower($var)) {
        case (
"true"):
            return
true;
            break;
        case (
"false"):
            return
false;
            break;
        default:
            die(
"<br />\n<b>Warning:</b> Invalid argument supplied for ".__FUNCTION__." function in <b>".__FILE__."</b> on line <b>".__LINE__."</b>: the argument can contain only 'true' or 'false' values as a string.<br />\n");
    }
}
?>

Here it is a small example:

<?php
$xmlResponse
= "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xmlResponse .= "<Result>";
$xmlResponse .= "<AuthError>false</AuthError>";
$xmlResponse .= "<TransferStatus>true</TransferStatus>";
$xmlResponse .= "</Result>";

if (!
$responseDoc = domxml_open_mem($xmlResponse, DOMXML_LOAD_PARSING, $XmlParsingError)) {
    echo
"Error while parsing the XML string:<br />".print_r($XmlParsingError, TRUE);
} else {
   
$ResultNode = $responseDoc->get_elements_by_tagname('Result');
   
   
$AuthError = $ResultNode[0]->get_elements_by_tagname('AuthError');
   
$auth_error = bool($AuthError[0]->get_content());
   
   
$TransferStatus = $ResultNode[0]->get_elements_by_tagname('TransferStatus');
   
$transfer_status = bool($TransferStatus[0]->get_content());
   
    if (!
$auth_error) { echo "Auth OK<br />"; } else { echo "Auth error<br />"; }
    if (
$transfer_status) { echo "Transfer OK<br />"; } else { echo "Transfer error<br />"; }
}

?>

It would be useful this function to be implemented in the core of PHP5.
21-Apr-2006 01:12
Small caveat to rh's post: back in PHP 3, "0" would be considered non-empty (i.e., empty would return false), even though (bool) on "0" would also evaluate to false; thus, they would not be complete opposites for someone using PHP 3.
rh at richardhoward dot net
22-May-2005 08:17
punkpuke is wrong here; what he means to say is that empty($x) is the opposite of (bool)$x.  is_bool($x) returns true where $x === false.

is_buffer> <is_binary
Last updated: Fri, 11 Apr 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites