PHP Basics

Before you can learn PHP programming, you need to understand the basics of this language.

The beauty of PHP programming is how versatile it is. It can actually be used alongside HTML, CSS or even Javascript programming.

Since PHP can be used along with other website programming languages, the PHP code on your pages must be properly identified so that it will be parsed correctly.

To indicate an area of PHP code, simply enclose the area of code in the appropriate opening and closing tags:

<?php

// Above is an opening PHP tag

/* The PHP code is everything between the tags

All of the additional text written here is commented, which means that it isn't parsed as PHP code and can be used to write your own notes with your code.

*/

// Below is the closing PHP tag ?>

In the example above, I have shown the opening and closing PHP tags.

The opening tag begins with a less than symbol, followed by a question mark and then the letters php: <?php

The closing tag is just a question mark and then a greater than symbol: ?>

Everything between those two tags will be the PHP code, unless you use comments. A code comment is an area where you can write anything you want, and it won't be processed as PHP code or shown on your live website!

I have provided two examples of code commenting above. The first uses two forward slashes: //

When you use two forward slashes to comment code, it only comments that one line of code. If you wanted to have multiple commented lines that way, you would want to have the two forward slashes at the beginning of each line.

Another commenting option allows you to comment multiple lines at a time. To do this, start it with a forward slash followed by a star (asterisk): /*

You then end the comment with the same thing backwards - a star followed by a forward slash: */ Everything between the two stars is commented, so it won't get processed.

Why is this even important?

#1. It is considered good programming practice to comment your coding, even if nobody will ever see it, because it helps you to remember what your own coding does. This is especially helpful for beginners that may not be able to look at a section of code and immediately know what it does.

#2. Personally, I find it useful for debugging and even building new features. If you have some code that is working correctly and you want to make some changes to it, you can simply duplicate the code and comment out half of it to essentially save the old code. Then, you can modify the duplicated code. If you run into problems and realized that you messed up the code with your changes, it makes it very easy to revert your changes.

Using PHP in WordPress

Typically, you can't use PHP coding like that within WordPress post/page content. However, if you search for and install a free plugin called EXEC-PHP, it will allow you to do just that. Other plugins exist that do the same or something similar, but I have found this one to be reliable.

PHP Variables

Now that you know how to indicate code as PHP code, we can proceed to learn some actual programming.

At the heart of all programming is something called a variable. If you aren't familiar with this term, just think of a variable as temporary storage for information in your code. A variable could be a number or even a string (non-numerical text is referred to as a string in programming).

I have created a very basic example for you below that shows how a variable works:

<?php
$a = 1;
echo $a;
?>

In this PHP code, I have created a variable called 'a' and assigned it a value of the number '1

Notice the dollar sign before the letter 'a'. That dollar sign indicates that it is a variable and the letter 'a' is the name of the variable. To assign a value, just use the equal symbol, followed by the value for the variable, and then close it with a semicolon.

The second line of PHP code in the example uses a PHP command called echo. Echo simply outputs information to the browser (browser output is then essentially treated as HTML code). So, this PHP code would simply show the number 1 in a browser.

Next, I have created another example just like the previous example, except this time I have used text as the variable value:

<?php
$a = 'Hello';
echo $a;
?>

This example will simply output the text Hello to the browser. The key difference between this example and the previous example are the quotes around the variable value of Hello. Quotes are needed for values that are NOT numerical, which is why I used it in the second example but not the first.

Variable Arrays

Once variables are understood, the next step is to learn about arrays. An array is simply a set of variables.

You actually start an array in the same manner as a variable - with a dollar sign and then the name you want to give the array.

The code below shows an example array being created:

<?php
$a = array('Hello', 'Hello Again');
echo $a[0];
echo '<br />'.$a[1];
?>

In this example, I have created an array called 'a' this time. The value begins with array and an opening parenthesis. After that are the values for my array. These are entered in the same way that variables are assigned values (use a numerical value without quotes and text values with quotes), except that you split the values of the array with a comma. The array declaration ends with a closing parenthesis and a semicolon.

