<h2>Introduction </h2>
In the last part of the tutorial I explained some of the advantages of PHP as a scripting language and showed you how to test your server for PHP. In this part I will show you the basics of showing information in the browser and how you can use variables to hold information.
<h2>Printing Text</h2>To output text in your PHP script is actually very simple. As with most other things in PHP, you can do it in a variety of different ways. The main one you will be using, though, is print. Print will allow you to output text, variables or a combination of the two so that they display on the screen.
The print statement is used in the following way:
print("Hello world!");
I will explain the above line:
print is the command and tells the script what to do. This is followed by the information to be printed, which is contained in the brackets. Because you are outputting text, the text is also enclosed instide quotation marks. Finally, as with nearly every line in a PHP script, it must end in a semicolon. You would, of course, have to enclose this in your standard PHP tags, making the following code:
<?
print("Hello world!");
?>
Which will display:
Hello world!
on the screen.
<h2>Variables</h2>As with other programming languages, PHP allows you to define variables. In PHP there are several variable types, but the most common is called a String. It can hold text and numbers. All strings begin with a $ sign. To assign some text to a string you would use the following code:
$welcome_text = "Hello and welcome to my website.";
This is quite a simple line to understand, everything inside the quotation marks will be assigned to the string. You must remember a few rules about strings though:
Strings are case sensetive so $Welcome_Text is not the same as $welcome_text
String names can contain letters, numbers and underscores but cannot begin with
a number or underscore
When assigning numbers to strings you do not need to include the quotes so:
$user_id = 987
would be allowed.
<h2>Outputting Variables</h2>To display a variable on the screen uses exactly the same code as to display text but in a slightly different form. The following code would display your welcome text:
<?
$welcome_text = "Hello and welcome to my website.";
print($welcome_text);
?>
As you can see, the only major difference is that you do not need the quotation marks if you are printing a variable.
You can log-in or register for a user account here.


