Mindmajix

Data Types in PHP

DATA TYPES

PHP supports mainly 8 kinds of data types among which four are stable data types, two are compound data types and another two are special data types

STABLE DATA TYPES:

i) int (or) integer       ii) Float (or) real (or) decimal         iii) Boolean data types

iv) string data types

i) int (or) integer:- all non decimal values . range in between 2(32-1) to -2(32-1)

ex: –  <?php

echo “<h1 align=’center’>Stable Data types</h1>”;

echo “<h2>Integer Data Type</h2>”;

$x = 10;

var_dump($x);echo ‘<br>’;

$x = (pow(2,32-1)-1);

var_dump($x); echo ‘<br>’;

$judge = 2147483647;

var_dump($judge);

?>

ii) Float (or) real (or) decimal: –all decimal values. Range in between 10(38) to 10(-38)

ex: –

<?php

echo “<h2>Float Data Type</h2>”;

$a = 10.25;

var_dump($a);echo ‘<br>’;

?>

iii) Boolean data types:-Boolean data types written in the form of true (or) false

Ex: –

         <?php

echo “<h2>Boolean Data Type</h2>”;

$a = false;

var_dump($a);

?>

iv) String data types: – a collection of characters enclose in between single (or) double cotations  are known as string data types

Ex: –   <?php

echo “<h2>String Data Type</h2>”;

$x = 10;

echo ‘<br>The Value of $x is ‘,$x,'<br>’;

echo “The Value of $x is :$x”;

$name = “Rajesh”;

echo ‘<br>my name is $name <br>’;

echo “my name is $name <br>”;

$x = 10;    $y = 20;

echo ‘The value of $x+$y = ‘,$x+$y,'<br>’;

echo “The value of $x+$y = “,$x+$y,'<br>’;

?>

 

COMPOUND DATA TYPES:-

compound data types again two types

  1. Array data type
  2. Object data type

1. ARRAY DATA TYPE:

Array data type which can hold more than one value. In php we have mainly 3 kinds of arrays

i) Numerical array

ii) Associative array

iii) Mixed array

Declaring an array in php can be done by two types

  • array as the function
  • array as a square bracket

i) Numerical array: –

A numeric array stores each array element with a numeric index.

There are two methods to create a numeric array.

1. In the following example the index are automatically assigned (the index starts at 0):

$cars=array(“Saab”,”Volvo”,”BMW”,”Toyota”);

2. In the following example we assign the index manually:

$cars[0]=”Saab”;
$cars[1]=”Volvo”;
$cars[2]=”BMW”;
$cars[3]=”Toyota”;

Ex: –   <?php
$cars[0]=”Saab”;
$cars[1]=”Volvo”;
$cars[2]=”BMW”;
$cars[3]=”Toyota”;
echo $cars[0] . ” and ” . $cars[1] . ” are Swedish cars.”;
?>

ii) Associative array: – An associative array, each ID key is associated with a value.

When storing data about specific named values, a numerical array is not always the best way to do it.

With associative arrays we can use the values as keys and assign values to them.

Example 1

In this example we use an array to assign ages to the different persons:

$ages = array(“Peter”=>32, “Quagmire”=>30, “Joe”=>34);

Example 2

This example is the same as example 1, but shows a different way of creating the array:

$ages[‘Peter’] = “32”;
$ages[‘Quagmire’] = “30”;
$ages[‘Joe’] = “34”;

The ID keys can be used in a script:

<?php
$ages[‘Peter’] = “32”;
$ages[‘Quagmire’] = “30”;
$ages[‘Joe’] = “34”;

echo “Peter is ” . $ages[‘Peter’] . ” years old.”;
?>

The code above will output:

Peter is 32 years old.

 

iii) Mixed array: – mixed array is nothing but combination of numerical & associative array  are known as mixed array

Ex:- mixedarrays.php

<?php

$a = array(“Name”=>”Rajesh”,”Age”=>30);

print_r($a);

echo ‘<hr>’;

echo ‘<h1 align=”center”>Mixed Arrays</h1>’;

define(‘A’,’ABC’);

define(‘B’,’XYZ’);

$c = 10;

$d = 20;

$g = array(0=>10,1=>20,’Name’=>’Raj’,’Age’=>30,5=>500,5.55=>600,-9=>’Negative value’,true=>200,false=>200,”=>’Empty Value’,NULL=>’Null Value’,A=>B,$c=>$d);

echo ‘<pre>’;

print_r($g);

echo ‘</pre>’;

?>

 

2. OBJECT DATATYPE:

object is an instance of class. Class can be denoted with in class name. Collection of variables and functions are known as class. Calling class name should be create an object variable then that particular variable is known as object variable

Ex: –   <?php

echo “<h2>Object Data Type</h2>”;

class A{

public $x = 10;

public $a = 20;

function test(){   echo ‘A test Function called<br>’;              }  }

$b = new A;

var_dump($b);

echo ‘<br>Public Value $x = ‘,$b->x,$b->test(),'<br>’;

?>

SPECIAL DATA TYPES:

Special data types are again two types

i) Resource data types

ii) Null data types

i) Resource data types:- resource data type is calling an external variable value into another variable using (&) resource variable

ii) Null data types:- this is the pre-defined key-word data type. When a variable not initialize and by asking through printing method. This will result in null value

ex: –

<?php

echo “<h1 align=’center’>Special Data types</h1>”;

echo “<h2>Reference Data Type</h2>”;

$x = 10;

$y = $x;

echo ‘The Value of $x = ‘,$x,'<br>The Value of $y = ‘,$y,'<br>’;

$x = 12;

echo ‘The Value of $x = ‘,$x,'<br>The Value of $y = ‘,$y,'<br>’;

$y = ‘Rajesh’;

echo ‘The Value of $x = ‘,$x,'<br>The Value of $y = ‘,$y,'<hr>’;

echo “<h2>Resourse Data Type</h2>”;

$a = 10;

$b = &$a;

echo ‘The Value of $a = ‘,$a,'<br>The Value of $b = ‘,$b,'<br>’;

$a = ‘Raj’;

echo ‘The Value of $a = ‘,$a,'<br>The Value of $b = ‘,$b,'<br>’;

$b = ‘Amith’;

echo ‘The Value of $a = ‘,$a,'<br>The Value of $b = ‘,$b,'<br>’;

echo “<h2>Null Data Type</h2>”;

var_dump($z);

?>

0 Responses on Data Types in PHP"

Leave a Message

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

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.