What is PHP
PHP is a popular server side programming language that is fair easy to (initially) learn, but can be challenging to master. It is available on almost every web server.
PHP Language Basics
PHP code must live within it’s own special tags. This is where the server goes to get the info. If PHP code is outside of the PHP tag, it will be ignored.
There is no closing tag. Everything goes within the tag.
To write something to the HTML stream, you use the echo command, normally. example:
Notice the semicolon at the end of the line. That let’s the server know that that command is completed. This line could also be written as:
This is because PHP doesn’t care about white space. Much like HTML.
Additionally, you can use double quotes as well as single quotes. So this example is also the same.
Now, there are some differences between the two styles of quotes, however, at this level, we don’t need to worry about those differences.
Variables
Variables store information for a short amount of time (like while a page is being processed). This information can then be latter accessed to be used at a different part of the script.
Variables start with a dollar sign ($). They can be made up of letters, numbers, and underscores. For example $first_name and $firstName. While these two variables look similar, PHP treats them as independent and unique. PHP is also case sensitive so $name and $Name are different variables.
Variables can store text, numbers, dates, and more. You just have to assign the value you want to it. example:
Arrays
An array is a special type of variable. That is because it is actually a collection of variables. For example, I may want to have a list of my employees. I could store them in an array, so that each employee gets its own space.
To access an array’s unique item, called an element, you have to specify an offset. These are often a number like:
Alternatively, I can store values in an associative array. Here I will store the value against a unique name, called a key, such as:
Conditions
Condition statements allow us to check to see if something is true or false. This is normally done through an if statement. example
That is all that has to be done. If a statement is true, you put it within the braces. If it is false, it gets skipped, unless there is an else clause. If so, the else clause gets called.
Getting Info from a User
PHP has several built in special associative arrays for handling information sent to the page via a form. This is the $_POST[] array and the $_GET[] array. The form name is used as the key. example:
You can get the info from a form, and then run a condition on it to let other items work. Such as:
Example of Loading a File
There are several ways to load a file in PHP. It depends upon if you want to use the information to process later, of if you are simply wanting to display that information. For example, you could do:
// store information in a variable as a large string for later use $file = file_get_contents('./people.txt'); // do other things echo $file; // now we display the contents of the file
or you could
// automatically display contents echo file_get_contents('./people.txt');
There is also a file command, and others, if you don’t want to read the entire file into a string.
Example of Sending an Email
Sending an email is a fairly common thing to do with PHP. This is partially because it is so easy to do.
// $header is optional mail($recipient, $subject, $mail_body, $header);
An Introduction to PHP was originally found on Access 2 Learn