HP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:
It holds only single value. There are 4 scalar data types in PHP.
It can hold multiple values. There are 2 compound data types in PHP.
There are 2 special data types in PHP.
Booleans are the simplest data type works like switch. It holds only two values: TRUE (1) or FALSE (0). It is often used with conditional statements. If the condition is correct, it returns TRUE otherwise FALSE.
if (TRUE) echo "This condition is TRUE."; if (FALSE) echo "This condition is FALSE."; output:This condition is TRUE.PHP Integer
Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional part or decimal points.s
Rules for integer:$dec1 = 34; $oct1 = 0243; $hexa1 = 0x45; echo "Decimal number: " .$dec1. ""; echo "Octal number: " .$oct1. ""; echo "HexaDecimal number: " .$hexa1. ""; output: Decimal number: 34 Octal number: 163 HexaDecimal number: 69Float
A floating-point number is a number with a decimal point. Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign.
$n1 = 19.34; $n2 = 54.472; $sum = $n1 + $n2; echo "Addition of floating numbers: " .$sum; output:Addition of floating numbers: 73.812String
A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters.
String values must be enclosed either within single quotes or in double quotes. But both are treated differently. To clarify this, see the example below:
$company = "Javatpoint"; //both single and double quote statements will treat different echo "Hello $company"; echo ""; echo 'Hello $company'; output: Hello Javatpoint Hello $companyArray
An array is a compound data type. It can store multiple values of same data type in a single variable.
$bikes = array ("Royal Enfield", "Yamaha", "KTM"); var_dump($bikes); //the var_dump() function returns the datatype and values echo ""; echo "Array Element1: $bikes[0] "; echo "Array Element2: $bikes[1] "; echo "Array Element3: $bikes[2] "; output: array(3) { [0]=> string(13) "Royal Enfield" [1]=> string(6) "Yamaha" [2]=> string(3) "KTM" } Array Element1: Royal Enfield Array Element2: Yamaha Array Element3: KTMobject
Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared.
ResourceResources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. For example - a database call. It is an external resource.
NullNull is a special data type that has only one value: NULL. There is a convention of writing it in capital letters as it is case sensitive.
$nl = NULL; echo $nl; //it will not give any output
Total : 26654
Today :3
Today Visit Country :