PHP Data Types

Posted By Anchal Goel | 17-Dec-2014

PHP supports following data type

 

  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

 

PHP String

 

- PHP is a sequence of characters

- It can be any text inside quotes, yoy can use single or double quotes

 

Input

<?php

$x = "Hi PHP !";

$y = 'Hi PHP !';

 

echo $x;

echo "<br>";

echo $y;

?>

 

Result

Hi PHP !

Hi PHP !

 

PHP Integer

 

- Integer is a whole number without decimals

- It is a number between -2,147,483,648 and +2,147,483,647

- Rules for integers

  • Integer must have at least one digit (0-9)
  • Cannot contain comma or blanks
  • Must not have a decimal point
  • Can be either positive or negative
  • Can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)

 

 

Input

<?php

$x = 55;

var_dump ($x);

?>

 

Result

int(55)

 

PHP Float

 

- Float (floating point number) is a number with a decimal point or a number in exponential form

 

Input

<?php

$x = 55.5;

var_dump ($x);

?>

 

Result

float(55.5)

 

PHP Boolean

 

Boolean represents two possible states: TRUE or FALSE

 

Input

$x = True;

$y = False;

- Boolean are often used in conditional testing

 

PHP Array

 

- Array stores multiple values in one single variable

 

Input

<?php

$animals = array("cat","dog", "lion");

var_dump ($animals);

?>

 

Result

array(3) { [0]=> string(3) "cat" [1]=> string(3) "dog" [2]=> string(4) "lion" }

 

PHP Object

 

- Object is a data type which stores data and information on how to process that data

- Must be explicitly declared

- First we must declare a class of object

For this, we use the class keyword

- Class is a structure that can contain properties and methods

 

Input

<?php

class Car {

          function Car () {

                   this->model = "BMW";

    }

}

// create an object 

$herbie = new Car ();

 

// show object properties

echo $herbie->model;

?>

 

Result

BMW

 

PHP NULL Value

 

- Null is a special data type which can have only one value: NULL

- Variable of data type NULL is a variable that has no value assigned to it

- If a variable is created without a value, it is automatically assigned a value of NULL

- Variables can also be emptied by setting the value to NULL

 

Input

<?php

$x = "Hi PHP !";

$x = null;

var_dump ($x);

?>

 

Result

NULL

 

PHP Resource

 

- Special resource type is not an actual data type.

- It is the storing of a reference to functions and resources external to PHP.

- Common example of using the resource data type is a database call.

 


Thanks

Request for Proposal

Recaptcha is required.

Sending message..