PHP Tutorial #1 – The Basics |
PHP scripting can be very complicated, but it can do very amazing things. One key feature that makes php so successful is its database support. For most of my websites I use MySQL (www.mysql.com). Database support allows for many possibilities. You can create things such as user/member systems, user-input forms, news systems, and forum systems. Most common forum systems such as phpBB and Invision Power Board are codedin php.
PHP has been very successful for many reasons. First, it’s free off of www.php.net. It’s also fairly easy to make simple php scripts. However, you can make your scripts as complicated as you want. Some dynamic pages are very advanced and the files required contain several megabytes of code. It’s all up to you.
Before we begin I want to make several comments. First, scripting can be a pain in the butt. You will get “parse errors” that make no sense. Just take your time and try to figure it out. If you are careful, you won’t have much of a problem. I recommend you do not copy code from websites unless you know what the code does. It is often hard combining someone else’s code with yours. Try to figure out how their code works then incorporate that into your own. Finally, there are many ways to execute the same command. I recommend sticking with the examples I use for they are often less complicated than other methods. Using the same methods on all of your work makes everything very neat, and that is key when trying to solve errors.
Let’s begin…
I recommend changing all website files on your server from .htm/.html to php. Files with the .php extension will execute normally. This may seem confusing to the newcomer so I’ll try to explain it. The PHP compiler will only read files with the .php extension. However, it will only read code enclosed between the opening/closing tags. The opening and closing tags are the following <?php and ?>. Here’s an example:
If you have a file called index.php but there is no PHP code included, the php compiler will skip the file. NOTE: Do not include the php tags when you are using html. An error will be returned.
#####index.php with no php code#####
<html>
<head>
<title>Welcome to my web site</title>
</head>
<body>
Hello World!
</body>
</html>
##### end of file #####
Now the next file is index.php with php code included.
#####index.php with php code#####
<html>
<head>
<title>Welcome to my web site</title>
</head>
<body>
<?php
echo “Hello World!”;
?>
</body>
</html>
##### end of file#####
The PHP compiler will read only the code between the <?php and ?> tags. In this case, the compiler would execute the line that reads ‘ echo “Hello World!”; ‘
The following file is index.html with PHP included.
#####index.html with php code#####
<html>
<head>
<title>Welcome to my web site</title>
</head>
<body>
<?php
echo “Hello World!”;
?>
</body>
</html>
##### end of file#####
When viewing the above file, the user would see the code itself. Because the file does not have a .php extension, the compiler will not check index.html for code.
When coding in PHP, I prefer to use Notepad of Vim. More information on this will be avaiable in the tutorial section of my website.
For the time being, you can use my PHP compiler found in the tutorial section. Enter in any example code to see the results.
Any line of code in php must end with a semicolon. There are several instances where this is not the case, but we will get into that later.
Let’s start with a sample script that will perform 1 task. We will get the script to “echo” or display a message on the screen.
## EX 1.1 ##
<?php
echo “hello”;
?>
## EX 1.1 ##
Note the script starts and ends with the PHP tags. The compiler will check and see that we told it to execute the “echo” command. Any text that follows will be echoed. However, with this simple command, there are already several things to look out for. First, make sure the words you want to echo are enclosed within double quote marks. Secondly, make sure your line ends with the semicolon (;). This tells the compiler that the command is finished. Lastly, be careful of using quote marks within other quote marks. The following line of code would be incorrect:
echo “I said “hi, how are you?””;
There are several ways around this. First, you could use a single quote.
echo “I said ‘hi, how are you?’”;
Or, if you want to use double quotes, you could do the following:
echo “I said, \”hi, how are you?\””;
Adding a “\” in front of many things such as quote marks and the $ sign will echo that actual symbol. It tells the compiler that the following symbol should be displayed, not viewed as a command or part of the script.
Now that we have the basic echo command down, let’s explore variables. Variables are tools that allow content to change based on the situation. For example, if you were making a login script and you wanted the site to read “Hello, (name)”. You could do this by using variables. I will explain that in a later tutorial. For now we will go over variable basics.
In order for a variable to be used, it must be defined. Defining a variable is quite simple. The following line will add the phrase “Hello World!” to the variable.
$sentence = “Hello World!”;
You can use as many variables as you want and you can name them whatever you want. Be careful not to use the same variable name for two different purposes in the same script. Now that the variable is defined, we can use it for many purposes. In this case, I want to echo the variable.
echo $sentence;
You do not need quote marks to echo the variable itself. However, if you want to echo the variable as part of a sentence you do.
echo “The man said \”$sentence\””;
In the above line I combined several things. First I used a variable. Secondly, I used the \” to echo a quote around the variable. If the above was executed, the user would see the following:
The man said “Hello World!”
You can use variables for many things. If you want to do addition, subtraction, multiplication, or division you could assign numbers to the variables. If you are assigning a number to a variable, you do not need quote marks.
$1 = 1;
$2 = 2;
You could use quotes if you wanted. Now, let’s do some math.
$answer = $1 + $2;
echo $answer;
The above would spit out “3″;
You can do longer equations such as:
$answer = $1 * $2 / $2 – $1;
As with the above, you do not need quotes.
If you want to display the actual equation on the screen you would need to use quotes and \.
echo “$1 \+ $2 = $1 + 2″;
The above would echo:
1 + 2 = 3
When the compiler reads the code, it notices that it should echo the first + sign. However, since the second plus sign is not preceded by a \ it views that as a function. There are several ways to do the above. You could do:
$answer = $1 + $2;
echo “$1 \+ $2 = $answer”;
Or, you could do the following. Note, you cannot echo two variables in a statement without the use of quotes.
$phrase = “$1 \+ $2 = “;
$answer = $1 + $2;
echo “$phrase$answer”;
Note, you can run two variables one behind the other without a space in between. Had the phrase quote ended with the = sign, I would have added the extra space when I echoed. Example:
$phrase = “$1 \+ $2 =”;
$answer = $1 + $2;
echo “$phrase $answer”;
Tutorial Written by Christopher Airey. Chris works for ReadySetConnect as the lead server administrator. Looking for a php web hosting provider. Try ReadySetConnect Web hosting.

(No Ratings Yet)







Leave a Reply