Re: "My truth comes out" [1]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Quadling wrote:
On 21 October 2010 12:42, Richard Quadling <rquadling@xxxxxxxxx> wrote:
On 21 October 2010 10:39, Gary <php-general@xxxxxxxxxxxxxxx> wrote:
Is there any nice way to convert a string containing either "TRUE" or
"FALSE" to a bool with the appropriate value? I know I can just "if
(strcmp..." but, as the song goes on to say "...ugly, so ugly, it's ugly"[1]
What is the response when the string is NOT "TRUE" or "FALSE"?

<?php
$s_OriginalValue = 'FALSE';
$b_Test = in_array($s_Test = strtolower($s_OriginalValue),
array('true', 'false')) ? !!strcmp($s_Test, 'false') : null;
var_dump($b_Test);

$s_OriginalValue = 'TRUE';
$b_Test = in_array($s_Test = strtolower($s_OriginalValue),
array('true', 'false')) ? !!strcmp($s_Test, 'false') : null;
var_dump($b_Test);


$s_OriginalValue = 'GARBAGE';
$b_Test = in_array($s_Test = strtolower($s_OriginalValue),
array('true', 'false')) ? !!strcmp($s_Test, 'false') : null;
var_dump($b_Test);
?>

outputs ...

bool(false)
bool(true)
NULL

This is one of those threads where you can go mad with proposals, thus:

$value = constant($value);

just so happens that constant() is case insensitive and there are constants "true" and "false"

<?php
var_dump( constant("false") );
var_dump( constant("FALSE") );
var_dump( constant("true") );
var_dump( constant("TRUE") );

//output
bool(false)
bool(false)
bool(true)
bool(true)


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux