note 103757 deleted from language.types.boolean by cmb
| From: | cmb@php.net | Date: | Fri, 15 May 2015 18:55:29 +0000 |
| Subject: | note 103757 deleted from language.types.boolean by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-202537@lists.php.net to get a copy of this message | ||
Note Submitter: frank at interactinet dot com
----
Be careful when assigning a value in the if statement, for example:
if($var = $arg)
$var might be assigned "1" instead of the expected value in $arg.
<?php
public function myMethod()
{
return 'test';
}
public function myOtherMethod()
{
return null;
}
if($val = $this->myMethod())
{
// $val might be 1 instead of the expected 'test'
}
if( ($val = $this->myMethod()) )
{
// now $val should be 'test'
}
// or to check for false
if( !($val = $this->myMethod()) )
{
// this will not run since $val = 'test' and equates to true
}
// this is an easy way to assign default value only if a value is not returned:
if( !($val = $this->myOtherMethod()) )
{
$val = 'default'
}
?>