Skip to content

Instantly share code, notes, and snippets.

@othonet
Created April 14, 2022 15:46
Show Gist options
  • Save othonet/b17b2280122ee94c84dc69f6aa861303 to your computer and use it in GitHub Desktop.
Save othonet/b17b2280122ee94c84dc69f6aa861303 to your computer and use it in GitHub Desktop.
PHP - Validate number of function parameters
<?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