Is this a PHP bug


<?php

    function test() {
    	return '1';
    }
    
    //case 1 output: no
    if($a = test() && $a == 1) {
    	echo 'yes';
    }
    else {
        echo 'no';
    }

    //case 2 output: yes
    if($b = test()) {
        if($b == 1) {
     	    echo 'yes';
        }
    }
    else {
        echo 'no';
    }

?>

case 1 and 2 are the same logic, but they have different output, is this a PHP bug or am I missing something

Thanks

= is not the same as ==

= assigns value
== compares value

Can you assign a value in a conditional statement?

Case 1 and case 2 are not the same logic.

Can you assign a value in a conditional statement?

Yes.

Thanks for the reply, I figured out why, it’s the operator precedence issue:

the code should be if(($a = test()) && $a == 1)

if ( ( $a = test() ) == 1 )

Arithmetic statements that do not evaluate to 0 are treated as TRUE. Assignment statements that do not cause a catastrophic error (which… would be rare-if-ever) are treated as TRUE.

The general rule of thumb with IF is: If it’s anything other than == 0, or === FALSE, then it’s TRUE.