PHP for Beginners: Part 1 – Introduction & Displaying Information

Posted in Tutorials, Web Design4 years ago • Written by 8 Comments

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:

< ?
Your PHP Code Is Here
?>
< ?php
Your PHP Code Is Here
php?>
<script language="php">
Your PHP Code Is Here
</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:


< ?php
phpinfo();
?>

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:

< ?php
print("Hello world!");
?>

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:

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 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.
$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:

$welcome_text = "Hello and welcome to my article.";
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 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:


<font face="Arial" color="#FF0000">
</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:


<font face=\"Arial\" color=\"#FF0000\">
</font>

You can now include this in your print statement:

print("<span style="font-family: &amp;quot;">Hello and welcome to my article.</span>");

which will make the browser display:

< ?php print("<span style="font-family: &amp;quot;">Hello and welcome to my article."); ?>

because it has only been sent the code:

<font face="Arial" color="#FF0000">Hello and welcome to my article.</font>

This does make it quite difficult to output HTML code into the browser but later in this tutorial I will show you another way of doing this which can make it a bit easier.

Conclusion

In this part I have introduced you to the basics of writing and running PHP. By this time you should now know if your host supports PHP and should have a basic understanding of how PHP scripts are structured. In the second part of the tutorial i have explained you how to print out information to the browser.

7 Written ArticlesWebsite

Hi there! My name is Nick Plekhanov. I'm a passionate web developer and user interface designer from Russia, experienced in PHP, and MySQL along with the beautiful XHTML and CSS under jQuery stuff. I've been doing web development for 3 years. Here, I'll be your PHP coder, sometimes designer, and SEO expert. My own web development blog is coming really soon. If you’d like to connect with me, follow me on Twitter.

8 Comments Best Comments First
  • Rahul

    Thursday, November 11th, 2010 12:36

    1

    Great post i know really people are using these languages too much despite its importance and knowing more about it is good , so thank you lot sharing this , taking looks at pictures made me read about them and know the steps and info. abt this thank You

    0
  • pathe3

    Saturday, November 13th, 2010 22:40

    3

    Good information, but with minor errors. You state to place PHP code in between two php tags that are the same, but your examples use mixed PHP tags. If someone cuts and pastes your examples, they will get an error if they try to execute the code.

    0
  • Julia

    Friday, November 12th, 2010 07:42

    2

    A very nice attempt to dive into php, but I dont think it’s a good idea to use tag “font” in year 2010. It’s deprecated.

    0
  • Allan Smithie

    Monday, November 15th, 2010 17:50

    4

    Someone who may be learning PHP will often take a tutorial at face value, and not what may be implied. Unless you explicitly state every step clearly and distinctly, you will just create more confusion. Following this tutorial at face value will only cause problems because every example given is not a complete PHP code snippet.

    0
  • Fred

    Saturday, November 20th, 2010 02:08

    5

    Great starter, As a web designer I’m trying to learn some programming languages to really expand my knowledge of the web and while stay expert in my particular field also broaden my skills and become more general in other areas.

    0
  • Jeremiah

    Sunday, January 29th, 2012 12:13

    8

    Great post Nick. I’m just starting to get involved with PHP myself, so I would definitely put myself in the “beginners” class… With that said, this post was more than just resourceful, it was timely too! I’m just starting to crack open a few books that are a bit more advanced but I want to thank you for this brief post to really help me understand the “basics.”
    Cheers!
    Jeremiah R.

    0
  • Hyder Mohammed

    Saturday, July 2nd, 2011 20:53

    7

    Great article, i’m new to web designing stuff, Hope i will read all parts of this series :-)

    0
  • Brett Widmann

    Sunday, February 27th, 2011 03:35

    6

    I know my way around PHP but this is a great overview for those just getting started

    0
  • Jeremiah

    Sunday, January 29th, 2012 12:13

    8

    Great post Nick. I’m just starting to get involved with PHP myself, so I would definitely put myself in the “beginners” class… With that said, this post was more than just resourceful, it was timely too! I’m just starting to crack open a few books that are a bit more advanced but I want to thank you for this brief post to really help me understand the “basics.”
    Cheers!
    Jeremiah R.

    0
  • Hyder Mohammed

    Saturday, July 2nd, 2011 20:53

    7

    Great article, i’m new to web designing stuff, Hope i will read all parts of this series :-)

    0
  • Brett Widmann

    Sunday, February 27th, 2011 03:35

    6

    I know my way around PHP but this is a great overview for those just getting started

    0
  • Fred

    Saturday, November 20th, 2010 02:08

    5

    Great starter, As a web designer I’m trying to learn some programming languages to really expand my knowledge of the web and while stay expert in my particular field also broaden my skills and become more general in other areas.

    0
  • Allan Smithie

    Monday, November 15th, 2010 17:50

    4

    Someone who may be learning PHP will often take a tutorial at face value, and not what may be implied. Unless you explicitly state every step clearly and distinctly, you will just create more confusion. Following this tutorial at face value will only cause problems because every example given is not a complete PHP code snippet.

    0
  • pathe3

    Saturday, November 13th, 2010 22:40

    3

    Good information, but with minor errors. You state to place PHP code in between two php tags that are the same, but your examples use mixed PHP tags. If someone cuts and pastes your examples, they will get an error if they try to execute the code.

    0
  • Julia

    Friday, November 12th, 2010 07:42

    2

    A very nice attempt to dive into php, but I dont think it’s a good idea to use tag “font” in year 2010. It’s deprecated.

    0
  • Rahul

    Thursday, November 11th, 2010 12:36

    1

    Great post i know really people are using these languages too much despite its importance and knowing more about it is good , so thank you lot sharing this , taking looks at pictures made me read about them and know the steps and info. abt this thank You

    0

Comments are closed.

54.226.80.196 - unknown - unknown - US