Broad Network


Null and Void and Resources in PHP

PHP Data Types Simplified – Part 3

Forward: In this part of the series we look at the data types, NULL, Void and Resource.

By: Chrysanthus Date Published: 10 Aug 2012

Introduction

This is part 3 of my series, PHP Data Types Simplified. In this part of the series we look at the data types, NULL, Void and Resource. I will also comment on the data types, Array and object.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

NULL
A variable is considered to be null if:
it has been assigned the constant, NULL.
it has not been set to any value yet.
it has been unset().

NULL is case-insensitive. So you can type NULL as null. The following program illustrates the first point. Read and try it:

<?php

    $var = null;
    echo $var;

?>

The value, null, cannot be displayed. So nothing is displayed for the above variable, $var. The following program illustrates the second point:

<?php

    $var;
    echo $var;

?>

For the variable, you just have a declaration. The echo statement displays nothing. There is a predefined function called, unset. It removes any assigned value to a variable. The following program illustrates this:

<?php

    $var = 45;
    unset($var);
    echo $var;

?>

After unsetting a variable, an echo statement for the variable displays nothing.

So if no value is assigned to a variable, the value of the variable is NULL; if the constant, NULL is assigned to a variable, the value of the variable is NULL; if the value was assigned to a variable and then the variable was unset with the unset function, then the new value of the variable is NULL.

Null is a data type whose only possible value is null.

void
void means empty. NULL and void have similar meanings, but they are used in different situations. void is used as a return data type for a function or as the only parameter in a function. As a return data type of a function, it means any return value is useless; such a function should not return anything. As the only parameter it means the function does not take any argument. You would normally find the use of void in function syntax and not in program code.

Resource
This is what the specification says about the data type, resource: “A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions.” A resource is code in memory that represents an input or output device, such as the hard disk. It can also represent a file in the hard disk. If you do not understand PHP reference, then read the series I wrote in this blog titled, “Understanding PHP Reference”.

Array and Object
To have basic knowledge about the array data tape, read the article in this blog, titled, PHP Array. It is part of the my series, “Basics of PHP”. To have knowledge about the object data type, read my series, titled, “Object Oriented Programming in PHP”.

Let us take a break now and continue in the next part of the series.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message