Data Type in PHP:

Data Type in PHP is classified into various categories according to its attributes. It defines as the type of data a variable can store.

  • Alphanumeric characters are defined as strings
  • Whole numbers are defined as integers
  • Numbers, as well as decimal points, are defined as floating points.
  • True or false values are defined as Boolean.

Integer:  

Integers hold only whole numbers which include positive and negative numbers that are without in fractional parts or decimal points. The integer number can be decimal (base 10), octal (base 8), or hexadecimal (base 16). The default base is decimal (base 10).  whole numbers e.g. -3, 0, 22. In integer the maximum value is platform-dependent.

<?php
// decimal base integers
$deci1 = 50;
$deci2 = 600;
// octal base integers
$octal1 = 07;
// hexadecimal base integers
$octal = 0x45;
$sum = $deci1 + $deci2;
echo $sum;
echo "\n\n";
?>
OUTPUT:
650

Floating point

This data type contains fractional or decimal parts including positive and negative numbers or in exponential form. The variables add a minimum number of decimal places. It is the exact as a float as floating-point numbers or real numbers.

<?php
 
$a = 50.85;
$b = 654.26;
 
$sum = $a+ $b;
 
echo $sum;
echo "\n\n";
 
//returns data type and value
var_dump($sum)
   
?>
OUTPUT:
705.11 float(705.11)

Boolean:

Boolean data types are the data types that are used in conditional testing. It holds two values: TRUE(1) or FALSE(0). The events which are successfully executed will return true and unsuccessful events return false. NULL type values are also considered false in Boolean. In this data type, 0 is also considered false. If there is an empty string then it is also considered false in the boolean data type.

<?php
if(TRUE)
    echo "This condition is TRUE";
if(FALSE)
    echo "This condition is FALSE";
?>
OUTPUT:
This condition is TRUE

Array

In PHP datatypes, the array is a container that can store multiple values of the same data type.  It mixes a series of data that are related together. Below is an example of an array.

<?php
$bikes = array("Hero", "Honda", "TVS");
echo "Example " . $bikes[0] . ", " . $bikes[1] . " and " . $bikes[2] . ".";
?>
OUTPUT: 
Example Volvo, BMW and Toyota.

How to Create an Array in PHP

  • In PHP, to create an array we used array().
  • EXAMPLE: array(“Hero”, “Honda”, “TVS”);
  • In PHP we consider three types of arrays:
  • Indexed arrays -In this, the arrays in which the elements are ordered based on an index. The index is also used to access or modify the elements which are in the array.

The syntax of an indexed array using the array() function is:

<?php
//indexed array
$newArray = ["apple", "coconut", "orange", "yellow", "guava"];
//access element using index
$element = $newArray[3];
echo $element;
?>
OUTPUT: 
yellow

Associative arrays  – 

 In PHP, associative arrays are very similar to numeric arrays but they are different in terms of the index method. In an associative array, the array is named with keys.

•         Associative elements are considered in the format “key” => “value”.

•         Example:

<?php 
    $marks = array("Maths"=>"99", "Science"=>"45", "HTML"=>"33");    
 echo "Obtained Marks in HTML = ".$marks['HTML']; 
?>
OUTPUT: 
Obtained Marks in HTML = 33

Multidimensional arrays

It is also called a Three Dimensional Array. This array has the same initialization as two-dimensional arrays. In another word, these arrays contain one or more arrays.

BikesStockSold
Hero24155
Honda15154
TVS50234
BMW357
<!DOCTYPE html>
<html>
<body>
<?php
$bikes = array (
  array("Hero",24,155),
  array("Honda",15,154),
  array("TVS",50,234),
  array("BMW",3,57)
);
  
echo $bikes[0][0].": In stock: ".$bikes[0][1].", sold: ".$bikes[0][2].".<br>";
echo $bikes[1][0].": In stock: ".$bikes[1][1].", sold: ".$bikes[1][2].".<br>";
echo $bikes[2][0].": In stock: ".$bikes[2][1].", sold: ".$bikes[2][2].".<br>";
echo $bikes[3][0].": In stock: ".$bikes[3][1].", sold: ".$bikes[3][2].".<br>";
?>
</body>
</html>
OUTPUT:
Hero: In stock: 24, sold: 155.
Honda: In stock: 15, sold: 154.
TVS: In stock: 50, sold: 234.
BMW: In stock: 3, sold: 57.

Sort Functions In Arrays:

  • sort() – In this function, sort arrays in ascending order
  • rsort() – In this function, sort arrays in descending order
  • asort() – In this function, sort associative arrays in ascending order, according to the value
  • ksort() – In this function, sort associative arrays in ascending order, according to the key
  • arsort() – In this function,  sort associative arrays in descending order, according to the value
  • krsort() – In this function,  sort associative arrays in descending order, according to the key
<!DOCTYPE html>
<html>
<body>
<?php
$bikes = array("Hero", "Honda", "TVS");
sort($bikes);
$clength = count($bikes);
for($x = 0; $x < $clength; $x++) {
  echo $bikes[$x];
  echo "<br>";
}
?>
</body>
</html>
OUTPUT:
Hero
Honda
TVS

For more read with us Oraltadalafil

Leave a Comment