Document

Steph'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 function six() : a = 6

This is inside Function six() : a = 16

After function six() : a = 16

________________________________________

Before 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 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 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
)
________________________________________
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Steph's Various Function Examples</title>
    <link rel="stylesheet" href="../css/assignmentHeaderStyles.css" />
</head>
<body>

<?php

include('../header/assignmentHeader.php');

error_reporting(E_ALL);

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

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

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

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

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

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

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

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

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

function 
seven($a)
{
    echo(
"<p>This is inside Function seven()</p>");

    for (
$i = 1; $i < 10; $i++) {
        
$a[] = $i;
    }

    return 
$a;
}

function 
eight(&$a): void
{
    echo(
"<p>This is inside Function eight()</p>");

    for (
$i = 1; $i < 10; $i++) {
        
$a[] = $i;
    }
}

//=============== main code ===============

line();
one();

line();
two();

line();

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

line();

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

line();

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

line();

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

line();

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

$a = seven($a);

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

line();

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

eight($a);

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

line();

$b = array();
echo(
"<p>Before function eight</p>");
echo(
'<pre>');
print_r($b);
echo(
'</pre>');

eight($b);

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

line();

show_source(__FILE__);

include(
'../footer/footer.php');

?>

</body>
</html>