Class 1 Notes

Program Downloads:

To be ready to use php and MySQL on your laptop,  you need to install a local server that will run php and MySQL on your computer so you can edit files for our exercises and view them through your browser.
Bonus useful tool: 

Komodo Edit (a free code editor that will color-code for you and help you with basic syntax like closing brackets correctly)

Exercise Downloads:

Dubi Kaufman provided this week's exercises through his website as a zip download: 
NOTE: if you are using these files on a PC, you will need to correct the first line of each to get them to run: from <? to <?php

Each exercise introduces a new basic concept:

Exercise 1:

Learn how to output text to your browser using echo

Exercise 2:

Learn how to link to other files and display them using include

Exercise 3:

Learn how to define variables and initialize them with content using $ and =

Exercise 4:

Learn how to combine variables using arithmetic functions or concatenation (+ or .)

Exercise 5:

Learn how to initialize an array using parenthesis and commas

Exercise 6:

Learn how to use for loops and incrementing variables to repeat an output 

After the Exercises:

  • using == to check values
  • using if loops
  • using if/else loops
  • using the count function on an array

example: if-else loops & counting

posted Apr 30, 2013, 12:01 PM by Emily Barney   [ updated Apr 30, 2013, 11:12 PM ]

This is how I used the count function to measure the number of items in my array and if/else loops to add text after each item.

Here's the code (the file is attached below, if you want to download it and run it or fiddle with it):
$myArray=array("apples","bananas","cantaloupes","dragonfruit","etrog","figs","bananas");
$total=count($myArray); 
//learn more about this function here: http://www.php.net/manual/en/function.count.php

echo"We have ".$total." fruit:"."<ol>";
//Creating an ordered (numbered) list will help you check for accuracy

for ($i = 0; $i <= $total-1; $i++)
//initial state: when i starts at 0
//condition to end loop: when i is equal to or less than the total (subtract 1 because it starts at zero)
//action while loop continues: add 1 and repeat until i meets the total
    {
    echo"<li>".$myArray[$i];
        if ($myArray[$i]=="bananas") //checking for bananas
            {
            echo " <i>We Have NO Bananas Today!</i></li>"; //adding one description
            }
        else
            {
            echo" <i>Fresh!</i></li>"; //adding a different description for the rest
            }
    }

echo "</ol>";

Here's what should display:
We have 7 fruit:
    1. apples Fresh!
    2. bananas We Have NO Bananas Today!
    3. cantaloupes Fresh!
    4. dragonfruit Fresh!
    5. etrog Fresh!
    6. figs Fresh!
    7. bananas We Have NO Bananas Today!

1-1 of 1

Comments