Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:-


  1. integer
  2. float 
  3. string
  4. Boolean 
  5. array 
  6. object 
  7. NULL
Now we will discuss these data types in details.

String Data type:- 


A string is a sequence of characters, like "PHP training".A string can be any text inside quotes. You can use single or double quotes:-

<?php 
$x = "PHP training";
$y = 'PHP training';

echo $x;
echo "<br>"
echo $y;
?>

Integer Data Type:-

An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.

Some Rules for integers:-
  1. An integer must have at least one digit
  2. An integer must not have a decimal point
  3. An integer can be either positive or negative
  4. Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)
In the following example $s is an integer. The PHP var_dump() function returns the data type and value:-

<?php 
$s = 4889;
var_dump($s);
?>

Float Data Type:-

A float (floating point number) is a number with a decimal point or a number in exponential form.
In the following example $s is a float. The PHP var_dump() function returns the data type and value:-

<?php 
$s = 30.433;
var_dump($s);
?>


Boolean Data Types:-

A Boolean represents two possible states: TRUE or FALSE.

$x = true;
$y = false;

Booleans are often used in conditional testing.

Array in PHP:-

An array stores multiple values in one single variable.In the following example $cars is an array. The PHP var_dump() function returns the data type and value:-

<?php 
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>


Object in PHP:-

An object is a data type which stores data and information on how to process that data.In PHP, an object must be explicitly declared.First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods:-

<?php
class Car {
    function Car() {
        $this->model = "VW";
    }
}

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

// show object propertiesecho $herbie->model;
?>

NULL Value in PHP:-

Null is a special data type which can have only one value: NULL. in NULL variable no value assign to it.if a variable is created without a value, it is atomically assign the Null value.Variables can also be emptied by setting the value to NULL:- 

<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>


0 comments:

Post a Comment

Thanks

 
Blogger Templates1 school © 2013. All Rights Reserved. Powered by Blogger
Top