PHP Variables
ADVERTISEMENTS
In PHP, the variable declares with the $ (dollar) symbol. There is no need to define the data type of variable, PHP automatically sets data type according to the variable's assigned value.
PHP supports ten primitives of data types
- Integer
- Float
- String
- Boolean
- Array
- Object
- Callable
- Iterable
- Resource
- Null
An example of variable declaration in PHP
<?php
/* Integer data type */
$a = 123;
/* Float data type */
$b = 125.08;
/* String data type */
$c = "Hi testing program";
/* Boolean data type */
$d = false;
/* Array data type */
$e = array(1, 2, 3, 4);
// This will print the data type of declared variables
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
?>
Output:
int(123)
float(125.08)
string(18) "Hi testing program"
bool(false)
array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
}
Integer Data Type
- An Integer data type is a numeric value.
- It's the set of (.., -3, -2, -1, 0, 1, 2, 3, ...).
- It can be a positive value and a negative value.
- The data size of an integer value depends on the hardware platform.
- An integer size on the 32-bit platform is 2147483647.
- An integer size on the 64-bit platform is 9223372036854775807.
Float Data Type
- A Float data type is a numeric value with an exponent.
- The size of the float data type depends on the platform.
- Max's size of the float data type is 1.8e308.
String Data Type
- A string data type is a set or series of characters.
- PHP only supports a 256-character set.
- A string declares with four literals (single-quoted, double-quoted, heredoc syntax & now doc syntax).
Boolean Data Type
- A Boolean is an expression value.
- It's the true value of a boolean expression.
- It may be only either True or False.
Array Data Type
- An array is a collection of many values more than one value.
- An array of supports homogeneous data types.
- Like It may be Integer, Float, or string value.
- This is the ordered map of values.
- There are values are associated with keys.