Primitive PHP Data Types
A language classifies the types of data it deals with into what are called primitive data types. Much like you would organize and label the condiments and spices in your kitchen or the books on your bookshelf, a programming language does some organization of data structures within the confines of the language. This is so the language can more easily deal with the different kinds of data it handles. In PHP we have 8 primitive data types. They are categorized into 3 basic categories – scalar, compound, and special. It’s also important to note that PHP is a dynamically typed language. So it does a lot of translation between many of these data types for you transparently. It makes PHP a very forgiving language in places where you try to do arithmetic operations on, normally, incompatible data types – like strings and integers, for example.
Scalar Types
Boolean types are the simplest PHP data types. They have only two possible values and they are true and false. TRUE and FALSE are treated as language constants in PHP and they are case-insensitive.
Read the documentation on boolean types here.
Integer types may contain any whole number. They are always represented as signed integers (denoted by the left most bit reserved to indicate if the integer is positive or negative). They have an upper-bound of 32 bits or 2^31 as signed integers. However, PHP is a dynamically typed language so it will automatically convert numbers that exceed these bounds into a more suitable type like floats.
Read the documentation on integer types here.
Float or Double types may contain any decimal number. They may be either signed or unsigned (positive or negative). Floats aren’t generally accurate by nature, because they are an approximation of value and not absolute value.
Read the documentation on float types here.
String types can contain any number of characters (PHP defines characters in a string as bytes) and has no upper-bound. The limitation only caps at the amount of memory PHP can allocate on the machine. Strings in PHP only support 256 bit character sets (ASCII and Extended ASCII), which sets them apart from some string types in other languages. Some languages distinguish between strings and byte arrays, but for all intents and purposes PHP strings are already considered byte arrays so they are inherently binary safe. You can store any amount of binary data in a PHP string without having to worry about encoding since PHP deals with its strings on the binary level. In places where encoding is important (like string functions) PHP offers multibyte safe functions that help you deal with multibyte encodings.
In PHP a string must be delimited by one of four syntaxes: single quotes, double quotes, heredocs, and nowdocs. The single quote syntax is simple and straight-forward. You begin the first character of the string with a single quotation mark and end the string with the last character as a single quotation mark. If you wish to use a single quote inside of your single-quoted string you must escape the quotation character with a backslash immediately proceeding the single quotation character. Double quotes are similar, except that they allow for interpolation. Interpolation means certain sequences of characters may be automatically replaced with other values inside of the string. This is some of the transparent translations PHP does to make programming in the language easier and more convenient. For example, you may include another variable inside of your string and PHP will replace the variable name inside the string with its value. The string must still begin with and end with the double quotation character and again if you wish to use the double quotation characters inside of the string you must escape them by placing a backslash immediately before the double quote. Heredocs and nowdocs are similar to double quote and single quote strings except that they don’t begin or end with quotes. Instead the heredoc and nowdoc syntaxes require an identifier. I’ll explain what the identifier is further ahead in this tutorial under the variables section. Also see the examples below.
Read the documentation on string types here.
Compound Types
Array types are essentially ordered maps in PHP. They are made up of key/value pairs. PHP arrays are associative in nature. That means they always associate a value with its key. An array key may be specified as any of the above 4 scalar types. However, PHP will automatically try to convert these keys to either numeric values or string values. Even if you don’t specify a key for a value in your array PHP automatically specifies a key based on the next available numeric index of the values in that array. Array values may be of any one of the 8 primitive data types we are discussing here. This makes it possible to assign an array as the value of another array.
Read the documentation on array types here.
Object types are the instances of class definitions. They can store properties, which are made up of property names (similar to array keys) and values (similar to array values). The key difference between objects and arrays is that objects define their own type in user-land code. This means an object is of the primitive data type object, but is defined as a type of its class member.
Read the documentation on object types here.
Special Types
Resource types are normally some link to an external resource that is supplying PHP with data or receiving data from PHP. This link may be to a socket connection, for example, a database connection, a connection to an external or shared library like an image resource, or even a connection to a file handler that’s reading from disk or some stream.
Read the documentation on resource types here.
Null types only have one possible value and that’s null. It is a special type that signifies the absence of value. NULL is also treated as a language constant in PHP and is case-insensitive. PHP considers all undefined variables to be of type NULL.
Read the documentation on null types here.
Variables
Try to think of a variable as a temporary place to store data during the execution of your script. Since PHP only runs for as long as it takes your code to execute (this is normally just fractions of a second) all of the data it stores from that script (the variables, constants, etc..) is only going to last during that time. PHP also doesn’t share any memory. It’s built on share-nothing architecture, which makes it easily scalable and robust. PHP also has a garbage collector. The garbage collector automatically manages cleaning up all of the memory your script used after its done running. Nothing persists in PHP and this is basically one of the things that make it a very forgiving and easy to work with language for beginners. In systems programming languages you have to actually allocate memory addresses for your data and then create pointers to these memory addresses for what data they are storing in your program. You then have to make sure you remove all of those pointers and release their allocated blocks of memory back to the machine when you’re done. If you break this relationship your program will crash. PHP takes away all of this worry by making sure these steps are already taken for you. PHP also doesn’t force you to declare any variables before you use them – although initializing your variables is still good coding practice.
A variable in PHP must begin with a sigil (in our case the dollar sign $). The dollar sign is then followed by a variable name. This is called a label, in PHP. PHP labels share the same naming rules (they are used to define variable names, function names, class names, and various other things like the heredoc/nowdoc identifiers mentioned earlier). The first rule is a label can not begin with a numeric character. It can begin with either a letter A-Z, or a-z, or an underscore. It may even begin with any character in the range of 0x7f through 0xff – which are the hexadecimal representations of extended ASCII chars 127 – 255. However, a label may not contain any spaces or any other characters than the numbers, letters, and characters mentioned here (with the exception of the first character not being a numeric character). There’s no upper-bound on labels, but you certainly want to keep them short. PHP won’t stop you from naming a variable that’s 10 million characters long, for example, but just because you can do it doesn’t mean there’s any good reason to do so. PHP variables are case-sensitive. So $a would be seen as a completely different variable from $A. The variable can store any of the 8 primitive data types we discussed in this tutorial. The variable can also be re-assigned any value at any time without declaring its data type. Variables can also be deleted using the unset() function. There’s also one more rule in variable naming in PHP and that’s that the pseudo variable $this is reserved and can not be assigned. Doing so will cause a fatal error. The assignment operator in PHP is the equal sign = and it has right to left associativity. This means the expression on the right side of the assignment operator is evaluated and assigned to the variable on the left side of the assignment operator.
Here are a few examples of assigning different data types to variables in php.
// Assign boolean types to a variable
$boolean = TRUE;
$boolean = FALSE;
unset($boolean); // Delete the variable
// Assign integer types to a variable
$integer = 0;
$integer = 1;
$integer = 239;
$integer = 1962781542;
$integer = -142
unset($integer); // Delete the variable
// Assign float types to a variable
$float = 1.31;
$float = -947.00001;
$float = 16E-146; // A floating point number in scientific notation
unset($float); // Delete the variable
// Assign string types to a variable
$string = 'This is a string...';
$string = "This is a string in double quotation marks...$string";
// The above string will actually include the value of the variable $string before it is re-assigned. So it would output "This is a string in double quotation marks...This is a string..." since double quotes allow interpolation as mentioned above.
// Double quotes in PHP strings cause interpolation of certain character sequences
$string = "You do not need to escape 'single quotes'\ninside double quoted strings.";
// This will cause the backslash n to become a new line character or ASCII character code 10
// Single quotes do not.
$string = 'You do not need to escape "double quotes"\ninside single quoted strings.';
// This will just stay a literal backslash n
// The escape character in a string is the backslash and it must be immediately proceeding the escaped character
$string = 'You do need to escape \'single quotes\' inside single quoted strings.';
$string = "You do need to escape \"double quotes\" inside double quoted strings.";
// Using heredoc syntax for strings.
$string = <<<HEREDOC
You don't need to escape 'single' or "double" quotes inside of a heredoc syntax string.
HEREDOC;
// You can also use nowdoc syntax which does not cause interpolation
$string = <<<'NOWDOC'
You don't need to escape anything inside of a nowdoc string.
This \n will not be translated.
NOWDOC;
unset($string); // Delete the variable
$array = array(1,2,3,4,5); // Defines an array of 5 values
$array = array('First Element' => 1, 'Second Element' => 2); // Defines an array of 2 elements with string keys and integer values
unset($array); // Delete the variable
$obj = new stdClass; // Defines an object from a methodless class
unset($obj); // Delete the variable.
$resource = fopen('foo.txt', 'r'); // Defines a resource to a file pointer.
unset($resource); // Delete the variable
$null = NULL; // Assigning null to a variable effectively removes its value and it becomes undefined.
unset($null); // Delete the variable.
Notice when I used the heredoc and nowdoc syntax for the string I started the string with three less-than signs followed by a label (this is called an identifier) and then a new line character. This is very important. The string then ended with the same label (or identifier) followed by a semicolon and possibly a new line (or line feed) character. The nowdoc requires that I place the label inside of single quotes. The end of the string also requires that the label is placed on the first column of the new line. That means the identifier must be proceeded directly by a line-feed or CRLF character (whatever is native to the operating system). So there can be no whitespace proceeding or exceeding the closing delimiter of heredoc or nowdoc syntax strings. There also can’t be any other characters after or before the identifier with the exception of a semicolon and new line character or (carriage return line-feed depending on your system), for example.
Some more examples of assignment…
$int = 1; $double = 12.0; $str = 'There are ' . $double * $int . ' eggs in a dozen!'; // $str will have the value 'There are 12 eggs in a dozen!' $STR = "Dozens of eggs are a good source of protein.\t"; // Notice that $STR is different from $str (variables are case-sensitive in PHP) and the backslash t will be translated to a tab character. $x = $y = $z = 3; // All three variables are assigned the value of 3
Constants
Another way to store data in memory in your PHP scripts is through the use of constants. Unlike variables, which can store any of the 8 primitive data types we discussed in the beginning of this section, constants can only store the four Scalar types we mentioned here and one of the Special types, which is null. Also, unlike variables, constants may not be re-assigned or removed. A constant can only be defined once in your script and may not be redefined (or over-written). We learned about some language constants like TRUE, FALSE, NULL, and digit characters like 0-9 that you can already use throughout the language. Notice we don’t quote the constant or proceed its name with a sigil, unlike a variable name. Constants also can not be interpolated inside of strings. Their values can be concatenated to a string, however.
You may define constants in your PHP code using one of two options (depending on which version of PHP you are using). One way is using the function define(), which allows you to additionally specify a third argument, or parameter, that defines the constant as case-insensitive. If you’re using PHP 5.3 or greater you can also use the const keyword followed by the constant name (same naming rules apply as all other PHP labels), an assignment operator, and finally the scalar value of the constant.
You shouldn’t use a constant in places where you expect to change the value from your code. For example, if you would like to create a for loop, like the one we demonstrated in the previous tutorial, using a constant in your for loop makes no sense, because you can’t overwrite the value of a constant. You expect to change the value of that variable throughout your script so it should remain a variable. Another thing I see a lot of beginners doing is making the mistake of assigning constants with things like the username and password for a database they want to connect to. You probably don’t expect to change these pieces of data from your code, but it’s probably still not a good idea. There may be situations where you will want to change those values from your code such as for testing purposes. A good use of constants would be a value that doesn’t mean anything to the developer, but means something to the interface or API you’re dealing with. For example, the cURL extension in PHP has a predefined constant of CURLOPT_AUTOREFERER, which looks meaningful to the programmer using that extension. It’s value is 58, which doesn’t look meaningful at all to the programmer, but they don’t see that value they just see the constant name in their code, which is descriptive enough, and curl knows what to do with the integer value when it’s used as one of the settings.
// PHP 5.3
const TURN_ON_PHP = 1;
const TURN_OFF_PHP = 0;
const FOO = 'foo bar';
const BAR = NULL;
const foo = TRUE; // User defined constants are case-sensitive by default
const bar = FALSE; // So foo isn't FOO and bar isn't BAR
// Using the define() function - before 5.3
define('TURN_ON_PHP', 1);
define('TURN_OFF_PHP', 0);
define('FOO', 'foo bar');
define('BAR', NULL);
define('foo', TRUE);
define('bar', FALSE);
/* Notice you still need to quote the constant name when using the define function, but not when using the constant itself */
- Next PHP Functions
- Previous PHP Control Structures