USER-DEFINED FUNCTIONS


php128 apg

EXECUTE user-determined procedures at RUNTIME.





You may have noticed that certain exercises, proposed in this tutorial, are very labor-intensive, which is not in line with good programming practice.

Well, now is the time to explain how to simplify the processing of a given script using functions defined at programming time.




The following are some examples of user-defined functions, which may be different with each new display on this page.


<?php

 
function name_by_user $arg1$arg2, ..., $argN )
 
 {
      
PROCEDURE
      
      
return $retval;
 }



where,


$arg1$arg2, ..., $argN = LIST OF ARGUMENTS 
                              REQUIRED TO IMPLEMENT PROCEDURE
                                        
                              variable number of arguments 
                              
and default arguments are supported

name_by_user 
NAME OF THE FUNCTION 
                           
PROVIDED BY THE USER
                                  
CASE SENSITIVE

PROCEDURE 
Any valid PHP code 
                           used to fulfill a particular purpose

$retval 
VALUE RETURNED BY THE FUNCTION

?>

FUNCTIONS


Function names follow the same rules as other labels and they are case sensitive.

A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

As a regular expression, it would be expressed thus:

[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*


All functions feature global scope.

It is not possible to undefine or redefine previously declared functions, since PHP does not support overload.

It is possible to operate with the value returned by a function. therefore, recursion is allowed.

However, infinite recursion is considered a programming error.

In the following pages we will show you the use of some very simple functions.



  1 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - 
   This is a simple example using 
   a function: isstring -> user-defined
   to display if the variable
   is a STRING
   - - - - - - - - - - - - - - - - - - - - - - - */
   
$var01a $_SERVER['REMOTE_ADDR'];
   
$var01b $_SERVER['HTTP_USER_AGENT'];
 
$var01c $_SERVER['HTTP_ACCEPT_LANGUAGE'];
 
$var01d M_E;
 
$var01e 1.6e-19
 
$var01f = [ 'c' => 299792458'G' => 6.67428E-11 ]; 
  
$var01g NULL;


function 
isstring($var)
{
    

if(
is_string($var))
    echo 
$var '<br>is a STRING!<br><br>';
else
{
    
var_dump($var);
    echo 
'<br>is NOT a STRING!<br><br>';
}
}    
    
isstring($var01a);

isstring($var01b);

isstring($var01c);

isstring($var01d);

isstring($var01e);

isstring($var01f);

isstring($var01g);

?>

 RESULT   

::1
is a STRING!

Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0
is a STRING!

pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
is a STRING!

float(2.718281828459)
is NOT a STRING!

float(1.6E-19)
is NOT a STRING!

array(2) { ["c"]=> int(299792458) ["G"]=> float(6.67428E-11) }
is NOT a STRING!

NULL
is NOT a STRING!


  2 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - 
   This is a simple example using 
   a function: ISbool -> user-defined
   to display if the variable
   is a BOOLEAN value
   - - - - - - - - - - - - - - - - - - - - - - - */

function ISbool ($var)
{
    if(
is_bool($var))
    {
        
var_dump($var);
        echo 
'<br>is a BOOLEAN value!<br><br>';
    }
    else
    {
        
var_dump($var);
        echo 
'<br>is NOT a BOOLEAN value!<br><br>';
    }
}

   
$v01 $_SERVER['REMOTE_ADDR']; 
$v02 $_SERVER['HTTP_USER_AGENT'];
$v03 $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$v04 M_E;
$v05 1.6e-19
$v06 = [ 'c' => 299792458'G' => 6.67428E-11 ]; 
$v07 NULL
$v08 false;
$v09 FALSE;
$v10 true;
$v11 TRUE;

    
ISbool($v01);
    
ISbool($v02);
    
ISbool($v03);
    
ISbool($v04);
    
ISbool($v05);
    
ISbool($v06);
    
ISbool($v07);
    
ISbool($v08);
    
ISbool($v09);
    
ISbool($v10);
    
ISbool($v11);
    
?>

 RESULT   

string(3) "::1"
is NOT a BOOLEAN value!

string(78) "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"
is NOT a BOOLEAN value!

string(35) "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3"
is NOT a BOOLEAN value!

float(2.718281828459)
is NOT a BOOLEAN value!

float(1.6E-19)
is NOT a BOOLEAN value!

array(2) { ["c"]=> int(299792458) ["G"]=> float(6.67428E-11) }
is NOT a BOOLEAN value!

NULL
is NOT a BOOLEAN value!

bool(false)
is a BOOLEAN value!

bool(false)
is a BOOLEAN value!

bool(true)
is a BOOLEAN value!

bool(true)
is a BOOLEAN value!


  3 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - 
   This is a simple example using 
   a function: ISnull -> user-defined
   to display if the variable
   is a NULL value
   - - - - - - - - - - - - - - - - - - - - - - - */
   
$var03a $_SERVER['REMOTE_ADDR'];
   
$var03b $_SERVER['HTTP_USER_AGENT'];
 
$var03c $_SERVER['HTTP_ACCEPT_LANGUAGE'];
 
$var03d M_E;
 
$var03e 1.6e-19
 
$var03f = [ 'c' => 299792458'G' => 6.67428E-11 ]; 
  
$var03g NULL;


function 
ISnull($var)
{
    

if(
is_null($var))
    echo 
$var '<br>is a NULL value!<br><br>';
else
{
    
var_dump($var);
    echo 
'<br>is NOT a NULL value!<br><br>';
}
}    
    
ISnull($var03a);

ISnull($var03b);

ISnull($var03c);

ISnull($var03d);

ISnull($var03e);

ISnull($var03f);

ISnull($var03g);

?>

 RESULT   

string(3) "::1"
is NOT a NULL value!

string(78) "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"
is NOT a NULL value!

string(35) "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3"
is NOT a NULL value!

float(2.718281828459)
is NOT a NULL value!

float(1.6E-19)
is NOT a NULL value!

array(2) { ["c"]=> int(299792458) ["G"]=> float(6.67428E-11) }
is NOT a NULL value!


is a NULL value!


  4 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    This is a simple example using 
    a function: isnumeric -> user-defined
    to display if the variable
    is NUMERIC 
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
   
$var04a $_SERVER['REMOTE_ADDR'];
   
$var04b $_SERVER['HTTP_USER_AGENT'];
 
$var04c $_SERVER['HTTP_ACCEPT_LANGUAGE'];
 
$var04d M_E;
 
$var04e 1.6e-19
 
$var04f = [ 'c' => 299792458'G' => 6.67428E-11 ]; 
  
$var04g NULL;


function 
isnumeric($var)
{
    if(
is_numeric($var))
    {
    echo 
$var '<br>is a NUMERC value!<br><br>';
    }
else
{
    
var_dump($var);
    echo 
'<br>is NOT a NUMERIC value!<br><br>';
}
}

isnumeric($var04a);

isnumeric($var04b);

isnumeric($var04c);

isnumeric($var04d);

isnumeric($var04e);

isnumeric($var04f);

isnumeric($var04g);

?>

  5 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   The is_nan function accepts 
   only FLOAT NUMBER as a parameter.
   
   If this is not taken into account, 
   Warning or a Fatal error 
   will be issued.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function isnan ($par)
{
if (
is_nan($par) == TRUE)
{
var_dump($par);

echo 
'<br>"Variable IS NOT a number"<br><br>';
}
else
{
var_dump($par);

echo 
'<br><br><br>';
}
}    

$a acos(3);

isnan($a);

isnan(M_E);

$var05e 1.6e-19

isnan($var05e);

$arr05 = [ 24];

isnan($arr05);

?>

  6 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -
   This is a simple example using 
   a function: isarray -> user-defined
   to display if the variable
   is ARRAY
   - - - - - - - - - - - - - - - - - - - - - - - */
   
$var06a $_SERVER['REMOTE_ADDR'];
   
$var06b $_SERVER['HTTP_USER_AGENT'];
 
$var06c $_SERVER['HTTP_ACCEPT_LANGUAGE'];
 
$var06d M_E;
 
$var06e 1.6e-19
 
$var06f = [ 'c' => 299792458'G' => 6.67428E-11 ]; 
  
$var06g NULL;

$var06if false;

$var06iF FALSE;

$var06jt true;

$var06jT TRUE;

 
function 
isarray($var)
{

if(
is_array($var))
{
    
var_dump($var);
    echo 
'<br>This variable is an ARRAY!<br><br>';
}    
else
{
    
var_dump($var);
    echo 
'<br>This variable is NOT an ARRAY!<br><br>';    
}    
}

isarray($var06a);

isarray($var06b);

isarray($var06c);

isarray($var06d);

isarray($var06e);

isarray($var06f);

isarray($var06g);

isarray($var06if);

isarray($var06jT);

?>