PHP Data Type

  • Author: Sajjad Hossain
  • |
  • Added: Feb 19, 2016
  • |
  • In: Basic PHP

Every language has the own data type. We should know the data type of any variable example is $a= 100; that means the value of $a is 100 and it is an Integer non-decimal number it could also a decimal number like 100.25 or it could also a text like $a = "PHPAns"; That means we should identify the data type of a variable. When we set a number like 100 is a value of $a then it means it's an integer, when we set $a = "PHPAns"; that means $a is a string and when we set value like $a = 100.25; that means the value of a is decimal and $x is floating number etc.

PHP has some data types that mean PHP variable can take that type's data and participate any type of operation. Mainly PHP has two data type php scalar data types and php compound data types. Let's know about these data types.

Scalar data also divide into 4 types.
1. boolean: This data type has two type of value TRUE or FALSE, PHP have many functions this function returns boolean data types. Normally use in the conditional statement like if is_bool() is a function to check variable data boolean. example-

<?php
$a = TRUE;
$b = 100;
var_dump(is_bool($a)); // output bool(true)
var_dump(is_bool($b)); // output bool(false)
?>
PHP checks these variables data type and executes results. First, it checks variable $a value using the is_bool() function it returns true cause TRUE is boolean data type and when checking $b variable then it returns false cause value of $b is an Integer not boolean. You can check any variable's value using this.

2. integer: Integer is full number means without decimal that's like 2,8,10,44,-23 etc are integer data. You can check integer data using is_int() function, you can also check any variable using is_int() function. If the varible value is the full number then this function will return TRUE otherwise this function will return FALSE. The full number can be a positive or negative number even decimal- 10 based, hexadecimal, octal or binary can be Integer.
Here is some example-

<?php
$x = 1011100; // binary number 92
var_dump($x);
?>
output int(1011100)

3. float: A float is a decimal number like 10.2646, 2.456 etc are float number or, doubles or, real number.
example code-
<?php
$x = 10.2646;
var_dump($x);
?>
output float(10.2646)

4. string: If you use one or more then one character into single quotation or double quotation syntax then it will be called string.
example-
<?php
$a = 'my text';
$a = "my text";
$a = <<<HTML
<p>my text</p>
HTML;
?>
Compound data type has array and objects data type. And it has also two more data type resource and NULL.
resource mainly some type of data system like database return is a resource data. And NULL is empty data system, If you declare any variable but not set the value of variable then this variable will return a NULL value.

Type Casting
You can change data type on PHP. The system of convert a data into another data is called type casting. Yours should declare tag into the first bracket before data. Tags are,
(int) for Full Number
(bool) for Boolean
(float) for Decimal, Double, Real Number
(string) for Character
(array) For Create Array
(object) For Convert into object
(unset) For NULL a value

Here is an example that $a = 100; in this variable $a is Integer that means full number and I want to convert it into string
<?php
$a = 100;
$a = (string) 100;
var_dump($a);
?>
Output string(1) "100"
if you remove var_dump from this code then this script will execute as int(100).

Top