Jump to content

If Statement Behavior


shane18

Recommended Posts

<?
$A = 2;
$B = 5;

if($A = 22 && $B = 54){
echo $A . "-" . $B . "<br>";
echo "PASS";
}else{
echo $A . "-" . $B . "<br>";
echo "FAIL";
}
?>

 

Why does it convert the $A to its Boolean value... but leaves $B alone? it always leaves the last one in the statement alone.... anyone know why?

Link to comment
Share on other sites

I told you that you are assigning not comparing. If  you wanted to use functions in an IF. look:

<?php
$a = 1;
$b = 2;
function foo(){
    return 1;
}
function bar(){
    return 2;
}

if ($a == foo() && $b == bar()) {
  echo "It works!" ; 
}
?>

 

And it should work as expected. See how I used ==?

Link to comment
Share on other sites

im trying to assign a variable... and have it checked... i know that to compare A with B u go A == B...

 

i wana be able to assign a function to a variable... then have it checked to be TRUE or FALSE like if($TEST = BLAH()){

 

be sides.... why does it convert every variable except for the last one... ? it'll return like 1 1 1 1 64... if i assign 5 variables in the if statement

Link to comment
Share on other sites

im trying to assign a variable... and have it checked... i know that to compare A with B u go A == B...

 

i wana be able to assign a function to a variable... then have it checked to be TRUE or FALSE like if($TEST = BLAH()){

 

be sides.... why does it convert every variable except for the last one... ? it'll return like 1 1 1 1 64... if i assign 5 variables in the if statement

What on earth are you trying to do?

if($A = 22 && $B = 54){
echo $A . "-" . $B . "<br>";

 

This will always be true. You can do

if ($a == foo()) {
   $a = pass;
}

 

An IF statement is not for assigning.

Link to comment
Share on other sites

Well, i just wondering why if you do....

 

if($A = A() && $B = B())

 

Lets say... each function can return 0-2

A returns 2... and b returns 2... for example...

 

why after the if statement does A = 1 and B = 2..... why does it only convert the first variable to its boolean value

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.