Learn PHP - A Beginner's Guide to PHP Programing

Learn PHP - A Beginner's Guide to PHP Programing

01of 09

Basic PHP Syntax

PHP is a server side scripting language used on the Internet to create dynamic web pages. It is often coupled with MySQL, a relational database server that can store the information and variables the PHP files may use. Together they can create everything from the simplest web site to a full blown business web site, an interactive web forum, or even an online role playing game.

Before we can do the big fancy stuff we must first learn the basics from which we build on.

  1. Start by creating a blank file using any program that can save in plain text format.
  2. Save your file as a .PHP file, for example mypage.php. Saving a page with the .php extension tells your server that it will need to execute the PHP code.
  3. Enter the statement to let the server know that there is PHP code coming up.
  4. After this we would enter the body of our PHP program.
  5. Enter the statement ?> to let the browser know the PHP code is done.

Every section of PHP code starts and ends by turning on and off PHP tags to let the server know that it needs to execute the PHP in between them. Here is an example:



//off ?>

Everything between the is read as PHP code. The statement can also be phrased as simply if desired. Anything outside of these PHP tags is read as HTML, so you can easily switch between PHP and HTML as needed. This will come in handy later in our lessons.

02of 09


If you want something to be ignored (a comment for example) you can put // before it as I did in our example on the previous page. There are a few other ways of creating comments within PHP, which I will demonstrate below:

//A comment on a single line

#Another single line comment

/* Using this method you can create a larger block of text and it will all be commented out */


One reason you may want to put a comment in your code is to make a note to yourself about what the code is doing for reference when you edit it later. You may also want to put comments in your code if you plan on sharing it with others and want them to understand what it does, or to include your name and terms of use within the script.

03of 09

PRINT and ECHO Statements

First we are going to learn about the echo statement, the most basic statement in PHP. What this does is output whatever you tell it to echo. For example:

This would return the statement I like About. Notice when we echo a statement, it is contained within quotation marks “”.

Another way to do this is to use the print function. An example of that would be:

There is a lot of debate about which is better to use or if there is any difference at all. Apparently in very large programs that are simply outputting text the ECHO statement will run slightly faster, but for the purposes of a beginner they are interchangeable.

Another thing to keep in mind is that all of your print/echoing is contained between quotation marks. If you want to use a quotation mark inside of the code, you must use a backslash:

"I like About too"" ?> var13 -->

PHP Test Page "; print "Billy said "I like About too"" ?> var13 -->

As you can see, you can insert HTML right into your php print line. You can format the HTML in the rest of the document as you please, but remember to save it as a .php file.

Do you use PRINT or ECHO? Share your answer!

04of 09


The next basic thing you need to learn how to do is to set a variable. A variable is something that represents another value.

This sets our variable, $like, to our previous I like About statement. Notice again the quotation marks “” used, as well as the semicolon ; to show the end of the statement. The second variable $num is an integer and therefore does not use the quotation marks. The next line prints out the variable $like and $num respectively. You can print more than one variable on a line using a period ., for example:

"; print $like . " " . $num; print "

"; print "My favorite number is $num"; ?> var13 -->

This shows two examples of printing more than one thing. The first print line prints the $like and $num variables, with the period . to separate them. The third print line prints the $like a variable, a blank space, and the $num variable, all separated by periods. The fifth line also demonstrates how a variable can be used within the quotation marks "".

A few things to remember when working with variables: they are CaSe SeNsitiVe, they are always defined with a $, and they must start with a letter or an underscore (not a number.) Also, note that if needed is possible to dynamically build variables.

05of 09


While a variable can hold a single piece of data, an array can hold a string of related data. Its use may not be apparent right away, but will become clearer as we start using loops and MySQL. Below is an example:

$age"Justin" = 45; $age"Lloyd" = 32; $age"Alexa" = 26; $age"Devron" = 15;

print "My friends names are " . $friend0 . ", " . $friend1 . ", " . $friend2 . ", and " . $friend3;

print "


print "Alexa is " . $age"Alexa" . " years old"; ?> var13 -->

The first array ($friend) is arranged using integers as the key (the key is the information between the brackets) which is handy when using loops. The second array ($age) shows that you can also use a string (text) as the key. As demonstrated the values are called by print in the same way a regular variable would be.

The same principals apply to arrays as variables: they are CaSe SeNsitiVe, they are always defined with a $, and they must start with a letter or an underscore (not a number.)

06of 09


You have probably all heard the term expression used in mathematics. We use expressions in PHP to preform operations and give an answer to a single value. These expressions are made up of two parts, the operators and the operands. The operands can be variables, numbers, strings, boolean values, or other expressions. Here is an example:

a = 3 + 4

In this expression the operands are a, 3 and 4

b = (3 + 4) / 2

In this expression the expression (3+4) is used as an operand along with b and 2.

07of 09


Now that you understand what an operand is we can go into more detail about what operators are. Operators tell us what to do with operands, and they fall into three major categories:

+(plus), - (minus), / (divided by), and * (multiplied by)

> (greater than), < (less than), == (equal to), and != (not equal to)

&& (true if both operands are true), || (true if at least one operand is true), xor (true if ONLY one operand is true), and ! (true if a single operand is false)

Mathematical operators are exactly what they are called, they apply mathematical functions to the operands. Comparison is also pretty straight forward, they compare one operand to another operand. Boolean however may need a little more explaining.

Boolean is an extremely simple form of logic. In Boolean every statement is either True or False. Think of a light switch, it must either be turned on or off, there is no in between. Let me give you an example:

$a = true;
$b = true;
$c = false;

$a && $b;
This is asking for $a and $b to both be true, since they are both true, this expression is TRUE

$a || $b;
This is asking for $a or $b to be true. Again this is a TRUE expression

$a xor $b;
This is asking for $a or $b, but not both, to be true. Since they are both true, this expression is FALSE

! $a;
This is asking for $a to be false. Since $a is true, this expression is FALSE

! $c;
This is asking for $c to be false. Since that is the case, this expression is TRUE

08of 09

Conditional Statements

Conditionals allow your program to make choices. Following the same sort of boolean logic you just learned about, the computer can only make two choices; true or false. In the case of PHP this is accomplished using IF : ELSE statements. Below is an example of an IF statement that would apply a senior's discount. If $over65 is false, everything within the {brackets} is simply ignored.

However, sometimes just the IF statement isn't enough, you need the ELSE statement as well. When using just the IF statement the code within the brackets either will (true) or will not (false) be executed before carrying on with the rest of the program. When we add in the ELSE statement, if the statement is true it will execute the first set of code and if it is false it will execute the second (ELSE) set of code. Here is an example:

09of 09

Nested Conditionals

One useful thing to remember about conditional statements is that they can be nested within each other. Below is an example of how the discount program from our example could be written to use nested IF:ELSE statements. There are other ways of doing this - such as using elseif () or switch () but this demonstrates how statements can be nested.

65) { $discount =.90; print "You have received our senior's discount, your price is $" . $price*$discount; } else { if ($age

This program will first check if they are eligible for the senior's discount. If they are not, it will then check if they are eligible for a student discount, before returning the non-discounted price.