Skip to content

Instantly share code, notes, and snippets.

@Mwamitovi
Last active January 29, 2025 02:08
Show Gist options
  • Select an option

  • Save Mwamitovi/f67a06daee87acad034cb93a11d7e9b5 to your computer and use it in GitHub Desktop.

Select an option

Save Mwamitovi/f67a06daee87acad034cb93a11d7e9b5 to your computer and use it in GitHub Desktop.

Revisions

  1. Mwamitovi renamed this gist Jan 29, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Mwamitovi created this gist Jan 29, 2025.
    72 changes: 72 additions & 0 deletions chapter3.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    <?php
    // Chapter - 3: Exercises

    // Question-1

    // 1. Without using a PHP program to evaluate them, determine whether each of
    // these expressions is true or false:
    // a. 100.00 - 100
    // b. "zero"
    // c. "false"
    // d. 0 + "true"
    // e. 0.000
    // f. "0.0"
    // g. strcmp("false","False")
    // h. 0 <=> "0"

    // Answer-1

    // 1. false
    // 2. true
    // 3. true
    // 4. false
    // 5. false
    // 6. true
    // 7. true
    // 8. false

    // Question-2

    // 2. Without running it through the PHP engine, figure out what this program prints:
    // $age = 12;
    // $shoe_size = 13;
    // if ($age > $shoe_size) {
    // print "Message 1.";
    // } elseif (($shoe_size++) && ($age > 20)) {
    // print "Message 2.";
    // } else {
    // print "Message 3.";
    // }
    // print "Age: $age. Shoe Size: $shoe_size";

    // Answer-2

    // Message 3.Age: 12. Shoe Size: 14

    // Question-3

    // 3. Use while() to print a table of Fahrenheit and Celsius temperature equivalents
    // from –50 degrees F to 50 degrees F in 5-degree increments. On the Fahrenheit
    // temperature scale, water freezes at 32 degrees and boils at 212 degrees. On the
    // Celsius scale, water freezes at 0 degrees and boils at 100 degrees. So, to convert
    // from Fahrenheit to Celsius, you subtract 32 from the temperature, multiply by 5,
    // and divide by 9. To convert from Celsius to Fahrenheit, you multiply by 9, divide
    // by 5, and then add 32.

    // Answer-3

    $f = -50;
    while ($f <= 50) {
    $c = ($f - 32) * (5 / 9);
    printf("%d degrees F = %d degrees C\n", $f, $c);
    $f += 5;
    }

    // 4. Modify your answer to Exercise 3 to use for() instead of while().

    // Answer-4

    for ($f = -50; $f <= 50; $f += 5) {
    $c = ($f - 32) * (5 / 9);
    printf("%d degrees F = %d degrees C\n", $f, $c);
    }