☰ See All Chapters |
PHP Data Types
Data type is used to specify the type of data that variable can hold. A variable’s data type determines what operations can be carried out on the variable’s data, as well as the amount of memory needed to hold the data.
In PHP we have 3 types of data types.
1. Scalar
2. Compound
3. Special
Scalar Data types
Boolean
Boolean data type represents either true or false. In PHP value of true is 1 and the value of false is nothing.
<?php $boolean_var = true; echo $boolean_var; ?> |

is_bool(variable)
is_bool function is used to check if variable is boolean variable.
Type casting to Boolean
Below is the syntax to convert variables of other type to boolean type.
(boolean)variableName;
OR
(bool)variableName;
<?php $boolean_var = true; echo $boolean_var."</br>"; echo is_bool($boolean_var)."</br>";
$string_var = "true"; $string_to_boolean_var = (bool)$string_var; echo $string_var."</br>"; echo $string_to_boolean_var."</br>"; ?> |

Integer
Integer data type represents numeric values.
is_int(variable)/is_interger (variable)
is_int/is_integer function is used to check if variable is integer variable.
Type casting to integer
Below is the syntax to convert variables of other type to integer type.
(int)variableName;
OR
(integer)variableName;
<?php $integer_var = 123; echo is_int($integer_var)."</br>";
$string_var = "10"; $string_to_integer_var = (int)$string_var; echo $string_to_integer_var."</br>"; ?> |

Float
Float data type represents decimal values.
is_float(variable)
is_float function is used to check if variable is float variable.
Type casting to float
Below is the syntax to convert variables of other type to float type.
(float)variableName;
<?php $float_var = 123.10; echo is_float($float_var)."</br>";
$string_var = "10.10"; $string_to_float_var = (float)$string_var; echo $string_to_float_var."</br>"; ?> |

String
String is group of any characters. String can contain any alphabets, numbers, special characters.
In PHP you can declare string variable in 3 ways.
Using single quotes
Using double quotes
Using heredoc syntax
String using single quotes
Strings are created inside single quotes.
<?php $name = 'Manu Manjunatha'; ?> |
String using double quotes
Strings are created inside double quotes. String using double quotes supports interpolation, means if you place a variable within the double quotes, it returns the value of that variable. You can place the variable name directly inside double quotes or you can use interpolation syntax ${variableName} as below example.
<?php $name = 'Manu Manjunatha';
echo "My name is $name </br>"; echo 'My name is $name </br>'; echo "My name is ${name} </br>"; echo 'My name is ${name} </br>';
?> |

String using heredoc syntax
The heredoc syntax is useful for multi-line strings and avoiding quoting issues. Below is the heredoc syntax:
$variable = <<<nameOfString
//contents
nameOfString;
String using heredoc syntax supports interpolation, means if you place a variable within a string, it returns the value of that variable. You can place the variable name directly inside double quotes or you can use interpolation syntax ${variableName} as below example.
heredoc string example:
<?php $author= "Manu Manjunatha"; $tablename = "Student"; $id = 100;
$sql = <<<SQL select * from $tablename where studenid = $id and studentname = $author SQL;
$html = <<<HTML </br><b>Welcome to www.java4coding.com</b> </br> <p>Author: ${author}</p> <p>Author Id: ${id}</p> HTML;
echo $sql."</br>"; echo $html; ?> |

Compound data types
Array
PHP array is collection of values of heterogeneous (dissimilar) same data type.
array() function is used to create an array.
$array_name = array(element1, element2, element3, …);
Array index starts form 0. Below is the syntax of access array element:
$array_name[index];
count() function is used to get the size of array.
<?php $array_var = array("Mango", "Apple", 100, true); echo $array_var[0] . ", " . $array_var[1] . ", ". $array_var[2] . ", " .$array_var[3]."</br>"; echo "Array size: ". count($array_var); ?> |

Objects
Objects are the instances of class. You will learn about classes in later chapters.
<?php class welcome{ public $var="Welcome to www.java4coding.com"; function show_msg(){ return $this->var; } }
//create object from the class $welcome_msg=new welcome; var_dump($welcome_msg); ?> |

Special data types
Resource data type
Resource data type represents an access to external resources like database, files etc...
<?php $conn = mysqli_connect(localhost,"root","admin","password"); $fp = fopen("index.php",'r'); ?> |
null data type
null is not a value, variable is considered as null based on below 3 conditions
1. If variable is not initialized with value.
2. If variable is set with Null explicitly.
3. If the value of variable is unset using unset function;
<?php $a=null; echo is_null($a);
$b; echo is_null($b);
$c=10; unset($c); echo is_null($c); ?> |

All Chapters