PHP Lab #3: Variables

  1. A PHP variable is a symbol used to store a value. For example, add the following assignment statement to your hello.php.

    $sentence = "The moose is brooding";

  2. Now use this variable by adding the following line to your hello.php, after the above:

    print("The sentence is: $sentence <BR>");
    Your web page should show "The sentence is: The moose is brooding".

  3. Variables can also be set equal to numbers. Try adding the following to hello.php, and see what happens:

    $x = 3;
    print($x);
    print("<BR>");
    $x = 3.14159265;
    print($x);
    print("<BR>");
    $x = "My pen is leaking.";
    print($x);
    print("<BR>");

  4. Some things to note:

    1. Whenever you mention a variable to PHP, give it a dollar sign.
    2. Variable names are case sensitive, so $var is not the same as $VAR.

  5. Try the following PHP code and see what happens.

    $a = 5;
    $b = 3;
    print($a * $b);
    print("<BR>");
    print($a / $b);
    print("<BR>");
    print($a % $b);
    print("<BR>");
    Answer the following questions.


  6. Page by Michelle Phillips, Jeff Ondich, and Dave Musicant. Last updated 10.03.02