X

Rate Us :



Share with Friends :

Menu

Data Types available in PHP :

Data types are used to specify the type of data which variable can hold. Basically, we have three types of data types in PHP :

  • Scalar




  • Compound




  • Special




Scalar data types again divided into different types :-

Boolean :

These data types represents either true or false. In PHP, value of true is '1' and the value of false is 'nothing'.

Example :

<?php $x = true; echo $x; ?>

Output :

1

is_bool(variable) :

By using this function, we can check whether the variable is boolean variable or not.

Example :

<?php $x = true; echo is_bool($x); ?>

Output :

1

(bool)variable or (boolean)variable :

It is use to convert the data type of variable into boolean data type.

Example :

<?php $x = "true"; $x=(bool)$x; echo is_bool($x); ?>

Output :

1

Integer :

This data type stores numeric values.

Example :

<?php $x = 123; echo $x; ?>

Output :

123

is_int or is_integer :

By using this function, we can check that the given variable is an integer or not.

Example :

<?php $x = 123; echo is_int($x); ?>

Output :

1

(int) variable or (integer) variable :

By using this conversion function, we can convert a variable data type into integer data type.

Example :

<?php $x = "123"; $x = (int)$x; echo is_int($x); ?>

Output :

1

Float :

This data type stores decimal values.

Example :

<?php $x = 123.0; echo $x; ?>

Output :

123.0

is_float :

By using this function, we can check that the given variable is an float or not.

Example :

<?php $x = 123.0; echo is_float($x); ?>

Output :

1

(float) variable :

By using this conversion function, we can convert a variable data type into floating type data type.

Example :

<?php $x = "123"; $x = (float)$x; echo is_float($x); ?>

Output :

1

String :

String is a collection of characters in PHP. In PHP, we can declare String variable in three ways :

  • Using single quotation




  • Using double quotation




  • Using Heredoc quotation




Example :

<?php $a="ABC"; echo "$a"."\n"; echo '$a'; ?>

Output :

ABC $a

Note :

If we place a variable within double quotation, it will return the value of that variable.


Heredoc :

By using this syntax we can display the HTML elements through PHP script. It avoids the problems what were getting with open quotations of String. This syntax doesn't require any open quotation that's why for internal values we can use both single & double quotations.


The syntax of Heredoc is :-

<?php
<<<name_of_string
////content
name_of_string;
?>

Example :

<?php echo<<<mystring <input type="button" value="Welcome"> mystring; ?>

Output :

Button will be created with text as 'Welcome'

Compound data types :-

Array :

It is a collection of elements.


Object :

It is an instance of a class.


Special data types :-

Resource data types :

It refers the external resources like Database connection, FTP connection etc.

Example :

<?php $con=mysql_connect("localhost" , "root" , " "); echo $con."\n"; echo get_resource_type($con); ?>

Output :

resource id #2 mysql link

Null data types :

In PHP, null is not a value, we can consider a variable as null based on three conditions :-

  • If the variable is not set with any value.

  • If the variable is set with null value.

  • If the value of variable is unset.

is_null :

By using this function, we can check whether the variable is null or not.

Note :

We can unset the variable value by using unset() function.

Example :

<?php $x=100; unset($x); echo is_null($x); ?>

Output :

1