Last active
August 29, 2015 14:24
-
-
Save jhonata-menezes-forseti/e233a7cb746837b355e6 to your computer and use it in GitHub Desktop.
trabalhando com parâmetros no PHP CLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* contar numero de x a y | |
* utilizando operador ternario, é um IF que verifica a condição true ou false | |
* isset verifica se existe a variavel na memoria | |
*/ | |
$x = isset($argv[1]) ? $argv[1] : 0;//$argv é do php, ele fornece os valores passados para ao script/programa | |
$y = isset($argv[2]) ? $argv[2] : 10;//acessa o segundo parametro | |
/* | |
* validando argumentos, quero que os mesmos sejam valores inteiros | |
*/ | |
if(!preg_match('#\d+#', $x) || !preg_match('#\d+#', $y) ){ | |
throw new \InvalidArgumentException('argumentos precisam ser numerico/inteiro'); | |
} | |
/* | |
* verifica se os valor y (segundo parametro) é maior que x (primeiro parametro) | |
*/ | |
if($y < $x){ | |
throw new \InvalidArgumentException('Segundo parametro maior que o primeiro'); | |
} | |
/* | |
* comeca o loop de x a y | |
*/ | |
while($x <= $y){ | |
printf("Numero: %s\n", $x); | |
$x++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment