We’ve covered a bunch of front end design and programming in some recent posts a la, twitter bootstrap. What about minding the back end? When we talk back end, we talk PHP. What is PHP? It’s a cool programming language efficient in many ways used to communicate with a database, generate sessions and cookies, drum up unique content to serve to a browser, and even interact with third party API’s or Application Programming Interfaces.
PHP is a broad topic so we’ll break it up into many blog posts, but let’s start at the beginning here and talk about variables and data types.
Data Types in PHP
PHP supports the following basic data types:
Integer—Used for whole numbers
Float (also called double)—Used for real numbers
String—Used for strings of characters
Boolean—Used for true or false values
Array—Used to store multiple data items
Object—Used for storing instances of classes
In addition to the above listed data types, PHP also has a NULL and resource type. If a variable has not been assigned a value, has been explicitly set to NULL, or has become unset, then it is said to have a NULL value. You may run into the resource type when dealing with files and databases as the functions in PHP often make use of a resource when you need to open a file for reading or connect to a database. What this means is that they give you a file handle or database connection as a resource, which you can then use to pass to other functions.
PHP is a weakly typed language which means that a variable does not need to be declared with a type, you simply assign values to the variable regardless of data type. Their are pros and cons to this approach. For the experienced programmer, it can save time. For the newcomer, it may lead to some strange results.
Consider the following code:
$value1 = 1; $value2 = 2.00;
$value1 holds an integer value, simply because the number 1 was assigned to it. $value2 on the other hand is a float since 2.00 is a floating point value.
Now get ready to have your mind blown.
$value2 = 'I can change the data type on the fly';
$value2 is now a string! By assigning the string value to the variable, you are allowing PHP to update the variable type based on what has been assigned to the variable. We need to cover MUCH more ground, but this is a super simple intro to PHP variables and data types. See you in the next post!