Introduction to PHP: Part 1
PHP, which stands for "Hypertext Preprocessor," is a programming language used mainly by server-side applications. For example, when you register or log into a service online (such as email) the server you're browsing on is likely using PHP. Overall, PHP can used for, but not limited to, database access, simple graphics generating, or online file access.
Unlike HTML and Javascript, PHP can not be run on any old PC. You must have some form of server software to simulate what your PHP code is going to do. A simple and easy software package, such as EasyPHP, will do just fine. The scope of this tutorial is learning about PHP, not on how to run it, so I won't tell you how to install PHP on your computer. If you're wondering how, most server packages come with extensive documentation on how to install the product.
So, lets dive into some code. To write the code, all you need is a simple text editor such as Notepad. A personal favorite of mine is called Programmer's Notepad. Open your text editor and then we can begin.
Example 1: Echo
Code: (click here to see what is does!)
<?php
echo "Hello, world!";
?>
Explanation:
This is one of the most simple PHP files. The <?php tells the server that "PHP code starts now." The next part, echo "Hello, world!";, tells the server to display something on the screen, much like just typing Hello, world! out in an HTML page. The quotes denote that the server is supposed to out put a string. A string is a plain old line of text. I'll will tell you later what other things echo can print out.
The semi-colon at the end of the line is a very vital part. at the end of each PHP command, you must add a semi-colon.
Finally, the ?> part tell the server that "PHP code ends here".
Example 2: Variables and Comments.
Code: (click here to see what is does!)
Explanation:
Alright, so the opening PHP code tag is first. We already know what that means. Next is $number1 = 300;. What this is a declaration of a variable. In this case, the variable is a number. The $ sign tells the server that "Here comes a variable name." number1 is the name of the variable. In your own programs, you can name variable anything you want, just as long as you keep the name free of spaces. Next, is the 300. This means that the variable number1 has the value of 300. Remember to end the statement with a semi-colon.
Next, on that same line is a comment. Comments are normally used by programmers to tell anyone who is reading the code what that line does. A one line comment starts with two forward slashes (//). The way the server knows this kind of comment is ending, is that a new line starts.
The next line declares another variable, called number2, with the value of 25. Then comes along another one line variable. Next comes yet another variable. But, as you might notice, this one is a bit different that the previous two. This third variable is a string. As I said before, a string is a plain old line of text. In this particular example, this string doesn't contain any text per se, but actually a line of HTML. So, breaking it down, the string1 variable is being declared with the value of <br />. Next is another one line comment.
The next line is a string variable, called string2, which has a value of <b>Yippee! Strings are fun!</b>. This string not only has HTML in its contents but also some text. If you know HTML, this is making the Yippee! Strings are fun! into bold font.
Next is an easy part. This is a multi-line comment. This can be used for whatever you want, but I mostly use it for an introduction to a PHP document or instruction. To start a multi-line comment. Just type in /*. Then add anything you want. Such as PHP examples, instructions, or whatever.
Next, we're actually outputting some stuff for the user to see. As you should remember, echo tells the server to output something on the screen. Unlike the first example, we're saying to echo the contents of the number1, string1, number2, string1 (again), and string2 variables. Instead of saying exactly what it's supposed to output, we're saying go check out these variable and output their contents. Pretty simple, you might say.
Lastly, the ?> command is ending the PHP script.
Example 3: Math and Conditional Statements
Code:
<?php
$first = 200; // Here, I'm declaring some variables.
$second = 45;
$third = 5;
$addition = $first + $second; // Hopefully, you know that $addition = $second + $first would od the same thing!
$subtraction = $first - $second;
$multiplication = $second * $third;
$division = $first / $second;
$easier1 = ( $first ) / ( $second - $third );
$easier2 = $first / $second - $third;
$harder1 = ( ( $second + $third ) * ( $third ) ) / ( $second + third );
$harder2 = $second + $third * $third / $second + third;
echo $first;
echo "<br />";
echo $second;
echo "<br />";
echo $third;
echo "<br />";
echo $addition;
echo "<br />";
echo $subtraction;
echo "<br />";
echo $multiplication;
echo "<br />";
echo $division;
echo "<br />";
echo $easier1;
echo "<br />";
echo $easier2;
echo "<br />";
echo $harder1;
echo "<br />";
echo $harder2;
echo "<br />";
if ( $third == $second ) { // Notice that for an equal operator, you use two equal signs ( == )
echo "5 has the same value as 45!";
} else {
echo "5 does not have the same value as 45!";
}
echo "<br />";
if ( $easier1 >= $easier2 ) {
echo "The easier1 variable is greater than or equal to the easier2 variable";
} else {
echo "The easier1 variable is not greater than or equal to the easier2 variable";
}
echo "<br />";
if ( $harder1 != $harder2 ) {
echo "The harder1 variable is not equal to the harder2 variable";
} else {
echo "The harder1 variable is equal to the harder2 variable";
}
echo "<br />";
if ( $harder1 == 125 ) {
echo "The harder1 variable is equal to 125.";
} else {
echo "The harder1 variable is not equal to 125.";
}
?>
Explanation:
First, we open the PHP script with <?php. Next we declare the variables first, second, and third. Next comes something new, at least partly. What we're doing on the next four lines is declaring four variables with values extrapolated from the previous three variables.
First, for the addition variable, we add together the values of first and second. This comes out to be 245, just so you know. Next, for the subtraction variable, we subtract the value of second from the value of first. For the third line of that segment, we multiply together the second and third variables and make that the value of the multiplication variable. Lastly, we divide first by second and assign that to the division variable.
This next segment demonstrates the use of parenthesis. The easier1 variable will not same value as easier2, despite the fact that the only difference is the (lack of) parenthesis. The same goes for harder1 and harder2. The reason you need to add parenthesis is because the server reads a math problem from left right. So, for example, it reads easier2's declaration as "Take the value of first and divide it by second. The answer of this is 2.2222. Now take 2.2222 and subtract the value of third. This is -0.5555. Now we set the value of easier2 to -0.5555." This is not what we want, so we must clarify for the server and add parenthesis.
The next (long) segment is printing out the values of all the variabes using echo.
The last part of this example are if else statements. To begin an if else statement, first is the word if. This tells the server "Here comes a statement where we have to compare to things." After the if is the condition. In the first case, the condition is ( $third == $second ). This is testing whether the third is eqaul to second. The signs in the middle, in this case ==, is called a comparison operator. For a list of all the operators in PHP, click here. == tests if the two variables being compared are equal.
The next part of the if else statement is the the actions the server is supposed to do if the comparison is true. In the first if else statement's case, the server will echo 5 has the same value as 45! If the comparison is false, then the server will echo 5 does not have the same value as 45!
The next two if else statements use the same concept, except the second tests if the first variable is greater than or equal to the second variable and the third tests is the first variable is not equal to the second variable.
Finally, the last if then statement is comparing a variable (in this case harder1) to a number, unlike the last two in which variables are being compared to variables.
Lasty, the ?> ends the PHP script. |