Hello folks! welcome back to a new section of our tutorial on
PHP. In this tutorial guide, we will be studying about PHP Variable and Data Types.
The paramount way of storing information in the middle of a PHP program is by using a variable.
Here are the most vital things that you need to know about variables in PHP.
The paramount way of storing information in the middle of a PHP program is by using a variable.
Here are the most vital things that you need to know about variables in PHP.
- All variables in PHP are denoted with a leading dollar sign ($).
- The value of a variable is the value of its most recent assignment.
- Variables are assigned with the "=" operator, with the variable on the left-hand side and the expression that is to be evaluated on the right side.
- Variables can, but do not need to be declared before assignment.
- Variables in PHP do not have inherent types - a variable don't know ahead of time whether it will be used to store a number or a string of characters.
- Variables that're used before they are assigned have difficult values.
- PHP does a good job of automatically converting the variable types from one form to another.
- PHP variables are Perl-like.
READ: PHP Basic Syntax
PHP Data Types
PHP has a total of eight data types which can be used to construct our variables -
- Integers - These are whole numbers, without a decimal point, like 242.
- Doubles - are floating point numbers, like 3.141.
- Booleans - Booleans have only two possible values, either True or False.
- Null - is a special data type that only has one value; Null.
- Strings - are order of characters for example "PHP has support for string operations".
- Arrays - These are named and indexed collections of other values.
- Objects - These are the instances of programmer-defined classes, which can package up other kinds of values and functions that are specific to the class.
- Resources - Resources are special variables that holds references that are external to PHP.
The first five are simple types, and the next two i.e. arrays and objects are compound - The compound types can package up other random values of random type, whereas the simple type can't.
We will explain only the simple types in this post. Arrays and Objects will be discussed in our subsequent tutorials.
We will explain only the simple types in this post. Arrays and Objects will be discussed in our subsequent tutorials.
Integers
They are whole numbers, without a decimal point, like 2421. They are the simplest type. They tally with simple whole numbers, both positive and negative.
Example
Integers can be assigned to variables or can be used in expressions, as follows -
$int_var = 12345; $another_int = -12345 + 12345;
Integers can be in decimal (base 10), octal (base 8), and hexadecimal (base 16) form. Decimal format is the default format, octal integers are specified with a leading 0, and hexedicimals have a leading 0x.
Doubles
They're like 3.14 or 4.90. By default, doubles prints with the minimum number of decimal places needed.
Example
The following below is a simple example -
<?php $many = 2.2888800; $many_2 = 2.2111200; $few = $many + $many_2; print("$many + $many_2 = $few <br>"); ?>
Output
When the above code is executed, it will produce the following result -
2.28888 + 2.21112 = 4.5
Boolean
They have only two possible values, either true or false. PHP makes available a couple of constants primarily for use as Booleans: True and False which can be used as shown below -
if (TRUE) print("This will always print<br>"); else print("This will never print<br>");
Interpreting other Types as Booleans
Here are the rules for determing the "truth" of any value not already of the Boolean type -
- If the value is a number, it is false if exactly equal to zero, otherwise true.
- If the value is a string, it is false if the string is empty or the string is "0". It is true otherwise.
- Values of the type NULL are always false.
- If the value is an array value, it's false if it contains no other values, else it is true. For an object, containing a value means having member variable which has been assigned a value.
- Valid resources are true (though some functions that returns resources when they are successful, is going to return False when unsuccessful).
- Don't use double as Booleans.
Each of the the following variables has the truth value embedded in its name when it is used in a Boolean context.
$true_num = 3 + 0.14159; $true_str = "Tried and true" $true_array[49] = "An array element"; $false_array = array(); $false_null = NULL; $false_num = 999 - 999; $false_str = "";
NULL
It's a unique type with only one value: NULL. To give a variable the NULL value, assign it like this -
$my_var = NULL;
The unique data type NULL is capitalized by convention, but actually it's case Insensitive; you could as well type -
$my_var = null;
A variable that has been assigned NULL has the following properties -
- It evaluates to False in a Boolean context.
- It returns False when tested with the IsSet() function.
Strings
Strings are order of characters for example "PHP has support for string operations".
Example
The following below are valid examples of strings -
$string_1 = "This is a string in double quotes"; $string_2 = 'This is a somewhat longer, singly quoted string'; $string_39 = "This string has thirty-nine characters"; $string_0 = ""; // a string with zero characters
The single quoted strings are treated almost literally, but double quoted strings replaces variables with their values. It also interprets certain character sequences.
Example
The following below is a simple example -
<?php $variable = "name"; $literally = 'My $variable will not print!'; print($literally); print "<br>"; $literally = "My $variable will print!"; print($literally); ?>
Output
When the above code is executed, it will produce the following result -
My $variable will not print! My name will print
There are no artificial limits on string length - within the bound of available memory, you should be able to make random long strings.
Strings that are delimited by double quotes are preprocessed in both the following two ways by PHP -
Strings that are delimited by double quotes are preprocessed in both the following two ways by PHP -
- Certain character order that begins with backslash (\) are replaced with the special characters
- Variable names(starting with $) are replaced with string representations of their values.
Following below are the escape-sequence replacements -
- \n is replaced by newline character
- \r is replaced by the carriage return character
- \t is replaced by the tab character
- \$ is replaced by the dollar sign itself ($)
- \" is replaced by the single double-quote (")
- \\ is replaced by a single backslash (\)
Here Document
You can assign multiple lines to a single string variable using the here document -
<?php $channel =<<<_XML_ <channel> <title>What's For Dinner</title> <link>http://menu.example.com/ </link> <description>Choose what to eat tonight.</description> </channel> _XML_; echo <<<END This uses the "here document" syntax to output multiple lines with variable interpolation. Note that the here document terminator must appear on a line with just a semicolon. no extra whitespace!
END; print $channel; ?>
Output
When the above code is executed, it will produce the following result -
This uses the "here document" syntax to output multiple lines with variable interpolation. Note that the here document terminator must appear on a line with just a semicolon. no extra whitespace! <channel> <title>What's For Dinner<title> <link>http://menu.example.com/<link> <description>Choose what to eat tonight.</description>
Variable Scope
Scope is the range of availability a variable has to the program in which it was declared in. Variables in PHP can be one of the four scope type below -
Variable Naming
Following below are the rules for naming a variable -
- The variable names must start with a letter or an underscore character.
- Variable name can consist of letters, numbers, underscores, but you can not use characters like +, -, %, (, ), etc.
READ: An Introduction to PHP
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP Local Variables
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.