Jump to content

Php Logic Problems

- - - - -

  • Please log in to reply
2 replies to this topic

#1
braunshedd

braunshedd

    Newbie

  • Members
  • Pip
  • 5 posts
Ok, I've created a piece of code that reads information from a json formatted file, and should respond differently with each "status code". Everything works, but one statement always runs even when it doesn't seem to be true. The statement is marked with asterisks.

        <?php

        $file = file_get_contents("/system/status"); //Get status file

        $output = json_decode($file); //Decode it into variables

        $render1 = 0; //Declare variables

        $render2 = 0;

        

        if ($output->render1 & $output->render2) //If variable not available, do not replace

        {

          $render1 = $output->render1;

          $render2 = $output->render2;

        }

        

        if ($render1 || $render2 != 1 || 2 || 3) //If value is zero, item is corrupt

        {

            echo "Status file is corrupt or unavailable"; ***** This always runs, no matter 

            *******what the actual value

        }

        

        if ($render1 == 1)

        {

            echo "Render 1 is up and running";

        }

        

        if ($render2 == 1)

        {

            echo "Render 2 is up and running";

        }

        

        if ($render1 == 2)

        {

            echo "Render 1 is offline";

        }

        

        if ($render2 == 2)

        {

            echo "Render 2 is offline";

        }

        

        if ($render1 == 3)

        {

            echo "Render 1 is offline for scheduled maintinance";

        }

        

        if ($render2 == 3)

        {

            echo "Render 2 is offline for scheduled maintinance";

        }

        ?>

   
If the $render1 and $render2 variables are 1, my program would output
Status file is corrupt or unavailable Render 1 is up and running Render 2 is up and running
Any ideas?

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You need to understand that each boolean statement must return true or false, and any number above zero is true.

a == b || 2 || 3 will always evaluate to true, as two and three are above zero.

You must do the (I somewhat agree hard to write) following:
$render1 != 1 || $render1 != 2 || $render1 != 3 || $render2 != 1 || $render2 != 2 || $render2 != 3
Although I might suspect you wish to check for both, you could always use an array as a shortcut (to store like values)
(PHP: in_array - Manual)

$valid_responses = array(1, 2, 3);
if(!in_array($render1, $valid_responses) || !in_array($render2, $valid_responses)) {
   //...
}
Feel free to look on to the PHP manual for information on how boolean operators and statements work in PHP: PHP: Booleans - Manual
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
braunshedd

braunshedd

    Newbie

  • Members
  • Pip
  • 5 posts
Thank you!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users