Parse error given when checking variables with empty () function

9

I have a problem with my PHP when I pass more than one variable for it to check separated by a comma it generates a parse error.

Look at my code below:

if (isset($_POST['btnCriaTempo'])){

  $ordem         = trim($_POST['ordem']);
  $timeHours     = trim($_POST['tempoHora']);
  $timeMinutes   = trim($_POST['tempoMinuto']);
  $timeSeconds   = trim($_POST['tempoSegundo']);
  $visao         = trim($_POST['visibilidade']);
  $momentHours   = trim($_POST['momentoHora']);
  $momentMinutes = trim($_POST['momentoMinuto']);
  $momentSeconds = trim($_POST['momentoSegundo']);

  if (!empty($ordem, $visao)){ /*Esta é a linha 41*/

    $sqlCriaTempo = "INSERT INTO sv_tempo (tempoTotal, tempoVisao, tempoMomento, tempoOrdem)"
                    ." values (:tempoTotal, :tempoVisao, :tempoMomento, :tempoOrdem)";

    $queryCriaTempo = $conecta->prepare();
  }
}

Give this error:

Parse error: syntax error, unexpected ',', expecting ')'
in C:\xampp\htdocs\newcemp\admin\create\tempo.php on line 41

Am I wrong with the syntax myself? When I put only a normal wheel.

    
asked by anonymous 10.01.2014 / 00:14

2 answers

10

Your problem is that the empty() function only accepts one argument, ie , you can only check one variable at a time.

An idea is to create a function that receives multiple arguments that for every argument received will call the function empty() and perform the verification:

function mempty() {

  foreach (func_get_args() as $arg) {

    if (empty($arg)) {
      continue;
    } else {
      return false;
    }
  }
  return true;
}

Credits for StackOverflow's user-defined solution imkingdavid this answer .

The alternative is to validate the variable variable:

if (!empty($ordem) && !empty($visao)) {
 ...
}
    
10.01.2014 / 00:27
8

Yes, the empty function only accepts one parameter. See the empty() handbook on the official website.

bool empty ( mixed $var )

This mixed means that it accepts multiple data types for the $var parameter.

There are some examples of how to do derived functions to check multiple items at the same time. It may not have exactly what you want, but it's pretty easy to do a function that accepts multiple parameters and make sure all are empty. But that's another problem.

You should change if to:

if (!empty($ordem) && !empty($visao)) {

I put it in Github for future reference .

    
10.01.2014 / 00:27