WikiGalaxy

Personalize

PHP Data Types

Example 1: Integer

Explanation:

Integers are whole numbers without a decimal point. In PHP, an integer can be positive or negative.


      <?php
      $integerVar = 42;
      echo $integerVar;
      ?>
    

Console Output:

42

Example 2: Float

Explanation:

Floats, also known as floating point numbers, are numbers with a decimal point or a number in exponential form.


      <?php
      $floatVar = 3.14;
      echo $floatVar;
      ?>
    

Console Output:

3.14

Example 3: String

Explanation:

Strings are sequences of characters, enclosed in single or double quotes. They are used to store textual data.


      <?php
      $stringVar = "Hello, World!";
      echo $stringVar;
      ?>
    

Console Output:

Hello, World!

Example 4: Boolean

Explanation:

Boolean data types represent two states: TRUE or FALSE. They are often used in conditional testing.


      <?php
      $boolVar = true;
      echo $boolVar; // Outputs 1 for TRUE
      ?>
    

Console Output:

1

Example 5: Array

Explanation:

Arrays in PHP are used to store multiple values in a single variable. They can be indexed or associative.


      <?php
      $arrayVar = array("apple", "banana", "cherry");
      echo $arrayVar[1]; // Outputs banana
      ?>
    

Console Output:

banana

Example 6: Object

Explanation:

Objects are instances of classes, which can hold both data (properties) and functions (methods).


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

Console Output:

VW

Example 7: NULL

Explanation:

The NULL data type represents a variable with no value. It is the only possible value of type NULL.


      <?php
      $nullVar = NULL;
      var_dump($nullVar);
      ?>
    

Console Output:

NULL

Example 8: Resource

Explanation:

Resources are special variables holding references to external resources like database connections.


      <?php
      $handle = fopen("file.txt", "r");
      var_dump($handle);
      fclose($handle);
      ?>
    

Console Output:

resource(4) of type (stream)

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025