Data Types


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


PHP supports the following data types:


  • string

  • integer

  • float

  • boolean

  • array

  • object

  • resource

  • NULL


String


Strings hold characters like "Hello World"


PHP strings are case-sensitive.


A string can be any text inside quotes. You can use single or double quotes:


            <?php 
                $x = "Hello world!";
                $y = 'Hello world!';

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

Output


            Hello world!
Hello world!

Integer


Integers hold whole numbers, either positive or negative, such as 1, -50, 12345678,etc. The limit of integer is between -2147483647 and 2147483647. The integer numbers outside the range are automatically converted to floats.


            <?php 
                 $x = 1578;
                 var_dump($x);
            ?>
        

Output


            int(1578)
        


Float


A float (floating point number) is a number with a decimal point or a number in exponential form.


            <?php 
                $x = 17.357;
                var_dump($x);
            ?>
        

Output


            float(17.357)
        

Boolean


A Boolean represents two possible states: TRUE or FALSE. Boolean is only used for test the condition.


            $x = true;
            $y = false;
        

Array


An array stores multiple values in one single variable.


            <?php 
                 $number = array("one","two","three");
                 var_dump($number);
            ?>
        

Output


            array(3) {
  [0]=>
  string(3) "one"
  [1]=>
  string(3) "two"
  [2]=>
  string(5) "three"
}
        

Object


Objects are complex variables that have multiple values, and they can have their own functions.


an object must be explicitly declared.


We must declare a class object fot use class keyword.


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

                // create an object
                $obj = new number();

                // show object properties
                echo $obj->model;
            ?>
        

Output


            one        


NULL


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


To give a variable the NULL value, simply assign it like this--


            $variable = null;
            $variable = NULL;
        

Resource


Resources might be picture data, or the result of an SQL query. We should free up resources after using them.


The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.


References


Reference type variables are actually pointer to the real data. Once two variables are pointing to the same data, you can change either variable and the other one will also update.


When you use the = (assignment) operator, PHP takes the value from operand two and copies it into operand one.


Syntax


To assign by reference, you need to use the reference operator (&) after the equals operator (=), giving =&.


            <?PHP
                $a = 10; 
                $b =& $a;                                                                                                 
                print $a;                                                                                   
                print $b;                                                                                   

                ++$a; 
                print $a; 
                print "\n";
                print $b; 
                print "\n";
                ++$b; 
                print $a; 
                print "\n";
                print $b; 
                print "\n";
            ?>
        

Output


            101011
11
12
12
        

objects are passed and assigned by reference by default. Assigning an object variable is actually copying its object handle, which means the copy will reference the same object as the original.


References allow a function to work directly on a variable rather than on a copy.





PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion