How To Learn PHP Programming

PHP is said to be an acronym for "PHP: Hypertext Preprocessor", however it's probably just HPP (hypertext preprocessor) rearranged to sound better.  It is an open source scripting language, meaning that it's source code is freely available for anyone to work with it.  More importantly it is free for you to download and use. 

PHP is server side, meaning that it is processed by the web server at the web host rather than the browser, which gives the web developer more control over the outcome.  Although it can be used for console scripting, it is primarily used by web developers to implement dynamic content.  Dynamic content is created on the fly using a combination of PHP and HTML.  HTML is usually used to create the look and feel of the site (a template) and PHP manipulates data from a database.

The most widely used feature of PHP is its ability to work with many different types of databases especially MySQL, another open source solution.  Open source operating system Linux, and open source web server Apache, are commonly used in conjunction with MySQL and PHP in what is called a LAMP (an acronym for the 4 open source technologies) setup.  One common application for the LAMP setup is a site where members have a personalized homepage that greets them by name.  In this tutorial you will learn the basics of PHP.  If you have programmed in another language you will find that things only differ slightly.

  1. Use A WAMP (Windows, Apache, MySQL, PHP) setup program to install Apache, MySQL, and PHP all at once.  You can find this program at http://www.wampserver.com/en/.  ry to use the latest, stable build for your operating system.
  2. In keeping with programming tradition your first program in PHP will be a "Hello World" script.  We will simply make the browser display a line that says "Hello World".  Remember, PHP is embedded in HTML.  To put some PHP code in your website you have to surround it with <? , ?>  tags. 

    For example : <? PHP is amazing ?>.  Now we will tell PHP what we want it to do by using a "statement".  To tell PHP to show "Hello World" we we will use the PRINT statement.

  3. The following is the PHP code using the PRINT statement to display a phrase: <? print("Hello World"); ?>.  Notice how the words we want displayed are surrounded by quotation marks and then parentheses.  Everything in the parentheses is processed by the print function and everything in quotations is immediately given to the browser to process and display.  Also notice that a semi-colon is placed immediately after the PRINT statement but before the ?> endtag.  The semi-colon denotes the end of the statement.
  4.  To see the PHP code in action we have to embed it into an HTML page. For our purposes we'll use a blank page written in HTML:

    <HTML>
    <HEAD><TITLE>HELLO WORLD</TITLE></HEAD>
    <BODY>
    <? print("Hello World"); ?>
    </BODY>
    </HTML>

  5. Save it with notepad or any ASCII based word processor (not any rich text word processing program like MS Word) into the document root (the folder where files for the websites are kept as established by your server) as "helloworldv1.php".  To open it, go to http://localhost/helloworldv1.php.  It should display a message saying "Hello World".
  6. Now we'll mix it up a little and learn about variables.  If you've ever programmed in another language beside HTML variables work the same way.  Even if you have not, you too have encountered variables in basic algebra.  Instead of x or n, a variable is any sequence of letters after a dollar sign ($). 

    For example: $thisisavariable.  Just like in an equation, a number (such as 4) can be the same x, a phrase or number could be assigned to a variable.  Let's say we want to assign the variable "Hello World" to a variable called $firstvariable.  As x=4 the code would read <? $firstvariable="Hello World"; ?>.  Now to display it we simply use the $firstvariable variable wherever we want "Hello World" to appear.  Now let's modify Hello World Version 1 using variables.

    • Version 1: <? print("Hello World"); ?>
    • Version 2: <? print($firstvariable="Hello World"; $firstvariable); ?>

  7. Notice that no quotations were used because it is a variable that's not ready to be processed by the browser.  Also notice that there is a semicolon between the statement that assigns the variable and the one that displays it. When you save Version 2 and open it in your browser it should appear the same as Version 1.

Now you've learned the basics of PHP. From here you can go on to learn about more useful functions for manipulating and using information from databases. To learn more about PHP visit their website at php.net.  Happy programming!

 

Share this article!

Follow us!

Find more helpful articles: