Welcome to Day#2 of PHP series tutorial. Today we are going to learn how to declare variables in PHP and how to display them in browser.
In the last part of the tutorial I explained some of the basic stuffs 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 A Text Using PHP:
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:
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:
Which will display:
http://www.coolajax.net on the screen.
– How to use Variables in PHP:
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:
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 sensitive 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:
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. 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 “(Quote) sign. You may notice that this will clash with the quotation marks used to print your text. 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).
Was this information useful? What other tips would you like to read about in the future? Share your comments, feedback and experiences with us by commenting below!