dimanche 30 mai 2021

Learning PHP basic if statement comparer struggles

I'm attempting to learn PHP and am currently struggling with a basic comparer I made with if statements.

I can't reliably get $teststype to display "mix" if the $test1 and $test2 are both a and b. However if I make one of them something random like "ball" then it works but in that case $teststype should execute the 0 case and display " ", but it doesn't. Echoing $test1_type and $test2_type and the gettype were just part of my trouble shooting.

Any help in explaining my errors and this behavior would be greatly appreciated.

Thanks

my code: http://sandbox.onlinephpfunctions.com/code/723d64346a12816e0db66b4af64a87253a31abe1

<?php
 
 
 $test1 = "a";
 $test2 = "b";

 
 
 if         ( $test1 === "a" ) {
                $test1_type = 1;
            } elseif ($test1 === "b") {
                $test1_type = 2;
            } else {
                $test1_type = 0;
            }

            if ($test2 === "a") {
                $test2_type = 1;
            } elseif ($test2 === "b") {
                $test2_type = 2;
            } else {
                $test2_type = 0;
            }
            
        
            if ($test2_type && $test1_type === 1) {
                $teststype = "a";
            } elseif ($test2_type && $test1_type === 2) {
                $teststype = "b";
            } elseif ($test2_type !== $test1_type) {
                $teststype = "mix";
            } elseif ($test2_type || $test1_type === 0) {
                $teststype = " ";
            }
            
             $type = gettype($test2_type);
            
            
            echo "type = $type  |||  test1 = $test1_type  ||  test 2= $test2_type  |||  evaluated = $teststype";
            
?>

Aucun commentaire:

Enregistrer un commentaire