Crus4

logo

What are PHP Data Types


In PHP or in any other Programming language Data Type refers to the type of value a variable holds. For example if we have a variable $x and it holds a numeric value 3. Like this $x = 3;. So here we can say that variable $x holds an integer data type of value 3.

Now the variable can hold different types of values, or we can say different data types. Like the string data type: For Example $x = 'Hello World'; or a Boolean data type: For Example $x = true;. So based on this, PHP supports following Data Types:

  • String
  • Integer
  • Float (double)
  • Boolean
  • Array
  • Objects

String Data Type

A string is a sequence of characters that are enclosed within single or double quotes. Here is an example of a PHP String:

$x = 'Hello World';
$x = 'Some text';
$x = "Hello World, we are programmers, we can't hack your ex's FB account";

Integer Data Type

In PHP, an integer is a non decimal number ranges from -2,147,483,648 to 2,147,483,647 or from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 in 64-bit systems. In PHP, Integers can include positive or negative values including zero, without a decimal value. Here is an example of a PHP Integer.

$x = 3;
$x = -3;
$x = 800754;

Float Data Type

In PHP, Float is also a number but with a decimal value. Float or double can include all the positive and negative numbers with a decimal value. Here is an example of a PHP Float.

$x = 3.6;
$x = -3.764;
$x = 785754.1;

Boolean Data Type

In programming, Boolean Data Type is used for conditional testing. It represents two values true and false. When you work with Boolean values you will also get a result in 1 or 0. 1 means true and 0 means false. Here is an example of a Boolean data type.

$x = true;
$y = false;

Array Data Type

An array is a type of variable that can store multiple values in a single variable. Here is an example of an PHP array.

$fruits = ["apple", "grapes", "mango"];

We will more about arrays in our upcoming posts.

Null Data Type

In PHP, the null data type represents a variable with no value or a variable that has been explicitly set to null. Here is an example of a Null data type.

$x = null;

How to Find Data Type of any Variable

Suppose you have a variable like this $x = "3"; and you want to know whether it is a string or an integer data type. To figure it out PHP provides you two built-in functions var_dump() and gettype() function. These two functions will help you to figure out the data type of any variable in PHP.

Using gettype() function

In PHP, gettype() function is used to find out the data type of any variable. All you need to do is to put the variable name between the round braces (). Here is an example.

$x = "9";
echo gettype($x); //Outputs: String

Using var_dump() function

Like gettype() function var_dump() function is also used to find the data type of a variable. But var_dump() function will provide you some information about the variable and it is necessary to use echo before it. Here in an below example let’s create a variable and figure out it’s data type using the var_dump() function.

$x = 8;
echo var_dump($x); //Outputs: int(8) 
$y = 'Hello';
var_dump($y);  //Outputs: string(5) "Hello"

Share This Post!

What are PHP Data Types

One thought on “What are PHP Data Types

Leave a Reply

Your email address will not be published. Required fields are marked *