PHP if multiple variables exist

筅森魡賤 提交于 2019-12-12 07:51:57

问题


So i'm having a bit of a problem with having my PHP run a command if multiple variables exist. I made a simple version for people to see what i'm trying to fix easier. Thanks in advance to anyone who can help :)

<?php
if ((isset($finalusername)) && if (isset($finalpassword)) && if (isset($finalemail)))
  echo "This will save.";
?>

回答1:


if (isset($finalusername, $finalpassword, $finalemail))

Also see The Definitive Guide to PHP's isset and empty.




回答2:


You don't need to place the multiple if in there.

if (isset($finalusername) && isset($finalpassword) && isset($finalemail)) {
   // ...
}

In fact, I'd even do it like so...

if (isset($finalusername, $finalpassword, $finalemail)) {
   // ...
}



回答3:


If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set.

So you can do this way:

if (isset($finalusername, $finalpassword, $finalemail)) {
    echo "This will save.";
}



回答4:


So this might be a good solution for much more than some variables:

  // INTI ARRAY
  $arr_final = array(
    'username'  => !empty($finalusername) ? $finalusername : false,
    'password'  => !empty($finalpassword) ? $finalpassword : false,
    'email'  => !empty($finalemail) ? $finalemail : false,
    'more_stuff'  => !empty($morestuff) ? $morestuff : false,
  );

  $bol_isValid = true;
  $arr_required = array('username','password');
  foreach ($arr_final as $key => $value) {
    if ( (in_array($key, $arr_required)) && ($value === false) ) {
      $bol_isValid = false;
      break;
    }
  }



回答5:


Try with this:

if (isset($var1))
{
    if (isset($var2))
    {
        // continue for each variables...
    }
}


来源:https://stackoverflow.com/questions/8997485/php-if-multiple-variables-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!