fork download
  1. <?php
  2.  
  3. function valida_variavel_zero_um(int $valor)
  4. {
  5. if ($valor != 0 && $valor != 1)
  6. {
  7. return "inválido";
  8. }
  9.  
  10. return $valor;
  11. }
  12.  
  13. $tests = [0, 1, false, true, "0", "1", [], "", 0.0, -0.0, 1.0];
  14.  
  15. foreach ($tests as $test)
  16. {
  17. try {
  18. var_dump( valida_variavel_zero_um($test) );
  19. } catch (TypeError $e) {
  20. echo $e->getMessage(), PHP_EOL;
  21. }
  22. }
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
int(0)
int(1)
int(0)
int(1)
int(0)
int(1)
Argument 1 passed to valida_variavel_zero_um() must be of the type integer, array given, called in /home/tzgjpG/prog.php on line 18
Argument 1 passed to valida_variavel_zero_um() must be of the type integer, string given, called in /home/tzgjpG/prog.php on line 18
int(0)
int(0)
int(1)