PHP Variables Data Type with Example of Int, Float, String and Boolean

PHP Variable Data Types

PHP Variables data type is used to define which type of variable you stored in your variables. PHP Supports 8 different data types of variables.

Followings are the different types of PHP Variable Type.

  • String
  • Integer
  • Float/Double
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

PHP String Variable Type

String is a set of characters. For Example- “I like to drink water.”

In other words a string is a continuous set of characters that include alphabet, numbers, spaces and special symbols.

You have to write “string” inside a single quotes or double quotes.

Example 2 – String Variable with echo function

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$text = "Today is too hot day.";
		$a = "I like to drink water."
		$b = "I like to travel.";
		$c = 'A';
		
		echo $a;
		echo "<br>";
		echo $b;
		echo "<br>";
		echo $c;
		echo "<br>";
		echo $text;
	?>
  </body>
</html>

In the above examples, we declare some variables($text, $a, $b, $c) and take the output of variables on the web page using echo.

Where all the variables are string type variables. And echo function is used to get output on the web page.

PHP String Variables Types
Fig.2 – String Variables Types

PHP Int Variable Data Type (Integer)

Interger Variable Type is a “numerical” data type. It is a whole positive and negative number data type. The decimal and fractional values are not accepted.

Integer data type must content at least one digit negative or positive value.

Example 3 – Integer Variable

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$a = 7634578;
		$b = -557383;
		$c = 6473;
		
		echo $a;
		echo "<br>";
		echo $b;
		echo "<br>";
		echo $c;
	?>
  </body>
</html>

In the above examples, we declare some variables($a, $b, $c) and take the output of variables on the web page using echo function.

Here the value of the variables $a, $b, $c are integer type because the values are a whole numbers.

PHP Int Data Type Variable
Fig.3 – Integer Type Variable.

PHP Float Variable Data Type

Float Variable Type is a data type which contains the decimal(or point) value. The negative and positive both type of decimal values are used.

Example 4 – Float Variable Syntax and Code with echo function

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$a = 7.78;
		$b = -5.83;
		$c = 64.3;
		
		echo $a;
		echo "<br>";
		echo $b;
		echo "<br>";
		echo $c;
	?>
  </body>
</html>

In the above examples, we have some variables($a, $b, $c) and we provide values to those variables. And that variables values are given in the decimal. So the type of variables is a float type.

PHP Float and Boolean Variable Data Type
Fig.4 – Float Data Type

PHP Boolean Variable Data Type

Boolean is a digital type Variable. It can attain one of the two values(1 or 0).

In another words Boolean Type Variable is a data type which is shows apply one of values from 2 value, such as true/false, yes/no, 1/0.

Example 5 – Boolean Variable

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$a = true;
		$b = false;
	?>
  </body>
</html>

In the above examples, we have two variable i.e. $a and $b and we provide true and false value to that variable. And these variables will be called when the requirement. But these variables call depend on the parameter of condition.

Caution:Boolean Type Variables are used in the conditional statement. Where you want only one result from two condition.

Array Variable Data Type

Array is used to store the multiple values in a single variable.

In other words Array is used to store more than one values in a single variable.

Example 6 – Array Type Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$color = array("blue", "green", "yellow", "black");
		$state = array("Madhya Pradesh", "Maharashtra", "Punjab");
		
		echo $color[0];
		echo "<br>";
		echo $color[1];
		echo "<br>";
		echo $color[2];
		echo "<br>";
		echo $color[3];
		echo "<br>";
		echo $state[0];
		echo "<br>";
		echo $state[1];
		echo "<br>";
		echo $state[2];
	?>
  </body>
</html>

In the above examples, we declare 2 array variables which are $color and $state.

And print the value of array through the echo function.

Array Data Type
Fig.6 – Array Variable Data Type.

Object Variable Data Type

The Object Variable term came out from the concept of the object oriented programing lagugages like c++.

You have to declare a class and then you can create object of that class. Class have properties and methods. Methods is nothing but you can say functions.

Example 7 – Object Type

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		class ball 
		{
			function ball() 
			{
				$this->color = "Red";
			}
		}
// creating object
		$x = new ball();

// calling of object
		echo $x->color;
	?>
  </body>
</html>

In the above examples, we are creating a class of name ball and object is used to call the function of that class.

And print the value of class through the echo function.

Object Data Type
Fig.7 – Object Variable Data Type.

NULL Variable Data Type

NULL Variable is used to apply the null value.

NULL Variable is used to assign no value to the variable.

When you declared a variable without a value than the default value of that variable is NULL.

Example 8 – NULL Variable Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$a = 5;
		echo $a;
        echo "<br>";
		$a = 10;
        echo $a;
        echo "<br>";
		$a = NULL;
        echo $a;
	?>
  </body>
</html>

In the above examples, we create a variable $a. After that we assign the value(5) to that variable and print the variable on the web page than value of that variable shows 5.

Again we assign a value(10) to that variable and print the value of variable on the web page than the value of variable shows 10.

Again we assign a value(NULL) to that variable and print the value of variable on the web page than the value of variable shows nothings.

NULL Data Type
Fig.8 – NULL Variable Data Type.

Resource Variable Data Type

Resource variable is a special data type variable, which is used to store reference of an external resource.

One of the examples of the Resource type variable is used to access the database or a file resource.

You can leave a response, or trackback from your own site.
Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!

Powered by Wordpress