I have assigned two values to this array - the first is the text Hello and the second is the text Hello Again. Both are enclosed in quotes because they are text values. Numerical values do not need the quotes.

With the next two lines of code, I have echoed the values of the array. This works very similar to my previous examples with variables except that with arrays you have to reference the name of a key in an array to get the value.

With this example, my array keys are automatically assigned a numerical name starting with 0 and incrementing one for each new value in the array. Since I have given this array two values, then there are two keys in the array: 0 and 1.

To reference an array key, simply enclose the key name in brackets: []

For this example, I simply add the number between the brackets to reference that key number.

Array keys can also be given text names instead of numerical names.

I have created another example for you below to see how this works:

<?php
$a = array('first_key' => 'Hello', 'second_key' => 'Hello Again');
echo $a['first_key'];
echo '<br />'.$a['second_key'];
?>

When I create the array this time, notice that I have provided an extra value before each of the previous values that I had in the array. The first of these extra values is called first_key and it is followed by an equal sign and then a greater than symbol and finally the original value of Hello. All of that is in reference to one value in the array - Hello and first_key is the key name for that value. I have then done the same thing with the second key and value in the array.

Now when I want to reference the values in this array, I use the key name enclosed in quotes. So, to get Hello to output to the browser, I would use echo and then reference the array value with $a['first_key'] (and be sure to end the line with a semicolon).

Semicolon Line Closings

By this point, you may have noticed that each line of my PHP code ends in a semicolon. This will almost always be the case - the symbol tells PHP that you've completed that line of code and/or PHP command and to start looking for the next. Think of this like HTML tags that are self-closing.

Next, I will get into more PHP commands, some of which are not self-closing, so they do not require a semicolon. Most of these commands will make use of parenthesis and/or curly brackets to indicate opening and closing instead of depending on a semicolon as a closer.

If, Else Statements

One of the most essential PHP commands that you will find yourself using all the time is actually a very simple command: IF.

The IF command is used to control what happens in your coding based on variable values.

 The example below demonstrates the IF command:

<?php
$a = 1;
if ($a == 1)
{
echo 'The variable 'a' is equal to 1.';
}
?>

In the example above, there is a variable that is set to a value of '1'.

Next, the IF command is used. Following the IF command, there is an opening parenthesis, more information, and then a closing parenthesis.

The information between the parenthesis is actually a test statement. $a is making reference to the variable. The two equal signs translates into English as “is equal to”. Last, there is the number 1 and a semicolon.

That entire line of code reads as: “IF the variable by the name of 'a' is equal to the number 1”

Next, you'll notice an opening curly bracket followed by a closing curly bracket two more lines down. These brackets are the opening and closing containers for the IF statement. All of the code between the two curly brackets will ONLY be processed when the IF statement is TRUE!

Now I am going to go another step and complicate the IF statement a bit with the ELSE command. When ELSE is combined with an IF statement, you can provide two sets of code - one that is processed when the IF statement is TRUE (like IF works by itself, as demonstrated previously) and then the other that is processed when the IF statement is FALSE.

Here is another example to demonstrate:

<?php
$a = 1;
if ($a == 1)
{
echo 'The variable 'a' is equal to 1.';
}else{
echo 'The variable 'a' is not equal to 1.';
}
?>

In this example, I have added the ELSE command to the IF statement. ELSE actually goes immediately after the closing curly bracket for the IF statement, and then a new set of curly brackets are used to contain the code for the ELSE statement (which will run when the IF statement is FALSE).

Obviously, using the example code above will only result in the text “The variable 'a' is equal to 1.” being shown. The code for the ELSE statement that says the variable is not equal to 1 would never run because the variable, $a, is set to the value of 1 and doesn't change in this code. However, this example was done to provide the most basic example of an IF, ELSE statement so you can understand the concept before it gets more complicated.

