Screen Printing

TUTORIALS » PHP / MySQL » PHP essentials (guided tutorials)

Variables in PHP

Page 2 of 2


You are in page:
» 2. Practice

Practice with PHP variables

In the previous page of this tutorial you learned the basics of PHP variables so, without further ado, let's put it into practice.
  1. Create a new PHP file named variables1.php
  2. Edit your file so it looks like this:
    <?php
     $greets = 'Hello World';
     echo $greets;
    ?>
  3. Upload the file to your server and test it with your browser (remember, PHP files are tested from the server, not from your computer).
Q. What do you see on your screen?

You just stored a string inside your variable $greets, then you used the instruction echo to display the value of that variable with the browser.
  1. Edit your file, this time with the following code:
    <?php
     $aaa = 200;
     $bbb = $aaa * 5;
     echo $bbb;
    ?>
  2. Upload the file to your server and test it with your browser.
You just performed a math operation a stored the result in a variable; then you displayed the value of this last variable on the screen.
  1. Now do it with:
    <?php
     $y = 10;
     $z = 2;
     echo $y * $z;
    ?>
  2. Upload the file to your server and test it with your browser.
This time you did not store the result of the operation in a variable, you just sent such result, using echo, directly to the screen of the client's computer.
  1. Last one; this time write:
    <?php
     $quantity = 50;
     $item = "Monkeys";
     echo "I have <b>$quantity</b> blue $item stored in a box";
    ?>
  2. Upload the file to your server and test it with your browser.

Now it is time to discuss the pros and cons of using single and double quotes for strings in PHP. Follow to the next tutorial.

More pages of this tutorial → Variables in PHP  <12

Comments & Questions