PHP Variable – int, boolean, null, double variable types with examples
What is variable?
Just like in any programming language, PHP Variables are the way to store information in the middle of the PHP programs.
Important things to know about variables
- Variables in PHP are declared/denoted by using a leading $ sign. e.g.
- The most recent assignment to a variable is the value of that variable.
- The assignment operator used in variables is ‘=’ sign where a variable is on the left and expression to be at the right side.
$variablename = “variable value”;
- In PHP, the variables can be declared before assignment but it is not necessary.
- Variables are not intrinsic types i.e. a variable does not know if it will be used to store a number or string.
- Before assigning any value, the variables have default values.
- When necessary, PHP can automatically convert types from one to another.
Types of variables
There is a total of 8 data types that are used to construct variables in PHP.
PHP int is the whole numbers without a decimal point. An example of int variable is:
$int_var = 12345;
$another_int = -12345 + 12345;
The Double type
The Double are the numbers with decimal point e.g. 1.234 or 50.1.
$many = 2.2888800;
$many_2 = 2.2111200;
$few = $many + $many_2;
Boolean data type
The Boolean variables may be assigned only two possible values. Either true or false. The example of PHP Boolean variable is:
//if condition is true
print("This will be executed
print("This will not be executed
PHP NULL type
The Null type is a special variable type that has only one value i.e. Null
$null_var = NULL;
Strings are the combination of characters e.g. ‘PHP supports string operations.’
$string_1 = "This is a string in double quotes";
$string_var2 = "This string has thirty-nine characters";
$string_0 = ""; // String with 0 character
The escape sequence in strings
You can use the following escape sequence within strings for respective results:
- \n means adding a new line.
- \r meant to be replaced by the carriage-return character.
- \t in a string means to replace by the tab character.
- \$ is replaced by the dollar sign ($).
- \” means a single double-quote (“).
- \\ means to add a single backslash (\).
Arrays in PHP are names and the indexed collection of values. Please see the dedicated chapter for declaring and using Arrays in PHP.
Object variables store instances of classes. This can package another kind of values and functions specific to use a class.
These are the special type that holds references to the resources external to PHP like a database connection.
This chapter explained only first 5 types that are simple in nature. Arrays and Objects are the compound in nature that can package up other arbitrary values of arbitrary types. The later types will be explained in their respective chapters in this PHP tutorial.
Naming conventions of variables
For choosing the name of your PHP variables, a few rules should be followed:
- The variable should start with a letter or an underscore “_”.
- The variable names can be combinations of a-z, A-Z, 0-9 or _ i.e. alpha-numeric characters, and underscores.
- If your variable is more than one word then it should be separated with an underscore e.g. $first_second.
- Python data types – Int, Float, string, dict, lists with examples
- PHP operators | Using assignment, ternary, arithmetic operators
- 4 examples to learn PHP array in 30 minutes
- PHP echo | 3 examples to learn echo statement
- An example to use PHP unset to destroy variables
- Learn to use simple and associative arrays in PHP with examples
- 2 demos of converting Java int to double and double to int
- How to use PHP Case | PHP switch select case with syntax and examples