IF, ELSEIF, ELSE Statements

Before I move on to a quick practical example of using an IF, ELSE statement, I want to show you one more PHP command that can be used with these statements.

The ELSEIF command works as both commands in one and can be used to make more complex IF, ELSE statements.

Here is another example that demonstrates the ELSEIF command:

<?php
$a = 1;
if ($a == 1)
{
echo 'The variable 'a' is equal to 1.';
}elseif ($a == 2){
echo 'The variable 'a' is equal to 2.';
}else{
echo 'The variable 'a' is not equal to 1 or 2!';
}
?>

This time my IF, ELSE statement actually has a total of three parts to it!

After the closing curly bracket for the IF command, I have used an ELSEIF command this time instead of an ELSE command.

Immediately after the ELSEIF command is a second test statement, which is asking if the variable $a is equal to the number 2.

Then there is another set of curly brackets for the code for the ELSEIF statement, and then I still have used a final ELSE on the end to run a third section of code when both the IF test and the ELSEIF test are FALSE.

FOR Statements

As I mentioned before, the code from these IF examples will always display the same thing because the value of the variable $a is defined as the number 1 and there is no way for it to change in the code.

There are actually a lot of different ways that variable values could be changed in your coding, depending on what you want your script to do, but I want to show you at least one of these ways just so you can see these previous examples work.

This time I am going to make use of a new command called FOR. This command is useful when you want to cycle through whole numbers, either one at a time or in specific increments.

I have provided another example below to demonstrate this command:

<?php
for ($a = 1; $a <= 3; $a++)
{
if ($a == 1)
{
echo 'The variable 'a' is equal to 1.<br />'; }elseif ($a == 2){
echo 'The variable 'a' is equal to 2.<br />';
}else{
echo 'The variable 'a' is not equal to 1 or 2!';
}
}
?>

This will output the following to the browser:

The variable 'a' is equal to 1.

The variable 'a' is equal to 2.

The variable 'a' is not equal to 1 or 2!

Instead of declaring the value of $a as the number 1 in the beginning of the code this time, I am using the FOR command.

The FOR command is actually going to be used to repeat some of my code!

Right after the FOR command is a set of opening and closing parenthesis.

Everything between these parenthesis provides information for the FOR command.

Notice the two semicolons between those parenthesis. These are command separators, like I have explained before, so these are splitting the FOR statement into three different parts, which I will explain below.

The first part should look familiar. This is the previous code that I was using for this line in the previous examples, which sets the value of $a to the number 1. This is telling the FOR statement where to begin.

The second part is the end point of the FOR statement. Here, I have used the variable $a and the number 3, but I have used a less than symbol and an equal sign here - this means just that (less than or equal to). This is telling the FOR statement to only run while this part of the statement is true.

The third part is the increment for the FOR statement. I have used $a++ here, which is actually the same thing as $a + 1. This means that I want the value of $a to increase by one each time.

Now let me put all of that into English for you - the FOR statement basically says this:

I want the variable $a to start with a value of 1. Then, I want the value of $a to increase by 1 while the value is less than or equal to 3.

Since the FOR statement loops through code depending on the values provided, this means that the FOR statement will loop a total of three times: once when the value of $a is equal to 1, once when $a is equal to 2, and once when $a is equal to 3.

As you can see from the output of all of this code, each part of the IF, ELSEIF, ELSE statement finally gets processed. This is happening one section of code at a time as the FOR statement changes the value of $a from 1 to 2 to 3.

The last thing I need to point out about this example is the additional set of opening and closing curly brackets that has been added. One is right after the closing parenthesis of the FOR statement and the other as at the very bottom of the code. These are the opening and closing curly brackets used with the FOR statement. Everything between those brackets is code that gets processed each time the FOR statement loops, so that code is being run a total of three times here.


© PHP Training Guide

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (23.08.2014)
Views: 372 | Rating: 0.0/0
Total comments: 0
avatar