Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to return multiple values from a function in PHP

5 Answers

0 votes
function f(&$a, &$b, &$c) { 
    $a = 98;
    $b = 621;
    $c = 7;
} 
  

$a = 0; 
$b = 0;
$c = 0;
      
f($a, $b, $c); 

echo "a = " . $a . " b = " . $b . " c = " . $c; 



   
/*
run:
        
a = 98 b = 621 c = 7
 
*/

 



answered Aug 9, 2019 by avibootz
0 votes
function f() { 
    $a = 98;
    $b = 621;
    $c = 7;
    
    return array($a, $b, $c);
} 
   
 
$a = 0; 
$b = 0;
$c = 0;
       
echo f()[0];
echo "\n";

echo f()[1];
echo "\n";

echo f()[2];
 
 
 
    
/*
run:
         
98
621
7
  
*/

 



answered Aug 9, 2019 by avibootz
0 votes
function f() { 
    $a = 98;
    $b = 621;
    $c = 7;
    
    return array($a, $b, $c);
} 
   
 
$a = 0; 
$b = 0;
$c = 0;
       
list($a, $b, $c) = f();

echo $a . " " . $b . " " . $c;
 
 
 
    
/*
run:
         
98 621 7
  
*/

 



answered Aug 9, 2019 by avibootz
0 votes
function f() { 
    $a = 98;
    $b = 621;
    $c = 7;
    
    return array($a, $b, $c);
} 
   
$array = f();

$a = $array[0];
$b = $array[1];
$c = $array[2];
       
echo $a . " " . $b . " " . $c;
 
 
 
    
/*
run:
         
98 621 7
  
*/
  

 



answered Aug 9, 2019 by avibootz
0 votes
function f() { 
    return array('a' => 98,
                 'b' => 621,
                 'c' => 7);
} 
   
$array = f();

$a = $array['a'];
$b = $array['b'];
$c = $array['c'];
       
echo $a . " " . $b . " " . $c;
 
 
    
/*
run:
         
98 621 7
  
*/

 



answered Aug 9, 2019 by avibootz

Related questions

5 answers 359 views
1 answer 85 views
4 answers 110 views
4 answers 118 views
1 answer 61 views
1 answer 79 views
1 answer 74 views
...