php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #25734 string false type-casts into boolean TRUE
Submitted: 2003-10-02 12:57 UTC Modified: 2003-10-02 16:03 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: rbotzer at yahoo dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.3.3 OS: MacOS X
Private report: No CVE-ID: None
 [2003-10-02 12:57 UTC] rbotzer at yahoo dot com
Description:
------------
When I type-cast the string "false" into a boolean it evaluates to TRUE.  This is non-sensical.  Can we please change the type-casting so that it evaluates as FALSE.


Thanks,
Ronen Botzer

Reproduce code:
---------------
<?php
// start example:
$x = "false";

if ($x === "false") {
  print ("the string value of x is evaluating as \"false\" \n");
  var_dump ($x);
  print ("\n");
}

$y = (boolean) $x;
print ("\$x was type-cast into boolean \$y\n\n");
if ($y) {
  print ("the boolean value of x is evaluating as true\n");
  var_dump ($y);
}
// end example
?>

Expected result:
----------------
I expect to only see a printout from the first conditional.  The second should evaluate as false, and not print.

Actual result:
--------------
the string value of x is evaluating as "false" 
string(5) "false"

$x was type-cast into boolean $y

the boolean value of x is evaluating as true
bool(true)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-10-02 13:08 UTC] elmicha@php.net
Bad idea - think about backwards compatibility.

And what would be next? Should "no", "off", "nyet", "falsch" and a bunch of other strings also evaluate to false?
 [2003-10-02 15:46 UTC] rbotzer at yahoo dot com
First, backwards compatability actually will still be maintained.  An extended list of FALSE type-casts will support the subset of previous versions.  For example, if a new String function is added in 4.3.4, that doesn't mean that 4.3.4 doesn't support the subset of String functions from 4.3.3.  Therefore, code that was developed before will not depend on the new type-casting, and backwards compatability will be maintained.

Second, I am not talking about the noun 'false', so comparisons to 'nyet' aren't relevant.  I'm talking about type casting the string 'false' to the boolean FALSE.  Let's not claim that PHP is strongly typed, since it isn't.  Due to the fact that it is a loosely typed scripting language, the scenario should make sense:

assume isValidUser is an RPC that uses SOAP and recieves strings "false" or "true" - where these are actually boolean values.  Then the line:

if ((boolean) $this->isValidUser())

should not evaluate to true if the method returned "false".  It should cast into FALSE.

I'm just suggesting a common sense thing here - treating (boolean)"false" as the boolean value FALSE.  I'm not talking about all the nouns for 'false' in all languages.  Consider that (boolean)0 === FALSE is just as arbitrary, and much less clear than (boolean)"false" being FALSE rather than TRUE.
 [2003-10-02 16:03 UTC] pollita@php.net
This FR will not be done.  Accept that.

It *IS* BC breaking because some scripts rely on ANY non-zero valued / non-zero length string evaluating to true regardless of textual content.

Your isValidUser method should be modified to return proper boolean values rather than text strings.  If nothing else, you can write a simple wrapper to satisfy your own personal needs very easily:

function my_boolean($val) {
  if (strtolower(trim($val)) === 'false') {
    return false;
  } else {
    return (boolean)$val;
  }
}

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 23:01:33 2025 UTC