guy
guy

Reputation: 40

php wont check the second argument even if the first is true

I wrote this:

$a[] = "guy";
$b[] = "g";

function login($a1, $b1)
{
  if( user($a1) == true and pass1($b1) == true)
  {
    login2($a1, $b1);
  }
  else
  { 
    echo "error!!!!";
  }
}

function login2($a1, $b1)
{
  if (array_search($_REQUEST["user"],$a1) == array_search($_REQUEST["pass"],$b1))
  {
    echo "you are logged in";
  }
  else
  {
    echo "erorr";
  }
}

function user($user1)
{   
  if(in_array($_REQUEST["user"],$user1))
  {
    echo "gooooooood?";
  }
}

function pass1($pas)
{
  if(in_array($_REQUEST["pass"],$pas))
  {
    echo "goooooooood!!!!!!!!";
  }
  else
  {
    echo "bad";
  }
}

login($a, $b);      

and I know that pass() and user() are true because I changed their positions on the function login() and every time I did this the first argument was returned as true and it didn't check the second one. Does anyone know why this happens?

Upvotes: 1

Views: 131

Answers (3)

Bobulous
Bobulous

Reputation: 13169

Your user and pass1 functions are not returning an explicit value, so they are implicitly returning the NULL value. As described on this page in the PHP manual the NULL type is converted to false when a boolean is expected. So both your user and pass1 functions return false every time.

The && and and logical operators in PHP use short-circuiting for efficiency (see the first code example on this page of the PHP manual) so in any and statement whose first operand evaluates to false it can never be possible for the whole and statement to evaluate to true, so the second operand (in the case of your code above, the second operand is the call to pass1($b1)) is never evaluated because it would be a waste of time to do so.

Which means you're seeing the user function being called, but never the pass1 function.

Upvotes: 0

user2533777
user2533777

Reputation:

Try using this instead:

$a[] = "guy";
$b[] = "g";

function login($a1, $b1)
{
    if( user($a1) == true && pass1($b1) == true)
        login2($a1, $b1);
    else 
    echo "error!!!!";
}

function login2($a1, $b1)
{
    if (array_search($_REQUEST["user"],$a1) == array_search($_REQUEST["pass"],$b1))
        echo "you are logged in";
    else
        echo "erorr";
}

function user($user1)
{

    if(in_array($_REQUEST["user"],$user1))
        echo "gooooooood?";
}


function pass1($pas)
{
    if(in_array($_REQUEST["pass"],$pas))
        echo"goooooooood!!!!!!!!";
    else
        echo "bad";
}
login($a, $b);

Upvotes: 0

Abhishek Kannan
Abhishek Kannan

Reputation: 988

user and pass1 functions should return true or false, not echo out.

Upvotes: 3

Related Questions