PHP Tutorial – Part II

Printing Text
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!”);
print is the command and tells the script what to do 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.

Variables
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.
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.

Outputting Variables
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);
?>

Formatting Your Text
Everything is just output in the browser’s default font. It is very easy, though, to format your text using HTML. This is because, as PHP is a server side language, the code is executed before the page is sent to the browser. Thismeans that only the resulting information from the script is sent, so in the example above the browser would just be sent the text:
Hello and welcome to my website.
This means, though, that you can include standard HTML markup in your scripts and strings. The only problem with this is that many HTML tags require the ” sign. This means that you must tell the script which quotes should be used (the ones at the beginning and end of the output) and which ones should be ignored (the ones in the HTML code).

For this example I will change the text to the Arial font in red. The normal code for this would be:
<font face=”Arial” color=”#FF0000″>
</font>

As you can see this code contains 4 quotation marks so would confuse the script. Because of this you must add a backslash before each quotation mark to make the PHP script ignore it. The code would change to:

<font face=”Arial”color=”#FF0000″>

</font>

You can now include this in your print statement:

print(“<font face=”Arial” color”#FF0000″>Hello and welcome to my website.</font>”);

which will make the browser display:

Hello and welcome to my website.

because it has only been sent the code:

<font face=”Arial” color=”#FF0000″>Hello and welcome to my website.</font>

Like to share it
Tags: php,

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *