Return Multiple Values From Function Using Arrays In PHP

Advertisement:


If you are a C/C++ programmer, then you know that functions can not return more than one value. To over come this, they use Call by Reference – where address of the variables is passed, hence whenever the value in the passed address changes, the value of the actual variable changes without anything being returned.

In PHP we can accomplish this with the help of arrays.

Video Explaining The PHP Code:



YouTube Link: http://www.youtube.com/watch?v=HsX2ay702R8 [Watch the Video In Full Screen.]


Work Flow:
Step 1: Here we pass the values to the function.

Step 2: Calculation or operation takes place inside the function.

Step 3: In case there are multiple things to return, we store it inside a array variable. Ex: $result = array($add, $sub, $mul, $div);

Step 4: We return the array variable.

Step 5: Now using array index we display the individual result using echo or print statement.

PHP program explained in above video:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<html>
 <head><title>Return Multiple Values From Functions, 
                                           using Arrays: In PHP</title></head>
 <body>
 
	<?php
		$get_result = calc(20, 10);
 
		echo "Addition: ".$get_result[0]."<br />Subtration :".$get_result[1].
			"<br />Multiplication: ".$get_result[2]."
                         <br />Division: ".$get_result[3];
		echo "<br /><br />";
 
		print_r ($get_result);
 
	?>
 
	<?php
		function calc($one, $two)
		{
			$add = $one + $two;
			$sub  =  $one  -  $two;
			$mul = $one * $two;
			$div  =  $one  / $two;
 
			$result = array($add, $sub, $mul, $div);
 
			return $result;
		}
	?>
 
 </body>
</html>

Output:
Addition: 30
Subtration :10
Multiplication: 200
Division: 2

Array ( [0] => 30 [1] => 10 [2] => 200 [3] => 2 )

Things To Remember:
1. All PHP files must have .php file extension.

2. Concatination operator in PHP is . [DOT]

3. print_r is a function which takes array variable as its argument and displays the input array structure. Ex: print_r ($get_result);

4. Function is defined in php using the keyword function, followed by function name.

5. $ is used to declare variables in PHP.

Array Syntax In PHP:
$variable = array(arrayElements);

For Numbers: Ex: $num = array(20, 40, 50, 60, 100);
For Strings: Ex: $str = array(“Microsoft”,”Apple”,”Oracle”,”Google”);
For Character: Ex: $chr = array(‘A’,'E’,'I’,'O’,'U’);

array is a keyword for declaring/initializing arrays in PHP.


Related posts:







Get FREE blog updates to your email inbox. Enter your email ID and subscribe:


Do not forget to click the verification link in your email, after subscribing.
--Thanks for your support

Start Making Money From Your Programming Skills


( Free ebook )
ebook cover
  • Subscribe to Technotip.com blog update and you will be able to download "Tips, Tricks and Strategies to Make Money Online" eBook for free.
  • You will also receive tips to improve your programming skills, and strategies to make money from your programming skills. We will also send useful resources for learning and building your application.
  • You can also make money with us, by just recommending our website to your friends and family. You will get complete strategy for making $300 and more in the ebook that we will send you - once you subscribe to our free blog update using the below form..

One Response to “Return Multiple Values From Function Using Arrays In PHP”

  1. Is there a way to return two arrays with a function? like sorta like this but i can’t really test this code right now…

    $n = “Jim”;
    $p = “Hi”;
    $i = 1;
    $array1 = test($n,$p,$i)[0];
    $array2 = test($n,$p,$i)[1];
    echo $array1[0]; //would output Jim
    echo $array1[1]; //Would output Hi
    echo $array1[2]; //would output 1
    echo $array2[0]; //would output 2
    echo $array2[1]; //would output 0
    function test($name,$pass,$id)
    {
    $user = array($name,$pass,$id);
    $blah = array($id + 1,0);
    $return = array($user,$blah);

    return $return;
    }

Leave a Reply