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
https://forums.phpfreaks.com/topic/185830-if-statement-behavior/
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 ==?

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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.