Here is a sample script, we will dissect it afterward:
----------
<?php
//We will set up our variables first
$name = "Josh";
$age = "14";
//Next, we will use our variables.
print "Hi, my name is $name and I am $age years old";
?>
----------
1. In line one, we open our PHP with the |<*?php| open tag.
2. In line two, we use '//' to signal internal notes. This is an important feature of PHP that many programmers use to keep notes while they write their script. Anything that is on the same line as '//' will not be show in the browser once the page has loaded. It will however, stay in the source code. Use this feature to your advantage as it can help keep your script in order.
3. In lines 3 and 4, we set up our variables. You know these are variables because of the money sign ($) in front. In line 3, '$name' is our variable, and 'Josh' is the value that is assigned to that variable. Respectively, in line 4, '$age' is our variable, and '14' is the value assigned to that variable. The variables act as substitutes for their values. Toward the end of this tutorial, you will find just how much easier variables can make things.
4. In line 5, we have another internal note that lets us know what is going on.
5. In line 6 is where we will put our variables to work. At the beginning of our string we have the word print. Print is the PHP function that tells the browser to show the value that is contained within the print function. In the example above, print is used like this:
print "Hi, my name is $name and I am $age years old";
You will notice that we have use $name and $age in the print statement. Once the browser has taken the print function and processed it, the final output in the browser of the viewer should read:
Hi, my name is Josh and I am 14 years old
Can you see how the print function works together with the variables to output the statement? Another thing you should know about print is that it can be used in place of the echo function. The echo function and the print function are the same basic funtion, but with two different names.
print "Hi, my name is $name and I am $age years old";
echo "Hi, my name is $name and I am $age years old";
The two strings above will both output the same thing. Print and echo can be use interchangeably.
6. Finally, in line 7, we end our PHP script using the '?>' tag.
How does all of this help you? Well, PHP is a language that helps a webmaster and/or programmer the ability to be more interactive with his/her users. When PHP is combined with mySQL, they create a powerful programming force that can help carry a website to a user interactive heaven. Variables help make this task easier. An example for the use of variables is a login system. When you go to a website that asks for your username and your password, that website probably uses PHP variables to make sure that your username and password are valid. Another example is form mail. If you make a page on your website for users to send you an email from the web browser, you can set up a form and use PHP variables to help carry the data along. A standard form e-mail would look something like this:
----------
Name: Josh
E-Mail: [email protected]
Subject: Testing PHP Form
Content Of E-Mail: I am learning how to use variables to my advantage, can you please send me a tutorial? I would really appreciate it, thank you!
----------
In this form, I entered all of my data. My PHP source code would set the Name entry box as '$name'. Similiarly, it would name the E-Mail entry box '$email' and the Subject entry box as '$subject'. The content of the e-mail might be named '$content'. Now, when somebody clicks submit, it will process all of the data and it will take those variables from the script and send them to my email as the value of those entry boxes. Get it?
Another thing you should know about php is that it is very strict on the rules of beginning and ending strings. What if you wanted to print something out to the browser that contained quotation marks? As goes for everything else, PHP has a solution for this minor problem. It is called "Escaping." Here is an example:
----------
<?php
print "I like to say "Easy Tutorials is the best"";
print "I like to say \"Easy Tutorials is the best\"";
?>
----------
In this example script, the first string will not work. Why? Since, a double quotation mark begins the value of the print funtion, the PHP will look for a second double quotation mark to end the value of that funtion. In the first string, the script tells the PHP to end its output after the word 'say.' You see the '\' before each of the double quotation marks in the second string? In the second string, we are escaping the quotation marks that we want the browser to print. Escaping is done by putting a \ before symbols that you want to escape. If the symbol is not meant to be part of your PHP script, you will want to escape it. There are exceptions to the quotation rule though, here are a few more examples.
----------
<?php
print "I made approximately \$0 dollars to write this tutorial";
print "I like to say 'Easy Tutorials is the best'";
print 'I like to say "Easy Tutorials is the best"';
?>
----------
In this sample script, the first string has escaped the money symbol. In this string, if the money symbold is not escaped, then the PHP will think it is a variable that needs to be processed. If the PHP attempts to process this variable, you will get an error in your browser because the PHP can't find a value for the variable '$0'.
In the second string of the sample script, we have begun the value of the print function with a double quotation mark (") and begun the quote inside the value with a single quotation mark ('). This is fine as long as the quotation marks do not conflict. as long as you begin and end the value with the same type of quotation mark (A quotation mark that is different than the one used in the quote within the value), then the string will be processed fine. The third string helps emphasize the example set by the second string, except the quotation mark types have been reversed.
Other things to remember about PHP include:
1. A file containing PHP script must end with the '.php'. If the page 'samplescript' has PHP in it that needs to be processed, then it must end in .php. If it doesn't, then the browser will not process the PHP.
Ex: http://www.example.com/samplescript.php
2. HTML can be integrated with PHP. It is very important sometimes that your PHP and HTML be used inside one another.
Ex: <?php print "<b>w00t, Josh rocks</b>"; ?>
3. There is a difference between using single quotes and double quotes at the beginning of your functions. Not understanding this concept can lead to much frustration so make sure you get this. Here is an example:
----------
<?php
$dir = "images";
$handle = opendir '/home/user/files/$dir/';
?>
----------
Single quotes will process directly to: /home/user/files/$dir/ instead of: /home/user/files/images/
How do you make it work properly, you ask? Put it in double quotation marks. In order for this string to be processed correctly, the following must be scripted:
----------
<?php
$dir = "images";
$handle = opendir "/home/user/files/$dir/";
?>
----------
4. PHP also allows for your strings to be broken up into multiple lines. This can be very useful when you are organizing your HTML tags such as for indenting tags used in table.
Ex: print <p>"
This is a
multi-line
string</p>
";
5. In many tutorials, you will see reference to 'The Manual'. The Manual is basically just what it is called... A Manual. It can be found at http://www.php.net. You can find everything about PHP here.
I hope this tutorial has been proven helpful to you.