Jump to content

Muck A. Round

Members
  • Posts

    10
  • Joined

  • Last visited

Posts posted by Muck A. Round

  1. 4 hours ago, Ingolme said:

    I've tested the code I wrote and it is correctly distinguishing odd and even values of $x. If you've changed the code then I'll have to see what changes you made to find out why it's not working.

    Ah - I didn't make all the changes - putting $value in the static function and if/else.  I had only added $x to the call.   Thanks !!

  2. 18 hours ago, Ingolme said:

    Variables are not global in PHP, so your function does not have access to $x. Because of that, $x is undefined, which is equivalent to 0.

    You should pass the variable into the function if you want to use it, like this:

    <?php
    $x = 11;
    var_dump ($x & 1);
    echo "<br>";
    //
    // var_dump is showing int(0) or int(1) for even or odd but function below is always saying even???
    //
    class calculations {
      public static function OddorEven($value) {
        if ($value & 1) {
         echo "x is odd";}
         else {
         echo "x is even";}
      }
    }
    // Call static method
    calculations::OddorEven($x);
    ?> 

     

    Ah Thank you !!  Wait... that still doesn't work.  I have to declare $x as global or something.

  3. I am missing something with this code I decided to try.  See comment in code for problem converting 0 or 1 to TRUE or FALSE ?

    <?php
    $x = 11;
    var_dump ($x & 1);
    echo "<br>";
    //
    // var_dump is showing int(0) or int(1) for even or odd but function below is always saying even???
    //
    class calculations {
      public static function OddorEven() {
        if ($x & 1) {
         echo "x is odd";}
         else {
         echo "x is even";}
      }
    }
    // Call static method
    calculations::OddorEven();
    ?>

  4. On 8/5/2021 at 10:21 AM, Ingolme said:

    Do they really have a try-it example with this code?

    It seems there are more examples later in this section where they have a "Try it yourself" but the code doesn't produce anything in the output.  It would be clearer if they either modified the code to produce at least something, or explain that the code won't produce any output because xyz. 

  5. 23 hours ago, Ingolme said:

    That is correct. The code defines a class, but nothing will happen if you don't use the class.

    Do they really have a try-it example with this code?

    Yes - that's the code copy/pasted from the "Try it yourself".   So they could have explained nothing would happen (or even not bother making it a "Try it yourself" when trying it doesn't do anything except show you the code again?).   Looking at subsequent examples, I see that more code is necessary.  It's good to know what these layers of code do, but was a little confusing when you hit the button and nothing happens.

  6. When I "Try it yourself" on the first example, and hit RUN, nothing appears in the right box.  Is this correct?  If so, I'm not sure of the point.  Below is the code for that example.

    <!DOCTYPE html>
    <html>
    <body>

    <?php
    class Fruit {
      // Properties
      public $name;
      public $color;

      // Methods
      function set_name($name) {
        $this->name = $name;
      }
      function get_name() {
        return $this->name;
      }
    }
    ?>
     
    </body>
    </html>

  7. Ah - thanks everyone.  What I was missing was that an associative array is defined as KEY => VALUE.  So when you say "value" you're not talking about the value (or amount?) of any given entry, but value as the entity that each key is associated with. 

  8. I feel like I'm missing something on the example for Sort Array (Ascending Order), According to Value - asort().    I added a var_dump and also switched the variables to ABC, and don't see that any sorting is occurring.

    <!DOCTYPE html>
    <html>
    <body>

    <?php
    $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
    asort($age);

    var_dump($age);
    echo "<br><br>";


    foreach($age as $x => $x_value) {
      echo "Key=" . $x . ", Value=" . $x_value;
      echo "<br>";
      }

    echo "<br>";
      
    $age = array("C"=>"35", "B"=>"37", "A"=>"43");
    asort($age);

    var_dump($age);
    echo "<br>";
    echo "<br>";

    foreach($age as $x => $x_value) {
      echo "Key=" . $x . ", Value=" . $x_value;
      echo "<br>";
    }
    ?>

    </body>
    </html>

×
×
  • Create New...