Getting a basic familiarity with PHP is not difficult, but if you don’t understand a few key elements you will find it difficult to work with. This beginning PHP tutorial attempts to break down a few of the core PHP programming elements through simple examples and descriptions.
Beginning PHP is a two part series. In this tutorial we will go through the different types of PHP variables (strings, numeric’s and arrays) and show you how to work with them.
Beginning PHP Tutorial Assumptions
It is assumed that the reader has a basic understanding of HTML. Previous programming experience is a huge plus, but it is not a requirement. If you have any kind of programming background, you should already know a lot of these core concepts.
Why Learn PHP?
PHP has grown incredibly fast over the last 10 years. It has a larger market share than any other web specific programming language. It is becoming more popular among enterprise solutions, which makes it a more large scale option.
One difference between PHP and other languages is that it can be processed inside of HTML. This makes it very convenient. It also is much less restrictive compared to languages such as Java or C#.
So let us jump into the very basics of the PHP programming language.
What are PHP variables?
Variables store data. You can look at them as data containers. A variable is like an identifier for a data container. Here is an example of creating three separate variables.
<?php $variable1 = 'data1'; $variable2 = 'more data'; $variable3 = 'additional data'; ?>
Line #1: You will notice the text “>?php”. This is required when writing PHP code. It tells the PHP engine that you are now writing PHP code.
Line #5: The script ends with “?>”. This tells the engine that you are done writing PHP code.
Simple PHP Example Page
Now let us see how PHP interacts with HTML.
<?php // Define the page title... $pageTitle = 'Test Page'; // Define the welcome message text... $welcomeText = 'Welcome to my website!'; ?> <html> <head> <title><?php echo $pageTitle; ?></title> </head> <body> <?=$welcomeText?> </body> </html>
Line #2 and #5: These are PHP comments. You can use comments to give additional meaning to your code.
Line #3: We create the variable “pageTitle” and assign the text we want to use. The same thing goes for #6 and the welcome text.
Line #7: We close PHP with “?>” because we are done programming in PHP (for now).
Lines #8-#19: Notice how this contains the html template for the page. Also see our PHP variables are printing to the screen using different methods.
Line #11: This is how you would print out a PHP variable when you are writing PHP code.
PHP Variable Types
There are three types of variables in PHP: Numeric’s, Strings and Arrays. In the previous examples, we have only been working with strings.
#1. Numeric’s: Integers and Doubles
These variables handle numbers. Integers are whole numbers, while doubles are floating point numbers. When working with integers and doubles, you do not have to enclose them with single or double quotes.
<?php $number1 = 1001; // this is an integer $number2 = 10.99; // this is a double ?>
Strings are characters (which can include numbers). Unlike integers and doubles, you have to enclose these values with single or double quotes.
<?php $string1 = 'This is a string!'; $string2 = "This is also a string, but it is #2."; ?>
Keep in mind that with double quotes, you can print out variables inside of the quotes.
<?php $string1 = 'This is a string!'; // Include $string1 inside of $string2... $string2 = "String1 equals: \"$string1\"."; ?>
With $string2 we used double quotes so that we could include the variable inside the quotes. We had to backslash (\) the quotes around $string1 because PHP will think you are ending that string value (when in fact we want the double quotes to be a part of the string.
Arrays are a little trickier to understand. They are variables that can contain an unlimited amount of variables inside of them. So envision a huge box that you can place many small boxes inside of. And then picture fitting smaller boxes inside of those boxes, etc…
Arrays are a key element when working with PHP as they can save you a lot of time if you know how to work with them. Take a look at the following array:
<?php $array1 = array( 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' );
Arrays are made up of key/value pairs, separated with a comma. You can create an array by using the array() function. Keys can be string values or integers. The value that you assign to each key can be a string, integer/double or even another array. Take a look this array that goes three levels deep.
<?php // This array goes two levels deep $array1 = array( 'key1' => 'value1', 'key2' => array( 'key2-1' => 'value2-1', 'key2-2' => 'value2-2'), 'key3' => 10 ); // This array goes three levels deep with Andrea $contacts = array( 'Bob' => array( 'phone' => '444-444-4444', 'city' => 'Helena', 'state' => 'MT'), 'Andrea' => array( 'phone' => array( 'cell' => '555-555-5555', 'home' => '554-444-5555', 'work' => '454-333-3333'), 'city' => 'Missoula', 'state' => 'MT'), 'Joe' => array( 'phone' => '444-444-4444', 'city' => 'Helena', 'state' => 'MT') );
Arrays are one of the most difficult concepts to understand as a beginning programmer. But you can take this information to other programming languages. If you still do not understand arrays, take a look at the PHP Documentation on arrays.
Now to access variables inside of arrays, you use the variable name and then a bracket. Take a close look at the below example.
<?php // Access the $contacts array for the data we want... $andreaCell = $contacts['Andrea']['phone']['cell']; // Print it to the screen... echo "Andrea's cell phone number: $andreaCell"; ?>
PHP allows you to create new keys/values inside of arrays using this technique.
<?php $array1 = array(); $array1['key1'] = 'value1'; $array1['key2'] = 'value2'; ?>
You do not have to specify the keys of an array. If you do not specify a key, it will automatically assign an integer starting at zero.
<?php $array1 = array('value1', 'value2', 'value3'); ?>
If you take a look at the keys for the values inside of $array1, value1 will have a key of 0, value2 will have a key of 1, etc….
You will often times need to use variables by combining them with other variables.
<?php $variable1 = 'Chris Roane'; $variable2 = 'Andrea Roane'; $variable3 = $variable1 . ' is married to ' . $variable2; ?>
The period (.) is what you use to put variables together. You can use this with strings, integers and doubles.
In PHP there are a few global variables you have access to. All of these variables are arrays. Below are a few options.
This global variable contains information on your server and script. Depending on the environment your server is running PHP in, will determine what variables you have access to. For example…. $_SERVER[‘DOCUMENT_ROOT’] ….will display the document root of where you are running the code from.
When information from a form is “posted” to another page, those form values are put into this variable. This is how you would work with values from a form. Take a look at the simple PHP form tutorial for more info in how to work with this array.
This is similar to the $_POST array, except the $_GET array is sent through the url. For example…the variable “name” is being sent as a $_GET variable to this url: http://www.montanaprogrammer.com/index.php?name=ChrisRoane .The get variable ‘name’ will be accessible in the index.php file with this code: $_GET[‘name’] .
A simple trick when working with or debugging any kind of array (including global arrays) is using the following code:
<?php echo '<pre>'; print_r($_POST); echo '</pre>'; ?>
That code will display the array in the browser in a readable format.
This concludes going through the basics of PHP in part one of our two part series. Stay tuned for the next beginning php tutorial. If you have any questions, post them in the comments.