In 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>
<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:</html>
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------------
//--------------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.
*/
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!
in the previous lesson you started to work with variables. You outputted text to a page. In the next few sections, you'll do some more work with variables, and learn how to do your sums with PHP.
But now that you can print text to a page, let's try some numbers. Start with the basic PHP page again, and save your work as variables2.php:
<html>
<head>
<title>More on Variables</title>
</head>
<body>
<head>
<title>More on Variables</title>
</head>
<body>
<?php
print ("Basic Page");
?>
</body>
</html>
We'll now set up a variable and print it to the page. So change your code to
this:</html>
<?php
$first_number = 10;
print ($first_number);
print ($first_number);
?>
All the code does is to print the contents of the variable that we've called
$first_number. Remember: if you're printing direct text then you need
quotation marks; if you're printing a variable name then you leave the quotes
out. To see why, run the first script above. Then change the print line to this:
print ("$first_number");
In other words, add double quotation marks around your variable name. Did it
make a difference? What did you expect would print out? Now change the double
quotes to single quotes. Run your script again. With double quotes, the number
10 still prints; with single quotes, you get the variable name!
0 comments:
Post a Comment