Skip to main content

See also:

Using functions in a PHP program (part 8)

Demonstrates pass the right number of values
Demonstrates pass the right number of values
Ginny Putscher

As we continue with using functions in a PHP program, we will discuss and demonstrate how to pass the right number of values in a function. As you design a function, you must pass the right number of values. If you do not pass enough values to the function as anticipated, the function sets the missing one to NULL as shown in the example to the left:

function format_usaflag($firstcolor, $secondcolor, $thirdcolor)

{

$usaflag = "$thirdcolor, ".$firstcolor .$secondcolor;

echo $usaflag;

}

The function is anticipating three values to be passed. If you call the function with the following statement:

format_usaflag("white, blue");

The warning messages are displayed as follows:

Warning: Missing argument 2 for format_usaflag(), called in C:\wampserver\www\noofvalues.php on line 27 and defined in C:\wampserver\www\noofvalues.php on line 17

Notice: Undefined variable: thirdcolor in C:\wampserver\www\noofvalues.php on line 21

The script will continue to run even though the warnings are displayed, the script will produce the following output.

, white, blue

Please Note:

I have installed and used a newer version of the Wampserver package for this program. The newer version is Wampserver (64 bits & PHP 5.5) 2.5, which includes the newer releases of .. Apache : 2.4.9 MySQL : 5.6.17 PHP : 5.5.12 PHPMyAdmin : 4.1.14 SqlBuddy : 1.3.3 XDebug : 2.2.5

Please refer to the following articles for the download, install, configuration and fixing the security hole.

Download, install and configure the web development environment (part 1)

Download, install and configure the web development environment (part 2)

Download, install and configure the web development environment (part 3)

Fixing the security hole that corresponds to the MySql privileged accounts

NOTE: As demonstrated in the previous programs, we used notepad a plain text editor. The PHP programs are stored on our web server, "C:\Wampserver\www." Please note the stored web server location.

Now let us create the PHP program, please follow the steps outlined below:

  1. Select and click "Start."
  2. Select and click "All Programs."
  3. Select and click "Accessories."
  4. Select and click "Notepad."
  5. Type the PHP code exactly as shown below.

<html>

<head>

<title>Format Colors or a USA Flag</title>

</head>

<body><h1>Format a USA Flag</h1>

<h3>Demonstrates use of passing the right number of values to a function<h3>

<?php

function format_usaflag($firstcolor, $secondcolor, $thirdcolor)

{

$usaflag = "$thirdcolor, ".$firstcolor .$secondcolor;

echo $usaflag;

}

format_usaflag("white, blue");

?>

</body>

</html>

  1. Once you have type the entire program, Select "File," "Save As," and then go to the webroot of your server. For example, C:\Wampserver\www. Type Filename, noofvalues.php," and then Select "Save."

In the next article of the Using functions in a PHP program (part 9); we will continue with Passing values by reference in a PHP Program.