php

Datatypes in PHP | Explained for beginners

Datatypes are metadata that define the type of data that can be placed inside the PHP variables; Metadata means that it is the data about the data (information about data like structure and syntax). We are going to discuss all the data types that are supported by the PHP programming language.

Datatypes in much simpler words are nothing but a classification of values that can be used inside a variable and the set of methods and operators that can be applied to those values. Php programming language supports around 7 different predefined data types, these data types are namely:

  1. Integer
  2. Float (or double)
  3. String
  4. Boolean
  5. Array
  6. Object
  7. Null

We are going to go over each one of them separately. So, let’s go over the first one which is an integer Datatype.

Integer Datatype

Integer data type, as the name suggests, is used to store integer values. Integer means numeric values that are without a decimal point, or in mathematical terms, whole numbers.

To define an integer simply put the numeric value equal to the variable like:

<?php

   $number = 500;

   var_dump($number);

?>

Here, as you can see we are creating an integer variable number and setting its value to 500.In the next line, we are using the command var_dump which displays the type of the variable and the value of the variable in the console.

After running this code, you get the following output:

As you can see, the compiler tells us that the variable is of type int and the value is 500.

Float DataType

Another data type that deals with numeric values is the float data type, which is also known as the double data type. This data type is used to store numeric values that contain a decimal point

To showcase this you can see the following code:

<?php

   $number2 = 800.85;

   var_dump($number2);

?>

You get the following output:

As you can see, we stored a floating number inside a variable and we were able to print it onto the screen.

String DataType

String data types are used to store textual data. Strings are essentially the combination of characters enclosed inside the quotation marks. Use the following lines of code to showcase the working of strings:

<?php

  $text = "This is a LinuxHint Tutorial";

  var_dump($text);

?>

You get the following output:

As you can see, the compiler is showing us that the variable is of the data type string with 28 characters inside it. Right after that, the actual string is displayed on the screen as well.

Boolean DataType

The Boolean data type, similar to other programming languages, is used to display one of the two possible states, either on or off(True or False). To test out the boolean data type try the following lines of code:

<?php

     $boVar = true;

     var_dump($boVar);

     $boVar2 = false;

     var_dump($boVar2);

?>

We are declaring two variables and putting different boolean values inside them. When you run the above code snippet, you get the following output:

The compiler displays the type of the variable and its value.

Array DataType

To understand the array better, we will have a very brief explanation of what a variable is; a Variable is a named memory location that is used to store the program’s data, now the array is used to store multiple data values under the same variable name.

To create an array, you need to use the keyword array and put the values inside round parentheses like:

<?php

   $person = array("John","Doe",1995);

   var_dump($person);

?>

In the above code, you are creating a variable named person and information about a person like its first name, last name, and year of birth. When you execute the following code, you get the output:

Now, you can see a few things, the first is the digit enclosed inside the square brackets, this digit represents the index number of this value, and then you have a data type and the value on that index.

A PHP Object

The next data type is the Php Object, just like in any other programming language, objects are used with the help of classes to implement the concept of object-oriented programming, Imagine a class containing the information about a person, You can create this class with the following lines of code:

<?php

classPerson {
public $name;
public $yob;
publicfunction__construct($name, $yob) {
$this->name = $name;
$this->yob = $yob;
  }
publicfunctionmessage() {
return"The person is " . $this->name ." born in " .$this->yob ."!";
  }
}

As you can see, our class has two properties, name and year of birth, and one function that prints the name and the year the person was born.

Now we can create the object using the following lines:

$person1 = new Person("John Doe ", "1995");

echo $person1 -> message();

The complete code snippet is as:

<?php
classPerson {
public $name;
public $yob;
publicfunction__construct($name, $yob) {
$this->name = $name;
$this->yob = $yob;
  }
publicfunctionmessage() {
return"The person is " . $this->name ." born in " .$this->yob ."!";
  }
}

$person1  =new Person("John Doe ", "1995");
echo $person1 -> message();
?>

You get the following output when you execute the above code:

As you can see, you successfully created an object using a class and printed out its properties onto the screen.

Null DataType

This data type is used to store only one value and that is null; If the variable is not given a value then it is considered to be a NULL value.

Type the following code to test out the Null data type:

<?php

$hello = null;

var_dump($hello);

?>

You will see:

As you can see the type of the variable is null.

Conclusion

Variables in Php can be of different data types depending on the value that they are storing inside them; We looked at each data type offered by the Php programming language and tested them out with examples. Variables are the most essential element when it comes to programming. That is why knowing how variables work in Php is crucial for becoming good in Php.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.