Skip to main content
  1. Tech
  2. Gadgets & Tech
  3. Tech Gear

Using functions in a PHP program (part 6)

See also

As we continue with Using functions in a PHP program (part 6), let’s discuss and show an example of how to pass the right type of values to a function. When passing values to a function, the right types must be used, such as passing values directly, computed values, or passing variables containing values. Listed below are valid examples of calls:

compute_salestax(5000, “VA”);

compute_salestax(3*1000, “ ”);

compute_salestax(5000, “C” . “A”);

You can pass values of any data type. The PHP programming code listed below demonstrates passing the right types of values to a function. For this example, we will use a function to compute the sum of two numbers. As discussed previously in Using a variable in a PHP program, the same rules apply. Please refer to the article for a refresher.

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>Sum of Numbers</title>

</head>

<body><h1>Sum of Numbers</h1>

<h3>Demonstrates use of passing variables to functions<h3>

<?php

function add_numbers($numbers)

{

if(is_array($numbers))

{

for($i=0;$i <sizeof ($numbers); $i++)

{

@$sum = $sum + $numbers[$i];

}

echo $sum;

}

else

{

echo "value passed is not an a array";

return;

}

}

$numbers = array(400,600);

add_numbers($numbers);

?>

</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, calcsumofnumbers.php," and then Select "Save."

In the next article of the Using functions in a PHP program (part 7); we will continue with Passing values in the correct order in a function in a PHP Program.

Advertisement

Don't Miss

  • Unity
    'Assassin's Creed Unity' preview: Ubisoft comes home to its urban origins
    Games Preview
  • Smart vending machine
    This smart vending machine will recommend drinks for you based on your gender and age
    Video
    Tech Buzz
  • Destiny
    The 'Destiny' beta: 7 things we absolutely love about Bungie's new franchise
    Games Feature
  • Wi-Fi
    Find out how to stretch your Wi-Fi signal where it has never gone before
    Tech Tips
  • Unity Exclusive
    'Assassin's Creed Unity' exclusive: Tell the world, 'I'm coming home'
    Games Interview
  • Gamer ghost
    Gamer finds and plays with ghost of deceased father on a classic game
    Video
    Headlines