Web Design Articles

Part 2: Formatting and Printing Text

Web Design Articles

Submit a Web Design Article


Web Design Articles    |    Code    |    PHP

Here you will find articles about Web Design, Web Site Accessibility, Web Standards and Internet Marketing. If you would like to have an article you have written published on this site please use the form above to submit your article for publication. If your article is published, you will receive a credit with a byline linked to your website URL.


   Article

Part 2: Formatting and Printing Text

by David Gowans


Introduction

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.


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!");


The print command above 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 inside 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 the following output on the screen:

Hello world!


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, everything inside the quotation marks will be assigned to the string. You must remember a few rules about strings:

  • 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 variables, you do not always need to include the quotes:

$user_id = 987


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


As you can see, the only major difference is that you do not need the quotation marks if you are printing a variable.


Formatting Your Text

Unfortunately, the output from your PHP programs is quite boring. 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. This means 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.


However, you can include standard HTML markup in your PHP scripts to give your text the look you want. This type of string manipulation can be accomplished via a technique called String Concatenation. We will attach HTML markup to the front and end of our output string to assign the desired attributes to our output string.


For this example I will assign a font style, text color, and text size to our output string:

<?
$welcome_text = "Hello and welcome to my website.";

$welcome_text = ´<p style="font: Arial; color: red; font-size: 11px;">´.$welcome_text.´</p>´
print ($welcome_text);
?>


You can now review your output string to ensure the correct attributes have been set:

Hello and welcome to my website.


Dynamic Text using PHP

PHP script can be inserted anywhere within a pre-formatted HTML document as long as it is contained within a <? and ?>. See below for one final example:

<?
$welcome_text = "Hello and welcome to my website.";
?>

....

<body>
<p style="font: Arial; color: red; font-size: 11px;"><? print ($welcome_text); ?></p>
</body>


The output string will be formatted the same either way because you have ultimately produced the same code.


posted on Jun 4, 2007

   Topics



   Google


Web Design Articles    |    Code    |    PHP