Data Types In Php With Example For Define Value
Posted By
Bajarangi soft ,
Posted On 04-10-2022
A data type in php is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error.
Introduction to PHP data types
- A type specifies the amount of memory that allocates to a value associated with it. A type also determines the operations that you can perform on it.
- PHP supports the following datatypes,
Scaler Types :
This type of datatype holds only single value. There are 4 scaler datatype in PHP.
- integer
- boolean
- strings
- float
Compound Types :
This type of datatype holds multiple values. There are 2 compound datatype in PHP.
- Array
- object
Special Types:
There is only two datatype in special datatype.
- resource
- NULL
Now Let's understand each datatypes in briefly.
PHP integer
- An integer is a number without any decimal part.
- it can be positive or negative.
- An integer data type is a non-decimal number between -2147483648 and 2147483647 in 32 bit systems, and between -9223372036854775808 and 9223372036854775807 in 64 bit systems.
Example :
<?php
$int = 15*12;
var_dump($int);
?>
Output :
// Output
int(180)
PHP Boolean
Booleans are the easiest type. It can be either TRUE or FALSE. It is used in control structure like the testing portion of an if statement.
Example :
<?php
$a=100;
$b=50;
if ($a)
{
echo "The area of the rectangle is".$a*$b;
}
else
{
echo "The b needs to be a non-zero number";
}
?>
Output :
//output
The area of rectangle is 5000
PHP Strings
- Strings are a sequence of characters.
- Strings can hold letters, alphabet, number or any special charecter.
- String can be enclosed by 'Single quote' or 'Double quote' but both are diffrent type.
Example :
<?php
$name= "mike";
//both single and double quote statements will treat different
echo "Hello $name";
echo "</br>";
echo 'Hello $name';
?>
Output :
Hello mike
Hello $name
PHP float
- A float is a number with a decimal point or a number in exponential form.
- Floating point numbers are numbers with a decimal . Like integers, -321, 497, 19345, and -976812 are all valid, but now 4.5, 0.0004, -324.984, and other non-whole numbers are valid too.
Example :
<?php
$a = 5.205;
$b = 1.3e3;
$c = 6E-10;
$x = 10.365;
var_dump($a,$b,$c,$x);
?>
Output :
//output
float(5.205) float(1300) float(6.0E-10) float(10.365)
PHP Array
- An array is a special variable that we use to store or hold more than one value in a single variable without having to create more variables to store those values.
- To create an array in PHP, we use the array function array( ) . By default, an array of any variable starts with the 0 index.
Example :
<?php
$names = array ("Mike", "John", "Bobby");
//the var_dump() function returns the datatype and values
var_dump($names);
echo "</br>";
echo "Student 1: $names[0] </br>";
echo "Student 2: $names[1] </br>";
echo "Student 3: $names[2] </br>";
?>
Output :
array(3) { [0]=> string(4) "Mike" [1]=> string(4) "John" [2]=> string(5) "Bobby" }
Student 1: Mike
Student 2: John
Student 3: Bobby
PHP Object
- In PHP, Object is a compound data type (along with arrays).
- Values of more than one types can be stored together in a single variable.
- Object is an instance of either a built-in or user defined class.
- In addition to properties, class defines functionality associated with data.
Example :
<?php
class car {
function model() {
$model_name = "Audi";
echo "Car Model Is: " .$model_name;
}
}
$obj = new car();
$obj -> model();
?>
Output:
//output
Car Model Is: Audi
PHP resource
- The special resource type is not an actual data type.
- It is the storing of a reference to functions and resources external to PHP.
- A common example of using the resource data type is a database call.
PHP Null
- Null is a special data type that has only one value: NULL.
- The special type of data type NULL defined a variable with no value if we don't define a null value it will take it null automatically.
Example :
<?php
$x = NULL;
var_dump($x);
?>
Output :
//output
NULL