Created
April 14, 2022 15:46
-
-
Save othonet/b17b2280122ee94c84dc69f6aa861303 to your computer and use it in GitHub Desktop.
PHP - Validate number of function parameters
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 | |
function contaParamentros(){ | |
$numArgs = func_num_args(); | |
if($numArgs > 3){ | |
echo "<strong style='color: orange;'>Ops, você passou ".$numArgs." parâmetros. O número máximo permitido são 3 parâmetros.</strong>"; | |
}elseif($numArgs < 1){ | |
echo "<strong style='color: red;'>Ops, esta função precisa de, pelo menos, um argumento.</strong>"; | |
}else{ | |
echo "<div style='color: green; font-weight: bold;'>"; | |
echo "Muito bem, agora a função está perfeita!"; | |
echo "<br>"; | |
echo "Parâmetro 1: ". func_get_arg(0) . "<br>"; | |
echo "Parâmetro 2: ". func_get_arg(1) . "<br>"; | |
echo "Parâmetro 3: ". func_get_arg(2) . "<br>"; | |
echo "</div>"; | |
} | |
} | |
contaParamentros('Param1', array('1', 2), 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment