Austin's Various Function Examples

Demonstrates how variables may be passed to, and returned from, functions.

________________________________________

This is inside Function one()

________________________________________

This is inside Function two() a = 2

________________________________________

Prior to function three() : a = 3

This is inside Function three() : a = 3

After going to function three() : a = 3

________________________________________

Before function four() : a = 4

This is inside Function four() : a = 14

After going to function four() : a = 4

________________________________________

Before function five() : a = 5

This is inside Function five() : a = 15

Returned from function five() : a = 15

________________________________________

Before to function six() : a = 6

This is inside Function six() : a = 16

After function six() : a = 16

________________________________________

Before to function seven

Array
(
)

This is inside Function seven()

After function seven

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)
________________________________________

Before to function eight

Array
(
)

This is inside Function eight()

After function eight

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)
________________________________________

Before to function eight

Array
(
)

This is inside Function eight()

After function eight

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)
________________________________________

Code follows

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Functions</title>
    <link rel="stylesheet" href="../css/styles.css">
</head>
<body>

<div class="page">

    <p class="nav"><a href="../../index.php">Home</a></p>

    <?php
     
// code
    
error_reporting(E_ALL);    // set error reporting to all

    
echo("<h1>Austin's Various Function Examples</h1>");
    echo(
'<p>Demonstrates how variables may be passed to, and returned from, functions.</p>');

    
//============= functions ===============

    // function line
    // simple function that draws a line  ---------

    
function line()
    {
        echo(
str_repeat("_", 40));
    }


    
// function one ---------
    // simple function that echo's the program flow is inside the function

    
function one()
    {
        echo(
'<p>This is inside Function one() </p>');
    }


    
// function two ---------
    // simple function that assigns a variable and echo's that value inside the function

    
function two()
    {
        
$a = 2;
        echo(
"<p>This is inside Function two() a = $a</p>");
    }


    
// function three ---------
    // simple function that receives a value and echo's that value inside the function

    
function three($a)
    {
        echo(
"<p>This is inside Function three() : a = $a</p>");
    }


    
// function four ---------
    // simple function that receives a value, adds 10 to it, and then echo's that value inside
    // the function

    
function four($a)
    {
        
$a = $a + 10;
        echo(
"<p>This is inside Function four() : a = $a</p>");
    }

    
// function five ---------
    // a function that receives a value, adds 10 to it, echo's it, AND returns the value to the
    // main program

    
function five($a)
    {
        
$a = $a + 10;
        echo(
"<p>This is inside Function five() : a = $a</p>");
        return 
$a;        // note the return
    
}

    
// function six ---------
    // a function that receives a referenced variable, adds 10 to it, and echo's that value
    // NOTE: the value here is not returned, but rather the value of the referenced variable
    // is changed

    
function six(&$a)
    {
        
$a = $a + 10;
        echo(
"<p>This is inside Function six() : a = $a</p>");
        
// note there is NO return
    
}

    
// function seven
    // a function that receives an array, populates that array, and returns those values
    // to an array

    
function seven($a)
    {
        echo(
"<p>This is inside Function seven() </p>");
        for(
$i = 1; $i < 10; $i++)
        {
            
$a[] = $i;    // NOTE the [] where the index is automagically generated
        
}
        return 
$a;        // note the return
    
}

    
// function eight
    // a function that receives a reference to an array and populates that array.
    // NOTE: the values here are not returned, but rather the values of the referenced
    // array are changed

    
function eight(&$a)
    {
        echo(
"<p>This is inside Function eight() </p>");
        for(
$i = 1; $i < 10; $i++)
        {
            
$a[] = $i;
        }
        
// note there is NO return
    
}


    
//=============== main code ===============
    //            NOTE: the following code uses the above functions

    
line();    // =========== call to function line ============

    
one();    // call to function one

    
line();    // =========== call to function line ============

    
two();    // call to function two

    
line();    // =========== call to function line ============


    
$a = 3;
    echo(
"<p>Prior to function three() : a = $a</p>");

    
three($a);        // call to function three

    
echo("<p>After going to function three() : a = $a</p>");

    
line();    // =========== call to function line ============

    
$a = 4;
    echo(
"<p>Before function four() : a = $a</p>");

    
four($a);        // call to function four

    
echo("<p>After going to function four() : a = $a</p>");

    
line();    // =========== call to function line ============


    
$a = 5;
    echo(
"<p>Before function five() : a = $a</p>");

    
$a = five($a);        // call to function five --- NOTE the return value

    
echo("<p>Returned from function five() : a = $a</p>");

    
line();    // =========== call to function line ============

    
$a = 6;
    echo(
"<p>Before to function six() : a = $a</p>");

    
//  NOTE the & reference and NO return -- yet the variable is changed

    
six($a);        // call to function six

    
echo("<p>After function six() : a = $a</p>");

    
line();    // =========== call to function line ============

    
$a = array();    // init an array
    
echo("<p>Before to function seven</p>");
    echo(
'<pre>');
    
print_r($a);
    echo(
'</pre>');

    
$a = seven($a);        // call to function seven

    
echo("<p>After function seven</p>");
    echo(
'<pre>');
    
print_r($a);
    echo(
'</pre>');

    
line();    // =========== call to function line ============

    
$a = array();    // init an array
    
echo("<p>Before to function eight</p>");
    echo(
'<pre>');
    
print_r($a);
    echo(
'</pre>');

    
eight($a);        // call to function eight

    
echo("<p>After function eight</p>");
    echo(
'<pre>');
    
print_r($a);
    echo(
'</pre>');

    
line();    // =========== call to function line ============

    // NOTE: No initialization of any array
    
$b = array();
    echo(
"<p>Before to function eight</p>");
    echo(
'<pre>');
    
print_r($b);    // dump the array (not definded)
    
echo('</pre>');

    
// NOTE: the undefinded variable sent to the function
    
eight($b);        // call to function eight

    // NOTE: The above function populates the array even though the array was NOT defined
    
echo("<p>After function eight</p>");
    echo(
'<pre>');
    
print_r($b);
    echo(
'</pre>');

    
line();    // =========== call to function line ============

    
?>

    <hr>

    <h2>Code follows</h2>

    <div class="code"><?php highlight_file(__FILE__); ?></div>

    <p class="nav nav-foot"><a href="../../index.php">Home</a></p>

</div>

</body>
</html>