Sunday 3 April 2011

PHP for Beginners – Introduction & Displaying Information


Up until recently, scripting on the Internet was something which very few people even attempted, let alone mastered. Recently though, more and more people have been building their own websites and scripting languages have become more important. Because of this, scripting languages are becoming easier to learn and PHP is one of the easiest and most powerful yet.


What Is PHP?

PHP stands for Hypertext Preprocessor and is a server-side language. This means that the script is run on your web server, not on the user’s browser, so you do not need to worry about compatibility issues. PHP is relatively new (compared to languages such as Perl (CGI) and Java) but is quickly becoming one of the most popular scripting languages on the Internet.

Why PHP?

You may be wondering why you should choose PHP over other languages such as Perl or even why you should learn a scripting language at all. I will deal with learning scripting languages first. Learning a scripting language, or even understanding one, can open up huge new possibilities for your website. Although you can download pre-made scripts from sites like Hotscripts, these will often contain advertising for the author or will not do exactly what you want. With an understanding of a scripting language you can easily edit these scripts to do what you want, or even create your own scripts.
Using scripts on your website allows you to add many new ‘interactive’ features like feedback forms, guestbooks, message boards, counters and even more advanced features like portal systems, content management, advertising managers etc. With these sort of things on your website you will find that it gives a more professional image. As well as this, anyone wanting to work in the site development industry will find that it is much easier to get a job if they know a scripting language.

What Do I Need?

As mentioned earlier, PHP is a server-side scripting language. This means that, although your users will not need to install new software, you web host will need to have PHP set up on their server. It should be listed as part of your package but if you don’t know if it is installed you can find out using the first script in this tutorial. If you server does not support PHP you can ask your web host to install it for you as it is free to download and install.

Writing PHP

Writing PHP on your computer is actually very simple. You don’t need any specail software, except for a text editor (like Notepad in Windows or excellent and free Notepad++). Run this and you are ready to write your first PHP script.

Declaring PHP

PHP scripts are always enclosed in between two PHP tags. This tells your server to parse the information between them as PHP. The three different forms are as follows:
1< ?
2Your PHP Code Is Here
3?>
1< ?php
2Your PHP Code Is Here
3php?>
1<script language="php">
2Your PHP Code Is Here
3</script>
All of these work in exactly the same way but in this tutorial I will be using the second option (< ?php and ?>). You must remember to start and end your code with the same tag (you can’t start with < ? and end with for example).

Writing Your Very First Script

The first PHP script you will be writing is very basic. All it will do is print out all the information about PHP on your server. Type the following code into your text editor of choice:
1< ?php
2phpinfo();
3?>
As you can see this actually just one line of code. It is a standard PHP function called phpinfo which will tell the server to print out a standard table of information giving you information on the setup of the server.
One other thing you should notice in this example is that the line ends in a semicolon. This is very important. As with many other scripting and programming languages nearly all lines are ended with a semicolon and if you miss it out you will get an error.

Finishing and Testing Your Script

Now you have finished your script save it as phpinfo.php and upload it to your server in the normal way. Now, using your browser, go the the URL of the script. If it has worked (and if PHP is installed on your server) you should get a huge page full of the information about PHP on your server.
If your script doesn’t work and a blank page displays, you have either mistyped your code or your server does not support this function (although I have not yet found a server that does not). If, instead of a page being displayed, you are prompted to download the file, PHP is not installed on your server and you should either search for a new web host or ask your current host to install PHP.
It is a good idea to keep this script for future reference.

Let’s move on to Displaying Information & Variables Section.

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:
1< ?php
2print("Hello world!");
3?>
Let’s 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 inside quotation marks. Finally, as with nearly every line in a PHP script, it must end in a semicolon.
The result will look like:
1Hello 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:
1$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 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.
1$tut_number = 1234;

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:
1$welcome_text = "Hello and welcome to my article.";
2print($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 article. 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. 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). For this example let’s change the text to the Arial font in red. The normal code for this would be:
1<font face="Arial" color="#FF0000">
2</font>
*Note Never use inline styles in your projects! I wrote these just for example. 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:
1<font face=\"Arial\" color=\"#FF0000\">
2</font>
You can now include this in your print statement:
1print("<span style="font-family: &amp;quot;">Hello and welcome to my article.</span>");
which will make the browser display:
1< ?php print("<span style="font-family: &amp;quot;">Hello and welcome to my article."); ?>
because it has only been sent the code:
1<font face="Arial" color="#FF0000">Hello and welcome to my article.</font>
to be continued...

0 comments:

Post a Comment