Part 2: PHP Basics - Writing Scripts, Viewing Scripts, and Storing/Retrieving Variables
This part will deal with the basics of PHP. In this lesson, you will learn how to write PHP scripts, view them on the server, store variables, and retrieve variables.
First, let's talk about what exactly PHP is. PHP is a server-side scripting language. It stands for PHP Hypertext Preprocessor
(yes, it's a recursive acronym). The concept behind PHP is very simple. First, the script is written and a .php extension is added to the file. Then, when a client requests the file, the server recognizes the extension of the requested file. The file is then interpreted by the server, then output to the client. This means that the code contained in a PHP file is essentially "compiled" before it is output to the client, and the code is not viewable. However, the server does not do this to all the text in the PHP file. Instead, it only interprets the text that is surrounded by PHP tags. Therefore, it is possible to insert PHP code anywhere in the page. As long as the text is surrounded by PHP tags then it will be interpreted by the server.
PHP tags look like the following:
//PHP code goes here
tag tells the server that PHP code follows, and the ?>
tag tells the server that it's the end of the PHP code. The //
characters inform the server that that line of text is a comment, and that text is not interpreted by the server. In PHP, the server does not interpret single lines of code. Instead, it reads in the text until it reaches a semicolon ;
. Therefore, statements may be more than one line, but must be terminated with a semicolon. Otherwise, PHP will report an error.
Before we start, let's cover the basics of variables and output in PHP. If you have experience with other programming languages such as C, C++, C#, etc., you know that when you declare variables you need to declare a variable type. Well, you will be delighted to know that while PHP has this ability, it is not necessary. PHP determines the optimal variable type based on the data being passed to the variable.
I will cover more on variable types, changing variable types, operators, and control structures in the next lesson. For now, let's look at how we declare variables and retrieve variables. After that, we will create a script, upload it to the server, and view the output.
All variables in PHP are preceded by the $
sign. PHP variable names may only consist of numbers, letters, and underscore characters (A-Z,a-z,0-9,_). Underscores and capitalization may be used to make variables with multiple words easier to read (ie newVariable, new_variable). The following code declares multiple variables.
$intro = "Hello World!";
$variable1 = "This is";
$variable2 = 1;
$variable3 = "new ";
$variable4 = "variable.";
In the above code, 5 variables are declared. Each of these variables has a different value. But what good is storing values if we can't retrieve them, right? To printout a variable, there are a couple different methods. For this lesson, we will use the echo
command. Observe the following code.
$variable = "This ";
$variable = "is ";
$variable = "the output.";
This code would output This is the output.
on the screen. The echo command prints out the variable in the line. It does not start a new line. To write a new line, you must use HTML and the <br>
tag, which forms a new line. PHP can be written in line with HTML, as the following code demonstrates.
<title>My PHP Script</title>
$variable1 = "Hello ";
$variable2 = "World!";
This will print out Hello
on two separate lines. When using the echo
command, you can output values together using a period between variables. This tells the server to print each variable. We will use this in the following script.
Now let's create our first PHP script. First, load WampServer and make sure the icon in the system tray is white, indicating all processes are loaded. Open Notepad++ and type the following into the text editor.
<title>My PHP Script</title>
$variable1 = "This is the first PHP script.";
$variable2 = "It is good.";
Now you have to save it to your www directory, which is where WAMP looks to load files. Left-click on the Wamp icon and select the www directory
option. This will open an explorer window and show where your www directory is located. This is typically "C:\wamp\www". In Notepad++, click File->Save As
. Navigate to the www directory and type lesson2 in the filename box. In the Save as type:
dropbox select the PHP option and make sure the extension is .php. Now click Save
Now, left-click on the Wamp icon and select the localhost
option. This will open a browser showing you your local server. Now, click the address bar and type lesson2.php at the end of the URL and press Enter (the URL should be http://localhost/lesson2.php
The following should appear:
This is the first PHP script.
It is good.
There you go, you have created your first PHP script. This concludes Part 2. The next part will deal with variable types, changing the variable type, operators, and control structures.
NOTE: I have attached the .php file so you can see it.