
Monday 21 May 2012
how to return more than one value from a function in php
From w3schools.inHere is a cool trick you might not know if you want to return multiple values from a function and assign them to variables with different names each.
Here Is How It Works:
Say you have the following function returning an array.
function SomeFunction()
{
return array($value1, $value2, $value3);
}
Now to assign each value to a different variable, in the order in which you
returned them, do the following.
list($variable1,$variable2,$variable3)=SomeFunction();
Returning an Array of Associative Keys
You could just assign SomeFunction() to a variable, which would make it an array, but you would then have to access each value using numeric keys e.g $var[0] would hold $value1. A better approach if you want to do this would be to make associative keys in the returning array:
function SomeFunction()
{
return array('key1'=>$value1, 'key2'=>$value2,
'key3'=>$value3);
}
Usage:
$someArray=SomeFunction(); echo $someArray['key1']; // shows $value1
|
This page was last modified on 09 Jun 2011 at 23:39:03. |
Gautam Kumar EDP Manager |
Related Posts
» Useful functions to tighten the security in PHP» CodeIgniter A recommendation for PHP Programmer
» PHP Framworks Why when and which
» Solving Floating point number precision lost problem in PHP
» Handling array of HTML Form Elements in JavaScript and PHP
» How to filter user submitted data easily in PHP
» Prevent form post request from another domain in PHP
» Flaw in and or logical operator php
» Default arguments functions php
» Submit Form without Refreshing Page with Jquery
» MySQL Performance Tips
» Free ajax chat applications in PHP
» Return More Than One Value From a Function in PHP
» PHP Optimization Tips
» PHP Error Handling
» Useful PHP Classes and Libraries
» Useful Tools For PHP Developers
» PHP Frameworks
» Web based HTML Editors
» 301 redirect in PHP and .htaccess
» Hiding PHP file extension
» Rredirect Browser HTTPS SSL PHP
» Tighten php security functions
» PHP Framworks
