9

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.";
?>
4
  • 1
    you do not have any code posted Commented Jan 25, 2012 at 3:32
  • Without code, we can't do anything! I posted a basic answer with a code that I use for this purpose... Commented Jan 25, 2012 at 3:38
  • <?php if ((isset($finalusername)) && if (isset($finalpassword)) && if (isset($finalemail))) echo "This will save."; ?> Commented Jan 25, 2012 at 3:45
  • What's the problem with the given code? Commented Mar 30, 2021 at 11:01

5 Answers 5

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

@alex When something annoys me enough and I have some spare time on my hands. :o)
I want everyone to know.....im an idiot lol the variable was finalpass and thats why i was having trouble haha, but thanks everyone your codes were way ebtetr than mine :)
Can empty() also receive multiple parameters such as isset()? I just want to be sure. By the way, I've read the article. It's comprehensive! Nice one!
@deceze, thanks. I've just tried and NetBeans gave a syntax error, hehehe. Thanks again!
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)) {
   // ...
}

1 Comment

I want everyone to know.....im an idiot lol the variable was finalpass and thats why i was having trouble haha, but thanks everyone your codes were way ebtetr than mine :)
1

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.";
}

1 Comment

I want everyone to know.....im an idiot lol the variable was finalpass and thats why i was having trouble haha, but thanks everyone your codes were way ebtetr than mine :)
0

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;
    }
  }

Comments

-3

Try with this:

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

1 Comment

I tryed this before and Im not sure why but it isn't working... <php? if (isset($finalusername)) { if (isset($finalpassword)) { echo "please work"; } } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.