Broad Network


PHP Special Types and Pseudo Types

PHP Data Types with Security Considerations - Part 3

Foreword: In this part of the series, I give you the basics of PHP special types and pseudo types.

By: Chrysanthus Date Published: 16 Oct 2018

Introduction

This is part 3 of my series, PHP Data Types with Security Considerations. There are two special types in PHP, which are the NULL and the Resource types. Pseudo Types are types which do not really exist. In this part of the series, I give you the basics of PHP special types and pseudo types. You should have read the previous parts of the series before coming here, as this is the continuation.

The NULL Type
NULL means nothing. This value is case-insensitive. So, NULL, null, Null, nuLL mean the same thing.

NULL is the only possible value of the type NULL.

In the code,

<?php

    $var;

?>

$var is assigned the value, NULL, by PHP, unknown to you.

You can also actually assign the value, NULL to a variable. Try the following code:

<?php

    $var;

    echo $var;

?>

I tried it in my computer and the NULL value was not printed.

The unset() Function
When a non-NULL value has been assigned to a variable, the unset() function can be used to replace the value of the variable with NULL. Try the following code:

<?php

    $var = 48;

    unset($var);

    echo $var;

?>

The output is NULL.

The unset() function returns no value.

NEVER FORGET THIS: NULL is a valid value in PHP.

The Resource Type

What is a Resource?
A resource is usually external to the computer. A file is a resource. An image you see on the computer screen, is from an image file. Data byte stream coming in from the network, is a resource. Database (a set of tables with data) is a resource. A sound played by the computer is from a file. A sound file is a resource.

A file in the computer you are using is a resource.

In PHP, a resource is a variable, like $var, but refers to an actual resource. I will explain how such variables are used in a different series, in this volume.  

Is_type Functions

The is_null() Function
This function returns true if its argument is NULL and false if it is not. Try the following code:

<?php

    $var1 = null;
    $var2 = 'some text';

    if (is_null($var1))
        echo 'it is null', '<br>';
    else
        echo 'it is not null', '<br>';
    if (is_null($var2))
        echo 'it is null', '<br>';
    else
        echo 'it is not null', '<br>';

?>

The output is:

    it is null
    it is not null

The is_resource() Function
The following statement will return a resource on success and false on error:

    $handle = fopen('c:/temp.txt', 'r');

Try the following code:

<?php

    $handle = fopen('c:/temp.txt', 'r');
    $var2 = 'some text';

    if (is_resource($handle))
        echo 'it is resource', '<br>';
    else
        echo 'it is not resource', '<br>';
    if (is_resource($var2))
        echo 'it is resource', '<br>';
    else
        echo 'it is not resource', '<br>';

?>

The output is:

    it is not resource
    it is not resource

because the path and file, 'c:/temp.txt' does not exist.

Pseudo Types
Pseudo types are known as mixed, number and callback.

The Mixed Type
An argument to a function may be an integer, a float, a string, a Boolean, an array, an object, a resource or a NULL. If a function would take two or more of these types, for a particular argument, then we say the function takes a mixed type.

The return value to a function may be an integer, a float, a string, a Boolean, an array, an object, a resource or a NULL. If a function would return two or more of these types, we say the function returns a mixed type.

Pseudo types are for readability and documentation. They are not coded in PHP script. You will find pseudo types in PHP documentation syntaxes, but not in PHP code.

The Number Type
An integer or a float can be called a number type.

The Callback Type

Callback Function
In a callback function scheme, there are two functions: the main function and a smaller function, which is the callback function. In the body of the main function, certain values become arguments to the callback function call. The callback function is actually executed when the main function is called. The body of the callback function is coded in the call of the main function. An argument for the callback function may be any of the types above, except the pseudo types. Read and try the following code:

<?php

    function mainfn($dummyVariable, $callBFn)
        {
            $str= 'We are learning.';
            $colorArr = array('blue', 'green', 'red');

            $callBFn($str, $colorArr);
        }

    mainfn('dummy', function($arg0, $arg1)
        {
            echo $arg0, '<br>';
            echo $arg1[0], ' ', $arg1[1], ' ', $arg1[2];
        });    //main function call

?>

In the main function definition, the callback function is identified by the variable, $callBFn. The callback function is called in the main function with arguments: $str and $colorArr. However, the callback function is defined in the call of the main function. So, when the main function is called, the definition of the main function calls the callback function. The definition of the callback function is an argument of the call of the main function. The definition of the callback function here, takes no function name (but it needs the reserved word, function).
So, the callback function is defined in the call of the main function, while it is called in the definition of the main function.

In PHP documentation, you would see the word, callback, preceding the no-name function definition, in the main function call (above). The word, callback, is a pseudo type.

That is it for this part of the series.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK

Comments



Send the Writer a Message