Here is the code:

public function townUse($town) { $town = strtoupper($town); if( array_key_exists($town,$_SESSION['towns']) ){ if($_SESSION['towns'][$town] == 1){ return false; }else{ $_SESSION['towns'][$town] = 1; var_dump($_SESSION['towns']); return true; } }else{ return false; } } 

$ town - any city

When $ town = 1 (var_dump to help), the function still returns true, why?

PS $ _SESSION ['towns'] I filled with another function, so that it is not empty

  • var_dump($_SESSION['towns']) to the studio - ArchDemon

1 answer 1

Check the data you transfer to this method. I changed it a bit to be able to run on LAN - it works fine:

 $town = 1; $_SESSION['towns'] = ['Москва', 'Питер']; /*public */function townUse($town) { $town = strtoupper($town); if ( array_key_exists($town,$_SESSION['towns']) ) { if ($_SESSION['towns'][$town] == 1) { return false; } else { $_SESSION['towns'][$town] = 1; return true; } } else { return false; } } echo '<pre>'; var_dump(townUse($town)); echo '</pre>'; /* bool(true) */