saftutorials.com
PHP Data Types Tutorial

PHP variables do not need to be declared before the assignment, it is declared at the time of assignment by the leading of dollar sign ($). PHP variables can store different types of data, there is no need to declared data types before or at the time of assignment.


There are eight data types in PHP

- Strings

- Integers

- Float or Doubles

- Booleans

- Null

- Array

- Objects

- Resources


How many data types in PHP?
There are eight data types in PHP such as Strings, Integers, Float or Doubles, Booleans, Null, Array, Objects, Resources.

Example of strings

<!DOCTYPE html>
<html>
<body>
<?php
$string-1 = "Hello John!";
$string-2 = 'String can contain 39 charecter';
echo $string-1;
echo $string-2;
?>
</body>
</html>


The above code produces the following results

Outputs: Hello John! String can contain 39 characters


Example of Integers

An integer must start from -2,147,483,647 and end at 2,147,483,647

<!DOCTYPE html>
<html>
<body>
<?php
$a = 357;
$b = -753;
echo $a;
echo $b;
var_dump($a);
?>
</body>
</html>

The above code produces the following results

Outputs: 357 -357 int(357)


Example of Float

The PHP var_dump function return also data types

<!DOCTYPE html>
<html>
<body>
<?php
$a = 3.57;
echo $a;
var_dump($a);
?>
</body>
</html>

The above code produces the following results

Outputs: 3.57 float(3.57)


Example of Boolean

<!DOCTYPE html>
<html>
<body>
<?php
$a = TRUE;
$b = FALSE;
if($a){
echo "TRUE";
}
if(!$b){
echo "FALSE";
}
?>
</body>
</html>

The above code produces the following results

Outputs: TRUE FALSE


Example of NULL

The PHP var_dump function return also data types

<!DOCTYPE html>
<html>
<body>
<?php
$a = "Hello John!";
$a = null;
var_dump($a);
?>
</body>
</html>

The above code produces the following results

Outputs: NULL


Example of Arrays

The PHP var_dump function return also data types

<!DOCTYPE html>
<html>
<body>
<?php
$fruits = array('apple', 'banana', 'mango');
var_dump($fruits);
?>
</body>
</html>

The above code produces the following results

Outputs: array(3) { [0]=> string(5) 'apple' [1]=> string(6) 'banana' [2]=> string(5) 'mango' }


Example of Objects

<!DOCTYPE html>
<html>
<body>
<?php
class fruits
{
function best_fruit()
{
echo "Mango.";
}
}
$object = new foo;
echo $object->best_fruit();
?>
</body>
</html>

The above code produces the following results

Outputs: Mango


Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic