Google News
logo
Data Types in PHP
The PHP Variables can store data of different types, and different data types can do different things.

PHP supports total eight primitive data types: 
  • Array
  • Boolean
  • Float
  • Integer
  • Object
  • Resource
  • String
  • NULL 
These data types are used to construct variables.
PHP Arrays
An array stores multiple values in one single variable.
<!DOCTYPE html>
<html>
<head>
	<title>PHP Datat Types - Arrays</title>
</head>

<body>
	<?php
		$books = array("HTML5", "CSS3", "JavaScript", "jQuery", "Bootstrap", "PHP", "MySQL");
		var_dump($books);
		echo "<br /><br />"; 
		$colors = array(
			"Red" => "#ff0000",
			"Green" => "#00ff00",
			"Blue" => "#0000ff"
		);
		var_dump($colors);
	?>
</body>
</html>
Output :
PHP Booleans

Booleans are the easiest type. A boolean expresses a truth value. It can be either TRUE or FALSE.

To specify a boolean literal, use either the keyword TRUE or FALSE. Both are case-insensitive.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Data Types - Booleans</title>
</head>

<body>
	<?php
		// Assign the value TRUE to a variable
		$error = True;
		var_dump($error);
	?>
</body>
</html>
Output :
PHP Floating Point Numbers or Doubles

Floating point numbers ("floats", "doubles" or "real numbers") can be specified using any of the following syntax :

<!DOCTYPE html>
<html>
<head>
	<title>PHP Data Types - Floating Point Numbers or Doubles</title>
</head>

<body>
	<?php
		$a = 5.261;
		var_dump($a);
		echo "<br />";
		 
		$b = 9.2e3;
		var_dump($b);
		echo "<br />";
		 
		$c = 3E-9;
		var_dump($c);
	?>
</body>
</html>
Output :
PHP Integers

An integer data type is a number of -2, -1, 0, 1, 2, ...

Integers can be specified in decimal (10-based), hexadecimal (16-based) or octal (8-based) notation, optionally preceded by a sign (- or +).

If you use the octal notation, you must precede the number with a 0 (zero), to use hexadecimal notation precede the number with 0x.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Data Types - Integers</title>
</head>

<body>
	<?php
		$a = 123; // decimal number
		var_dump($a);
		echo "<br />";
		 
		$b = -123; // a negative number
		var_dump($b);
		echo "<br />";
		 
		$c = 0x1A; // hexadecimal number
		var_dump($c);
		echo "<br />";
		 
		$d = 0123; // octal number
		var_dump($d);
	?>
</body>
</html>
Output :
PHP Objects

To initialize an object, you use the new statement to instantiate the object to a variable.

Every object has properties and methods corresponding to those of its parent class. Every object instance is completely independent, with its own properties and methods, and can thus be manipulated independently of other objects of the same class.

Here's a simple example of a class definition followed by the object creation.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Data Types - Objects</title>
</head>

<body>
	<?php
		class Website{
			function Website(){
				$this->model = "www.freetimelearning.com";
			}
		}
		// create an object
		$my_site = new Website();
		
		// show object properties
		echo $my_site->model;
	?>
</body>
</html>
Output :
PHP Resources

A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions. See the appendix for a listing of all these functions and the corresponding resource types. Resource handlers to opened files and database connections.

<?php
	$con = mysqli_connect("localhost","root","");
?>
PHP Strings

A string is sequence(series) of characters. where every character is the same as a byte.

A string literal can be specified in three different ways.

  • single quoted : 'Hello world!'
  • double quoted : "Hello world!"
  • heredoc syntax : strings is by using heredoc syntax ("<<<").
<!DOCTYPE html>
<html>
<head>
	<title>PHP Data Types - Strings</title>
</head>

<body>

<?php
	$a = 'Hello world!';
	echo $a;
	echo "<br />";
	 
	$b = "Hello world!";
	echo $b;
?>

</body>
</html>
Output :
PHP NULL

The NULL is a special data type. NULL value represents that a variable has no value. NULL is the only possible value of type NULL.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Data Types - NULL</title>
</head>

<body>

<?php
	$a = NULL;
	var_dump($a);
	echo "<br>";
	 
	$b = "Free Time Learning";
	$b = NULL;
	var_dump($b);
?>

</body>
</html>
Output :