PHP Lesson #1:

Lesson 1 - The constant echo of variables


In this lesson, I will try to explain to you how variables work in PHP, and how you can use them to your advantage. This tutorial assumes that you have a background in programming and are familiar with structured code, comments, and code syntax.

Step 1 - index.php
            To begin, you need a web server capable of processing and displaying PHP pages. The most common for this is the Apache web server (http://www.apache.org) compiled with PHP support (http://www.php.net). In later lessons you will want to have MySQL support as well, so if you are setting up your server now, do it with MySQL in addition to PHP. MySQL can be found at http://www.mysql.org
 
I will not go into the How-To of setting up a server in this tutorial, as there are many great resources available on the web. Just check Google or your favorite search engine for it.
 
For our first file, we will create a folder in our web root, typically /var/www/html on Linux machines, called less1. This folder will be accessible in a web browser at the url http://www.myserver.com/less1/
 
Now, in your favorite text editor, such as notepad or WordPad, create a plain text file and save it in the folder you just created as index.php this will be the first page to be displayed by default.
 
Step 2 - $myvar1 = 'your mom';
              In your file you just created, type out the following lines of code:

<?
     $myvar1 = "your mom";

     $myvar2 = "my mom";

     $myvar3 = "the store";

     echo($myvar1." and ".$myvar1." went to ".$myvar3);

?>  

            What does this mean? Good question. In almost all programming languages, you have to identify where you will be storing information. In lower level languages, such as assembly, you need to specify the exact address in memory. With higher level languages, such as C, VB, and in this case, PHP, you can simply give that memory location a name, and the compiler will take care of the rest. Variables are used to store temporary information, no more, no less.  

            Usually, you will find it easier to create a variable and give it an initial value at the same time, which is what has been done in the code that you just typed out. The variable names in PHP always begin with the dollar sign ($). There are some variables that are pre-defined, which we will cover later, but most generally you can call your variables anything you want.  
In this lesson, we are covering local variables. Local refers to the scope of the variable, or where in the code it can be accessed. In most programming languages you have the option of local and global variables. In PHP, you have the option of local, global, and superglobal. Local means that any code that is currently active, whether it in the current file, another file that is included in the current file, or not inside a function, can access this variable without special steps needing to be taken. We will cover includes and functions in later lessons.
 
Back on the thought of creating variables, you will notice that the variable name is on the left, and the value it is receiving is on the right. This will almost always be the case regardless of what programming language you are using. Typically, when you create a new variable, there are 3 steps involved:


1)      Name the variable

2)      Set the data type

3)      Set an initial value

 
We have covered steps 1 and 3 already, but what happened to step 2? In most programming languages, you would perform step 2 right away, but in PHP it seems as though we just ignore this step. As you should know from your previous experience with code, there are 4 basic data types:
 


·        Integer        -           Whole numbers, positive and negative

·        String         -           Text or numbers treated as text contained in quotes

·        Float          -           Numbers containing decimals, positive and negative

·        Boolean      -           True or false
 

In PHP, the data type is assumed by the type of data you assign to a variable. If you initiate a variable such as the one in the example below, you would be creating an integer variable.
 
$myVar1 = 12;
 
In the next example, it appears as though we've assigned the same data type to the same variable, but look closely. Since the value being assigned is contained in quotes, the variable becomes a string variable.
 
$myVar1 = "12";
 
As we go along with this series of lessons, the various data types will be used and explained. For now, it is imperative that you understand what string variables are.
 
You will notice that after we complete a statement, we have to include a semicolon. This simply tells the compiler, or in this case, the processor, that the current statement has ended. This will not always be required, which you will also see later. For now though, make sure you end your statements with a semicolon.
 
Step 3 - echo($myvar1);


            No, this doesn't have anything to do with sound waves bouncing around, it has to do with what happens to the contents of our variables. Taking another look at the code, it is actually very comprehensive, rather than being a garbled up mess.
echo($myvar1." and ".$myvar1." went to ".$myvar3);


            There are actually a lot of things happening in this one line of code. A key principle to programming is the joining of two variables, known as concatenation. Concatenation, aside from being a rather cumbersome word, simply refers to putting two objects together, in this case strings. Each language has its own way of concatenating, but PHP's method is to use the period character. To concatenate $myvar1 and $myvar2 and store the result in $myconvar, you would need the following code:

            $myconvar = $myvar1.$myvar2;
If you look at the code again, you will notice that we have strings that aren't even assigned to variables. This is acceptable in almost all cases, but for more complex situations may be more of a hassle. For our little bit of code it is fully acceptable.
All this mess is contained in the parenthesis for the echo function. The echo function simply displays what is contained in its parenthesis on the screen. If you were to have a line of code, such as echo("hello george");, it would simply display hello george on the screen. In our case, the following will be displayed when you go to the path on your server that you created earlier, http://www.myserver.com/less1/:

your mom and my mom went to the store


Conclusion - What the heck was all that?!


            In conclusion, lets look at what we did with this code:
-         Create string variables and assign values
-         Concatenate string variables and string values
-         Display the result of the concatenation


If you had trouble following this lesson, by all means, feel free to post a message on the PHP forum at http://wildebeest.dynup.net/tim/. In your posting, please try to be as specific as possible when describing your problem, whether it was understanding the code, or if you received an error when you tried running the code. Be sure to check the site regularly for further lessons where we will go deep into the depths of PHP.