Overview
PHP is a Hypertext Preprocessor, or in layman's terms, general-purpose scripting language that was originally designed for web development to produce dynamic web pages. It is a server-side scripting language that executes the scripts on the server. A php file itself can contain text, HTML tags, and various scripts, and are returned to the browser as plain HTML. In that way, PHP is essentially hidden in the backbones of your website.
Basic Setup
A PHP script always starts with <?php and ends with a ?>. A PHP file normally consists of HTML tags, and some PHP scripting code. Each line in PHP must end with a semicolon. When saving the file, you must save it as .php, if it is saved as a HTML file, the PHP code will not be recognized.
Variables in PHP
Variables are used for storing values, like text strings, numbers, or arrays. Once a variable is first defined in your PHP, it can be used over and over again throughout the file. All variables in PHP start with a $ symbol. A Correct example of using a variable in PHP is: $var_name = value;
A very common mistake for new PHP programmers is that they forget to add the $ sign, which causes the PHP code to not work.
Notes about Variables
-
A variable does not need to be declared before adding a value to it
-
PHP automatically converts the variable to the corresponding data type, depending on it's value
-
In a strongly typed programming language you have to define the type and name of the variable before using it
-
A variable name must start with a letter or an underscore
-
A variable can only contain alpha-numberic characters and underscores (a-z, A-Z, 0-9, and _ )
-
A variable name should not contain spaces, if the variable name is more than one word, it should be separated with an underscore
Uses
There is so much to PHP that there is too much to summarize in one page, but this page will give you a good idea of how to start understanding and using PHP. PHP has many uses to cover, but here are a few key examples:
Database Access: Using PHP with MySQL has become common enough that the MySQL interface is now part of core PHP instead of a plugin, that means PHP can access most SQL or ODBC database. This opens the door for a variety of online business applications that require data storage on the server. For that reason, PHP is becoming more widely used in the world of e-commerce.
File Access: PHP has the ability to read and write files, and it can do basic file and directory maintenance. Through this you can use PHP to such things as edit documents remotely. PHP can also take content and use it to generate files in various formats. PHP's ability to work across multiple data sources and return multiple content types makes it a great tool for things like search engines and message boards
Application Control: PHP was started as an application control scripting language, and it was designed to handle acess logging for HTTP servers. PHP even has it's own use in programs like Microsoft Word or Excel.
Graphics: PHP does more than manage text content, it also manages graphical content. PHP is able to design graphs and charts, and even generate GIF and PNG images.
Potential: PHP is written in C, and can be broadened upon freely by others, because it is open source. The definition of 'open source' is " of or relating to or being computer software for which the source code is freely available." This means internet users from all over can bring their own ideas into PHP and generate scripts, then share them with the rest of this world. In this way, the possibilities of PHP are endless!
How I used PHP
- Link to my Site
- Link to (one of) my PHP files ← This file belongs to this
*note, you will not view my file with all of it's images, focus on the code itself
As you can see in my PHP file (results1.php), I implemented a matching quiz script into my site. Through the use of PHP, readers on my site are able to quiz themselves on vocabulary thanks to a useful script I found through Jake Lavery's site.
As you can see, at the very top of my PHP file I have my script. This is where I go in and edit the script to make it match the file "matching.html." How this works is I go to the matching.html, and I look at question one, then I find the letter that gives me the correct answer. I then go to my PHP and where it says "if ($_POST['q1'] == 'd')" I make sure that q1 = d (Question one equals D). Since this is correct, I get the answer right and get points for getting the answer correct. I then have to go through q1-q20 and make sure all the questions and answers are lined up.
The other PHP scripting I have in this file is the grading. After filling in the answers, I have a link that will send the user to the next page, and PHP goes through the answers the user submitted, and gives them a percentage of what they got right over what they got wrong. Since I have 20 questions, and they are all the same point value, each of them is worth 5% (20 x 5% = 100%). In my PHP I defined the grading, which means 1 correct equals 5%, 2 correct equals 10%, 3 correct equals 15%, and so on. I also have a message that will be displayed depending on how many you got right. Examples of this include: Try harder next time, You can do better, and Well done! This scoring PHP code I have is what displays the grade to the viewer. Try the matching on my site!
Works Cited
Kantor, Peter L. "PHP: Uses of PHP." What Can PHP Do? Jan. 2003. Web. 24 Jan. 2011. <http://www.daaq.net/old/php/index.php?page=uses of php&parent=what is php>.
"PHP Tutorial." W3Schools Online Web Tutorials. Web. 24 Jan. 2011. <http://www.w3schools.com/php/>