~ Data types in PHP -PHP Coders point- Learn php online

Data types in PHP

PHP Data Types
  • PHP variables can store data of different types, and different data types can do different things.
  • PHP datatypes categorized in 3 types.
    • Predefined datatype
      1. Integer
      2. string
      3. float
      4. boolean
    • User-defined datatype
      1. Array
      2. Object
    • Special Types
      1. NULL
      2. Resource
    Predefined datatype
    1.Integer
    • An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
    • Rules for integers:
      • An integer must have at least one digit
      • An integer must not have a decimal point
      • An integer can be either positive or negative
      • Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation
      • <!DOCTYPE html>
        <html>
        <body>
        
        <?php
        $decimal = 51;
        $octal = 0151;
        $hexadecimal = 0x51;
        
        echo "Decimal numbe : " .$decimal. "<br>";
        echo "Octal number to decimal number: " .$octal. "<br>";
        echo "Hexadecimal number to decimal number: " .$hexadecimal. "<br>";
        ?>
        </body>
        </html>
        
    2.String
    • A string is a sequence of characters, for example "Hello PHP coders".
    • A string holds letters or any alphabets, numbers, and even special characters, so the String is a non-numeric data type.
    • A string can be any text inside quotes. You can use single or double quotes.
    • <!DOCTYPE html>
      <html>
      <body>
      
      <?php
      $frt = "Hello PHP coders";
      $sec = 'learn PHP from phpcoderspoint.com';
      
      echo $frt;
      echo "<br>";
      echo $sec;
      ?>
      
      </body>
      </html>
      
3.Float
  • A floating-point number is a number with a decimal point or a number in exponential form.
  • A string holds letters or any alphabets, numbers, and even special characters, so the String is a non-numeric data type.
  • A string can be any text inside quotes. You can use single or double quotes.
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $number1 = 50.35;
    $number2 = 38.45;
    
    $tot = $number1 - $number2;
    echo "Subtraction of floating numbers: " .$tot;
    ?>
    
    </body>
    </html>
    
4.Boolean
  • Booleans are the simplest data type.
  • A Boolean represents two possible states: TRUE or FALSE.
  • $ab = true;
    $cd = false;



User-defined datatype
1.Array
  • An array stores multiple values in one single variable, so the array is a compound data type.
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $name = array("Ram","Sita","Laxman");
    
    var_dump($name);
    echo "<br/>"; echo "name 1: $name[0] <br/>";
    echo "name 2: $name[2] <br/>";
    echo "name 4: $name[2] <br/>";
    ?>
    
    </body>
    </html>
    
2.Object
  • An object is a data type which stores data and information on how to process that data.
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    class name {
    function model() {
    $name = "Ram";
    echo "Name : " .$name;
    }
    }
    $obj = new name();
    $obj -> model();
    
    ?>
    
    </body>
    </html>
    
Special Types
1.NULL
  • Null is a special data type which can have only one value: NULL.
  • A variable of data type NULL is a variable that has no value assigned to it.
  • Tip: If a variable is created without a value, it is automatically assigned a value of NULL.
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $x = "Hello world!";
    $x = null;
    var_dump($x);
    ?>
    
    </body>
    </html>
    
2.Resource
  • The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.
  • A common example of using the resource data type is a database call.
  • We will not talk about the resource type here, since it is an advanced topic.