Pseudo-Types in PHP
PHP Data Types Simplified – Part 4
Forward: In this part of the series we look at PHP Pseudo types.
By: Chrysanthus Date Published: 10 Aug 2012
Introduction
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.
number
number is a pseudo type, which means that a parameter is either an integer or a float.
mixed
mixed is a pseudo type. This is what the specification says about the mixed pseudo type. “mixed indicates that a parameter may accept multiple (but not necessarily all) types.” mixed simply means an integer or a float or a string or a bool, etc. It may not mean all the different options.
void
This is a pseudo type. We saw it in the previous part of the series.
callback
call_user_func() and usort() are predefined functions. These two functions and some other predefined functions can take the name of a function as a parameter. Any function whose name goes as a parameter to these functions is called a callback function. I will use only the call_user_func() function to illustrate the behavior of a callback function.
The syntax of the call_user_func() function is:
mixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )
The callback function is any function that you define. The name of the callback function goes into the first position of the parameter list of the call_user_func() function. The rest of the parameters of the call_user_func() function are actually the arguments of the callback function. The return value of the call_user_func() function is actually the return value of the callback function. In the syntax, mixed, means any data type. Read and try the following program:
<?php
function square($par)
{
$sqr = $par * $par;
return $sqr;
}
$answer = call_user_func("square", 25);
echo $answer;
?>
You have the function, square(), which takes only one argument. This function squares the argument and returns the result. After that, the predefined call_user_func() function is executed. Its first argument is the name of the callback function and may or may not be in quotes. The second argument is actually the single argument of the callback function. If the callback function had more than one argument, then the rest of the arguments would start from the second argument position of the call_user_func() function.
When the call_user_func() function is executed, its returned value is actually the returned value of the callback function. Above, the return value of call_user_func() is assigned to the variable, $answer. The last statement echoes $answer.
Note: the call_user_func() function returns false if it fails.
That is it for this part of the series. Let us stop here and continue in the next part.
Chrys
Related Links
Major in Website DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT