Do you want to learn How to Code PHP? Get your thinking caps on and let’s start cranking the PHP Engine.
PHP is a programming language that allows you to process and react to user actions on your website. While JavaScript allows you to process actions directly on a visitors computer, PHP requires that actions are processed on the server in which your website resides.
PHP is most commonly used to access databases on the server that hosts your website. Please read my SQL Tutorial before you read this article, or you may get lost. This tutorial will cover most all you can accomplish with PHP, but will very specifically cover how to interact with a database with PHP.
The Basics of PHP
Like my other tutorials, I will provide you with working PHP Sources you can use, but for now I’ll focus on the basics.
You can embed PHP code directly in your web pages by surrounding the code with an opening <?php and closing tag ?>. You also want to give your web page the PHP extension .php, when you embed PHP code in a web page.
Variables
You define variables, storage areas with a defined name, by starting the name with a dollar ($) sign and then a name. The first character following the dollar sign must be a letter or an underscore(_). There after you can use a series of letters, numbers, or under scores. It is considered good for to separate multiple words in a variable name with underscores. Ex:
You assign values to the newly created variable with the equals operator (=). $my_name = “New Think Tank”;
If you want to define a variable with a value that will never change (Constant), you would do that with the define() function. define (‘pi’, ‘3.14’);
Printing Information in the Browser
You can easily output text to a web page by using the echo or print functions. Here is an example:
or
You can use single or double quotes with both of these functions, but the data contained between them is handled differently.
Escaped Characters
Sometimes you are going to want to echo text to the screen that is protected. If you want to echo a quote ( ‘ ) to the screen, you may confuse the browser if you place that quote between two other quotes. The browser won’t know when you text block ends. So, how do you use these protected characters? You follow them with a back-slash ( \ ). Here is a list of protected (escaped) characters:
Now you can place these protected characters in your double quoted strings, for use with the echo and print functions. Just remember these only work when you use double quotes
Commenting
It is a very good idea to start your code with a full description of what the program does. You should then follow up by writing comments that describe each function and any statements you might find confusing a year from now. This is so that you and others will be able to easily jump in years later to make any changes that are needed. Believe me there will be changes and you will forget!
You can comment out notes on your code for a full line by proceeding the comment with two forward slashes ( // ). If you need to make a multi-line comment start of with a forward slash, star ( /* ) and end the comment with a star, forward slash. You can also comment out a single line with the pound symbol (#), but this isn’t often used.
Just so you are aware, you create comments in HTML, with a starting <!–, and end it with a –>. For a full tutorial on HTML see my HTML W3C Tutorial.
Strings
Strings are just a quoted chunk of letters, numbers, punctuation and white space. The information you were outputting to the screen with the echo and print functions were strings. You create and assign strings to variables with the equals operator ( = ).
You can join two strings with the dot (.) operator like this:
If you want to find the number of characters in a string, you can use the strlen() function. $chars_in_string = strlen($location); The value returned would be 17, the number of characters in ‘Chicago, Illinois’.
I’ll show you some other useful string functions later in the article.
There are numerous functions you can use to work with numbers in PHP. You define them in the same way as other variables: $pi = 3.14; You can also perform these basic operations on them:
PHP Math Functions
PHP has many built in math functions. Here are the most commonly used:
Arrays
An Array is a variable that can hold multiple separate values. If you think of a variable as a box that holds one value. An array is like a big box with many boxes with values in it.
Arrays are structured as a series of key value pairs. The key part is like a note on a box that says Underwear. In the case of an array you could give each box a name like Underwear and other things like that. By default the first value you enter has the key value of 0 and proceeds upwards numerically.
You assign a value to an array like this $dogs[3] = “German Shepard”; Now the 4th box in the array has the value “German Shepard”. Remember the boxes start with the value of zero that is why the 3rd box is actually the fourth box. Get it?
To output the value of an array: echo $dogs[3]; That’s it.
You can cycle through and output the values of an array with the foreach function, like this:
foreach ($dogs as $dog_name) {
echo $dog_name;
}
The foreach function will keep adding one to the value of the array and then the echo function prints them to the screen. In this example the name of the array $dogs is followed by the keyword as and then the variable you want to temporarily use to hold the current value being issued by the array $dog_name. When placed in the foreach function, every statement called between the opening and closing curly braces { } will be called.
Multidimensional Arrays
You could also create arrays of data inside of other arrays. Let us say you want the dogs array to be part of your animals array. This is how you would do that:
$animals = array ( ‘DOGS’ => $dogs, ‘CATS’ => $cats);
Here we assigned keys to two arrays named $dogs and $cats and then passed them into the array named $animals. If I wanted to echo a value from the $dogs array I’d do it this way:
echo $animals[‘DOGS’] [3]; // This would print out the value German Shepard, that I assigned to it above.
Working with Arrays and Strings
It is common to use Arrays and Strings together, so here are a few commonly used functions :
explode(separator, $string): Turns a string into an array. You define what character in the string you want to separate the values that will be inserted into the array. Then also pass the string to separate. The separator is normally a comma, space, tab, semicolon, or other white space or punctuation mark.
implode(separator, $array): Turns an array into a string. Here you define what you want to separate the array values that will go in the new string. The separators used are the same as I just described.
Sorting Values in Arrays
It is also common to sort your array values. Just understand that when you do your keys will stay the same and just the values will be sorted. That is why most people allow their keys to remain the default numbers, like I described above.
The sort($array) function will sort the values in ascending or alphabetical order. The rsort($array) will sort the values in the opposite direction.
That’s All Folks
We’ll we came to the end of my first PHP tutorial. If you have any questions leave them in the comments section below. Prepare yourself for a ton of code examples in future articles.
Think Tank
Great information! I’ve been looking for something like this for a while now. Thanks!