PHP Variables


Share

A variable is a name given to a memory area that stores information at runtime.
All factors in PHP are signified with a main dollar sign ($).
Variable names are case touchy; this implies $my_code is unique in relation to $MY_CODE
All factors names must beginning with a letter follow different characters.


PHP has a total of eight data types


1) Integers

They are whole numbers, without a decimal point, like 813.

    <?php
    echo PHP_INT_MAX;
    ?> 
    OUTPUT:
    9223372036854775807

2) Doubles

Integer can be in decimal (base 10), octal (base 8), and hexadecimal (base 16) format.

<?php
   $a = 5.20;
   $b = 5.10;
   $c = $a + $b;
   print("$a + $b = $c 
"); ?>
    OUTPUT:
    5.2 + 5.1 = 10.3 

3) Boolean

They have only two possible values either TRUE or FALSE.

if (TRUE)
   print("This will always print");
else
   print("This will never print");

4) Strings

A string is a collection of characters. String is one of the data types supported by PHP.

<?php
    var_dump('welcome to the dcodeck world');
?>
    OUTPUT:
    string(28) "welcome to the dcodeck world"

5) Array

Arrays are named and indexed collections of other values.

<?php
$code = array("html", "css", "js"); 
echo "I love " . $code[0] . ", " . $code[1] . " and " . $code[2] . ".";
?>
    OUTPUT:
    I love html, css and js. 

6) Objects

Objects are occurrences of developer characterized classes, which can bundle up both different sorts of value and functions that are explicit to the class.

<?php
class Dpk {
    function Dpk() {
        $this->text = "Coder";
    }
}
// create an object
$rwt = new Dpk();

// show object properties
echo $rwt->text;
?>
    OUTPUT:
    Coder

7) Null

NULL is an extraordinary information type which can have just one worth: NULL OR Invalid.
A variable of information type NULL is a variable that has no worth appointed to it.
If a variable is created without a value it is automatically assigned a value of NULL.
Factors can likewise be discharged by setting the incentive to NULL:

<?php
$x = "Hello coder!";
$x = null;
var_dump($x);
?>
    OUTPUT:
    NULL

8) Resources

Resources are special variables that hold references to resources external to PHP (such as database connections).