The user-defined data types are

  • Array
  • Objects

Array

An array is formally defined as an indexed collection of data values. Each index (also known as the key) of an array is unique and references a corresponding value.

Example :

 <!DOCTYPE html>
<html lang="en">
<head>
    <title>PHP Arrays</title>
</head>
<body>

<?php
$colors = array("Red", "Green", "Blue");
var_dump($colors);
echo "<br>";
 
$color_codes = array(
    "Red" => "#ff0000",
    "Green" => "#00ff00",
    "Blue" => "#0000ff"
);
var_dump($color_codes);
?>

</body>
</html>

Output:-
array(3) { [0]=> string(3) "Red" [1]=> string(5) "Green" [2]=> string(4) "Blue" }
array(3) { ["Red"]=> string(7) "#ff0000" ["Green"]=> string(7) "#00ff00" ["Blue"]=> string(7) "#0000ff"

Objects

An Object is an individual instance of the data structure defined by a class. We define a class once and then make many objects that belong to it. Objects are also known as instances

Example :

 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Car Information</title>
</head>
<body>

<?php

// Define a simple class
class Car {
    // Properties
    public $brand;
    public $model;

    // Constructor
    public function __construct($brand, $model) {
        $this->brand = $brand;
        $this->model = $model;
    }

    // Method
    public function startEngine() {
        return "Engine started for {$this->brand} {$this->model}.";
    }
}

// Instantiate objects based on the class
$car1 = new Car("Toyota", "Camry");
$car2 = new Car("Ford", "Mustang");

// Access and manipulate object properties
$car1->brand = "Honda";

// Call object methods
$message1 = $car1->startEngine();
$message2 = $car2->startEngine();
?>

<!-- Display the output within the HTML structure -->
<h1>Car Information</h1>

<p><?php echo $message1; ?></p>
<p><?php echo $message2; ?></p>

</body>
</html>

Output :-

Car Information
Engine started for Honda Camry.
Engine started for Ford Mustang.

The special data types are

  • NULL
  • Resource

Null

In PHP, NULL is a special constant that represents the absence of a value or an uninitialized variable. It is a data type on its own and is used to indicate that a variable has no assigned value.

Example :

 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NULL</title>
</head>
<body>

<?php
$name = NULL;
if ($name === NULL) {
    echo '<p> variable is NULL.</p>';
}
?>

</body>
</html>

Output:

variable is NULL.

Resource

A resource in PHP is a special data type that represents a handle to external entities, such as files, database connections, network sockets, and more. Resources are typically created and manipulated by functions provided by PHP extensions or external libraries.

Example :-

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Handling Example</title>
</head>
<body>

<?php

// Open a file and get a file handle resource
$fileHandle = fopen('example.txt', 'r');

// Check if the variable is a resource
if (is_resource($fileHandle)) {
    echo '<p>File handle is a resource.</p>';

    // Read content from the file
    $content = fread($fileHandle, filesize('example.txt'));
    echo '<p>File content: ' . $content . '</p>';

    // Close the file handle to release the resource
    fclose($fileHandle);
} else {
    echo '<p>Not a valid resource.</p>';
}

?>

</body>
</html>

Output :- 

Not a valid resource.

Leave a Comment