Parse error given when checking variables with the Empty() function

Asked

Viewed 495 times

9

I have a little problem in my PHP when I step over a variable to check separated by 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();
  }
}

Makes that mistake:

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

Am I the one making the syntax wrong? When I put only a normal wheel.

  • I believe that the empty() does not support variable variables such as isset().

2 answers

10


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

An idea is to create a function that takes multiple arguments that for each received argument 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 the Stackoverflow imkingdavid user solution in this answer.


The alternative is to validate variable to variable:

if (!empty($ordem) && !empty($visao)) {
 ...
}
  • Vlw the best answer... Thank you!

8

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

bool empty ( mixed $var )

This Mixed means it accepts various data types for the parameter $var.

There are some examples of how to make derived functions to check multiple items at once. Maybe you don’t have exactly what you want, but it’s very easy to make a function that accepts several parameters and check that all are empty. But that’s another problem.

You must change the if for:

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

I put in the Github for future reference.

  • Thank you for the force!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.