how to define data type in php

By | October 20, 2019

Data-Type introduction -: Data types in “php ”language is rich in its data type. The storage representation and machine instruction to handle constant differ from machine to machine. the variety of data types available allow to the programmer to select the type approach appropriate to the needs of the application as well as the machine.

In other words we can say that the variable store different data to different things. Data Types defines the type of data a variable can store.

Types of data type in php -:PHP allows eight different types of data types . The list has been given here

  1. Integer data type
  2. Boolean data type
  3. string data type
  4. float data type
  5. Null data type
  6. resource data type
  7. Array data type
  8. object data type

 

Integer data type- –: Integer data type hold the whole number including positive and negative number. In the integer must have a number. the number must be define it is decimal,octal or hexadecimal. For example


<!DOCTYPE html>
<html>
<body>

  <?php
   $x = 2543;
   echo "integer value = $x";
  ?>

</body>
</html>

Output of this program

integer value =2543

Another example of integer data type in php-

<!DOCTYPE html>
<html>
<body>

<?php

// decimal base integers
$deci1 = 100;
$deci2 = 654;

// octal base integers
$octal1 = 06;

// hexadecimal base integers
$ctal = 0x43;

$sum = $deci1 + $deci2;
echo $sum;


?>

</body>
</html>

output of this program

754

 

Boolean data type in php-: A Boolean represent two possibility one is the condition is true and another is false.For example

 <?php

   if(true)
    echo "the condition is true";
   If(false)
    echo "the condition is false";
?>



The output of this program depend the condition.

String data type in php- : we have already use string data type in our early program.A string is a sequence of character.Like hello netnic

 <?php

   $x =  " welcome netnic ";

   echo $x;

?>

The output of string data type

welcome netnic

 

Float data type in php -:A float number is the real number. It is a decimal point number.For example 433.25,etc.

<?php

$x = 433.25;

echo $x;

?>

Output of this program

433.25

 

Null data type in php -: it is a special data type  which can have only one null value.

<?php

$x = "welcome netnic ";
$x=null;
var_dump($x);

?>

Output of this program

Null

Resources data type in php -: This is not the actual data type but it is the storing reference function and resources external of php.

Array data type in php- : Array is a multiple data compound that store multiple value of same data type. For example

<?php

$animals = array ("dogs","elephant","goat","cow ");

var_dump($animals);

?>

Output of this program

array(4) { [0]=> string(4) "dogs" [1]=> string(8) "elephant" [2]=> string(4) "goat" [3]=> string(4) "cow " }

 

Object data type in php-: This data type store the data and information of the variable.using the object it must be declare in the program for example

<?php

   class car
    {
       function car
         {
          $this->model ="BMW";
          }
     }

// create an object 
 
$newtopic = new Car();

//show object property
        
echo $newtopic->model;

?>

The output of this program

BMW

 

Leave a Reply

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