Return Multiple Values From Function Using Arrays In PHP

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 Tutorial: Return Multiple Values From Function Using Arrays In PHP


[youtube https://www.youtube.com/watch?v=HsX2ay702R8]

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


Work Flow: Logic

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.

Source Code: Return Multiple Values From Function Using Arrays In PHP

<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.

PHP Comments, String Concatenation, Addition of Numbers & Hello World

Today lets see these 4 basic things:
1. The classic “Hello World” program.
2. Comment system in PHP.
3. Concatenation of Strings.
4. Addition of Numbers.



Classic “Hello World” program
Program to output simple string

1
2
3
<?php
          echo "Hello World";
?>

Comments:

1
// This is single line comment
1
# This is also a single line comment
1
2
3
/* This is how multi-line comments
    are written.
*/

Anything following a double forward slash or a hash( # ) symbol will be ignored by your parser. Comments are very important and are often ignored by programmers. Comments come handy when we come back to the codes and want to make some changes to the coding. So its always a good practice to write comments wherever appropriate.

Concatenation of Strings
Concatination is joining of two or more strings end to end. Ex: if we concatenate Micro and soft, it will become Microsoft.
In PHP, we use .[DOT] operator for concatenation. Below is an example:

1
2
3
4
<?php 
         echo "Micro" . "soft";
         echo "<br />Oracle, " . "Sun Microsystems";
?>

The output for the above php code is:

1
2
Microsoft
Oracle, Sun Microsystems

Addition of Numbers
We can use variables for addition of numbers or we can simply make use of echo or print for addition of numbers.

1
2
3
<?php
         echo 10+20;
?>

This gives 30 as the output.

1
2
3
<?php
         echo "5"+10;
?>

This gives 15 as output. The string “5” will be implicitly converted into number and will be added to the actual number 10.

First PHP program & Worst Practices To Avoid

We can embed php inside html tags and html tags inside php codes, but whatever it is – your php file name must end with a .php extension, so that the parser can identify that the file contains php code and it must also look for

<?php and ?>

codes inorder to recognise the codes as PHP codes and execute them.

Note: The output of PHP code is html.

Some Worst Practices to avoid:
Some people may use

<? 
    some_php_codes(); 
?>

OR

<?= 
    some_php_codes(); 
?>

OR ASP style( Active Server Pages – from Microsoft Inc )

<% 
    some_php_codes(); 
%>

OR

<%= 
    some_php_codes(); 
%>

and whole lot of other things.

All these or atleast some of these styles may even work on your computer, and that’s because of the way your php.ini file is configured. The same file may not work on other web servers.
So the best practice is to stick with using

<?php 
    your_php_code; 
?>

Also note that, white space isn’t significant in PHP. i.e., you can introduce any number of spaces, tabs, and new line characters in between your PHP codes.
But there must be atleast a single space after

<?php



Our First PHP program:

<?php
          phpinfo();
?>

Make sure you have saved the file with ( filename.php ) .php extension. Now open the saved file in your web browser.
If your server is working, then you must see a screen that gives information about your web server and PHP.
Output Screenshot:
phpinfo

Don’t make this file available on your server for general public, as it gives a lot of information about your server. Its a handy tool to quickly scan your server configuration.