PHP Loose comparison and null on Switch -
today saw strange bug on code. have switch comparison on case , if variable equals null or '' comes in first case.
my code :
$shost = filter_input(input_server, 'http_host'); switch($shost){ // local case strpos($shost, "dev.localhost") !== false: $this->_senv = 'local'; break; // prod default: $this->_senv = 'production'; break; }
on if statement it's work on switch case doesn't work , don't know why .. maybe php problems ? has had similar bug ?
$shost can null because run script batch.
if(strpos($shost, "dev.localhost") !== false){ // nothing } else{ return false; }
of course can if is_null before switch , want understand why it's working that..
edit: forgot said have 5 cases on switch , 1 case environment
thank future answer :)
if $shost
equals null
or '', first switch case true
because
var_dump($shost == (strpos($shost, "dev.localhost") !== false));
is true. how switch work. can this:
switch(true) { // local case strpos($shost, "dev.localhost") !== false: $this->_senv = 'local'; break; // prod default: $this->_senv = 'production'; break; }
Comments
Post a Comment