[sr-dev] another syntax error

Juha Heinanen jh at tutpro.com
Wed Jun 3 20:11:47 CEST 2009


Andrei Pelinescu-Onciul writes:

 > > $var = '';
 > > 
 > > // This will evaluate to TRUE so the text will be printed.
 > > if (isset($var)) {
 > >     echo "This var is set so I will print.";
 > > }
 > 
 > This will evaluate to TRUE also in sip-router (if (defined($var))).
 > The question is how is if ($foo == "") evaluated when $foo is
 > undefined in php.

fine if defined($var) on $var = "" is true.

 > Why would you want sip-router to behave differently then other scripting
 > languages?

i don't want that.  in php

unset($foo);
if ($foo == "") {
  echo "yes\n";
 } else {
  echo "no\n";
 }

prints "yes", but

unset($foo);
if ($foo === "") {
  echo "yes\n";
 } else {
  echo "no\n";
 }

prints "no".

=== operator thus allows me to tell, if a var is undefined or assigned
"" value.  

== is not good for testing if variable is defined, which prompted this
discussion:

!defined $fU  fastest
$fU == "" - extra string conversion
$fU == $null slowest (has to go through a pv call and string or integer
                      conversion)

so the middle one is not ok for that purpose, but it doesn't need to be
because defined() exists.

-- juha



More information about the sr-dev mailing list