Jump to content

Logic Problems. Again :)

- - - - -

  • Please log in to reply
3 replies to this topic

#1
braunshedd

braunshedd

    Newbie

  • Members
  • Pip
  • 5 posts
I'm back!
Ok, this problem seems to evade me.
When I run this code, it works great, until the render1 variable equals 1 and the render2 variable equals 2, or vice versa.
I am lost as to what could be causing the problem, as any other combination of numbers works fine.
Thanks for helping me out. :)
<?php

        $status_codes = array(1, 2, 3);

        $render1 = 0;

        $render2 = 0;

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

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


        if ($output->render1 & $output->render2) { //If variable is available, decode. Otherwise, give up

	   $render1 = $output->render1;

	   $render2 = $output->render2;

        }


        if (in_array($render1, $status_codes) || in_array($render2, $status_codes)) { //If variables are legit, contiune

	   if ($render1 == 1) {

	       echo "Render 1 is up and running" . "<br>";

	   }


	   if ($render1 == 2) {

	       echo "Render 1 is offline" . "<br>";

	   }


	   if ($render1 == 3) {

	       echo "Render 1 is offline for scheduled maintinance" . "<br>";

	   }


	   if ($render2 == 1) {

	       echo "Render 2 is up and running";

	   }


	   if ($render2 == 2) {

	       echo "Render 2 is offline";

	   }


	   if ($render2 == 3) {

	       echo "Render 2 is offline for scheduled maintinance";

	   }

        }

        else {

	   echo "Status file is corrupt or unavailable";

        }

        ?>


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
These surrounding lines of code disconcert me:
  if ($output->render1 & $output->render2) { //If variable is available, decode. Otherwise, give up
       $render1 = $output->render1;
       $render2 = $output->render2;
  }
I am sure you meant something else in your if statement, the & operator is bitwise AND, of which likely has nothing to do with your script and will randomly prevent the $render1 and $render2 variables to not be assigned. Two can be represented as 10 in binary, and 1 as 01, so 2&1 and 1&2 = 0 as there are no shared bits in that bitset.

If you use plain &&, it will check if the number is above zero (and thus true), I would recommend however you use isset() for safety reasons:
if (isset($output->render1) && isset($output->render2))
You can always do this to make things look more clearer and reduce unneeded code
if (isset($output->render1) && isset($output->render2)) {
   switch($output->render1) { 
      case 1: echo "..."; break;
      case 2: echo "..."; break;
      case 3: echo "..."; break;
   }
   switch($output->render2) { 
      case 1: echo "..."; break;
      case 2: echo "..."; break;
      case 3: echo "..."; break;
   }
} else {
   //error
}
A switch can be a method to remove needless plain IF statements, although my style of placing case, statement, then break on one line is just me, each can be on separate lines.
PHP: switch - 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
Thanks for all your help :)
I'm still getting used to php, as I am native to c++.
Interesting though, I never knew "&" was bitwise. I thought....
Nevermind :p

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
& is bitwise in C/C++ as well, as PHP has borrowed (And is based on) C.

Feel free to browse PHP's operators to see what you already recognize:
PHP: Operators - 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.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users