Practice pinned on noticeboardIn the previous section, you saw what variables are: storage areas to hold things like numbers and text. You tell PHP to remember these values because you want to do something with them. In this section, you’ll get some practice using variables. Off we go.

Testing variables with PHP

First, we’ll take a look at how to display what’s in your variables. We’re going to be viewing our results on a web page. So see if you can get this script working first, because it’s the one we’ll be building on. Using a text editor like Notepad, or your PHP software, type the following. (You can copy and paste it, if you prefer. But you learn more by typing it out yourself – it doesn’t really sink in unless you’re making mistakes!)

 

 

<html>
<head>
<title>Variables – Some Practice</title>
</head>
<body>

<?php print(“It Worked!”); ?>

</body>
</html>

When you’ve finished typing it all, save the page as variables.php. Then Run the script. Remember: when you’re saving your work, save it to the WWW folder, as explained here. To run the page, start your browser up and type this in the address bar:

http://localhost/variables.php

If you’ve created a folder inside the www folder, then the address to type in your browser would be something like:

http://localhost/FolderName/variables.php

If you were successful, you should have seen the text “It worked!” displayed in your browser. If so, Congratulations! You have a working server up and running! (If you’re using Wampserver, you should see an icon in the bottom right of your screen. Click the icon and select Start All Services from the menu.)

The PHP script is only one line long:

<?php print(“It Worked!”); ?>

The rest of the script is just plain HTML code. Let’s examine the PHP in more detail.

We’ve put the PHP in the BODY section of an HTML page. Scripts can also, and often do, go between the HEAD section of an HTML page. You can also write your script without any HTML. But before a browser can recognise your script, it needs some help. You have to tell it what kind of script it is. Browsers recognise PHP by looking for this punctuation (called syntax):

<?php ?>

So you need a left angle bracket ( < ) then a question mark ( ? ). After the question mark, type PHP (in upper or lowercase). After your script has finished, type another question mark. Finally, you need a right angle bracket ( > ). You can put as much space as you like between the opening and closing syntax.

To display things on the page, we’ve used print( ). What you want the browser to print goes between the round brackets. If you’re printing direct text, then you need the quotation marks (single or double quotes). To print what’s inside of a variable, just type the variable name (including the dollar). Finally, the line of code ends as normal – with a semi-colon (;). Another way to display things on the page is to use an alternative to print() ā€“ echo( ).

Now let’s adapt the basic page so that we can set up some variables. We’ll try some text first. Keep the HTML as it is, but change your PHP from this:

<?php print(“It Worked!”); ?>

To this:

<?php

print(“It Worked!”);

?>

OK, it’s not much of a change! But spreading your code out over more than one line makes it easier to see what you’re doing. Now, it’s clear that there’s only one line of code – Print. So add this second line to your code (the one in red):

<?php

$test_String = “It Worked!”;

print(“It Worked!”);

?>

We’ve set up a variable called $test_String. After the equals sign, the text “It Worked!” has been added. The line is then ended with a semi-colon. Don’t run your script yet. Change the Print line to this:

print($test_String);

Then add some comments …

<?php
//————–TESTING VARIABLES————

$test_String = “It Worked!”;

print($test_String);

?>

Comments in PHP are for your benefit. They help you remember what the code is supposed to do. A comment can be added by typing two slashes. This tells PHP to ignore the rest of the line. After the two slashes, you can type anything you like. Another way to add a comment, is like this:

<?php

/* ————–TESTING VARIABLES————
Use this type of comment if you want to spill over to more than one line.
Notice how the comment begin and end.
*/

$test_String = “It Worked!”;

print($test_String);

?>

Whichever method you choose, make sure you add comment to your code: they really do help. Especially if you have to send your code to someone else!

But you can now run the script above, and test it out.

How did you get on? You should have seen that exactly the same text got printed to the page. And you might be thinking – what’s the big deal? Well, what you just did was to pass some text to a variable, and then have PHP print the contents of the variable. It’s a big step: your coding career has now begun!

Exercise
Change the text “It Worked!” to anything you like. Then run the script again. Try typing some numbers in between your double quotes, instead of text.

Exercise
Change the double quotes to single quotes. Did it have any effect? Put a single quote at the beginning of your text, and a double quote at the end. What happens when you run the code?

Exercise
Delete the dollar sign from the variable name. Then run your code. What error did you get? Put the dollar sign back, but now delete the semi-colon. Run your code again? What error did you get, this time? It’s well worth remembering these errors – you’ll see them a lot when you’re starting out! If you see them in future, you’ll be better able to correct your errors.

 
Download the PHP Course Files

From http://www.homeandlearn.co.uk