1

I am trying to set three variables from a test function.

Here's my code so far:

function test()
{
    $x = 1;
    $y = 2;
    $z = 3; 


}

test();

# should print '1', '2', and '3'
echo $x; 
echo $y;
echo $z;
4
  • 1
    you would have to make your variable global, or return the values from the function in the form of an array. Commented Jan 30, 2012 at 19:00
  • What's with all the downvotes? Commented Jan 30, 2012 at 19:12
  • Maybe because OP has made no effort to search for the answer... it's asked here on almost a daily basis Commented Jan 30, 2012 at 19:28
  • Yeah strange all the answers bar 1 have downvotes... Commented Jan 30, 2012 at 20:14

5 Answers 5

4

Just return an object or an array (array is probably the way to go):

function test(){
   $data = array(
     'x' => 1,
     'y' => 2,
     'z' => 3
   );

   return $data;

}

print_r( test() );

or call each value:

echo $data['x'];
...
Sign up to request clarification or add additional context in comments.

2 Comments

or print_r($data) to print out all the variables from the output array
oh yes ... i didn't see that you already put that there. sorry
3

There'd be 3 easy-to-apply options in this case. One would be to pass the variables by reference, in stead of by value. The other would be to return an array. Another option would be to use global variables.
Here's an example of both:

By reference

<?php
function example (&$x, &$y, &$z)
{
    $x = 1;
    $y = 2;
    $z = 3;
}
?>

Passing a variable by reference means that you're passing the actual variable (the space allocated for it in the computer's memory), in stead of by value (just passing the value to the function) as usual.
So when you pass a variable by reference (which the & character does), and you manipulate the value, the value of the original variable gets changed as well.

Returning an array

<?php
function example ($x, $y, $z)
{
    $arr['x'] = 1;
    $arr['y'] = 2;
    $arr['z'] = 3;

    return $arr;
}
?>

Now you can access the values by using $arr['x'];, $arr['y']; and $arr['z'];.

Global variables

<?php
$x = 0;
$y = 0;
$z = 0;

function example ()
{
    global $x, $y, $z;

    $x = 1;
    $y = 2;
    $z = 3;
}
?>

1 Comment

Reference worked perfect for my use case. Thanks for the different explanations.
2

options:

use references:

function test(&$x, &$y, &$z) {
    $x = 1;
    $y = 2;
    $z = 3;
}

or return an array and use extract:

function test() {
    return array(
        'x' => 1,
        'y' => 2,
        'z' => 3
    );
}

extract(test());

Comments

1

Define the variables as global? Like:

function test()
{
    global $x, $y, $z;
    $x = 1;
    $y = 2;
    $z = 3; 
}

5 Comments

Picked this answer for its simpleness
thnx, simple question simple answer :)
This answer is incorrect tho, as a global var is never returned, and it's really poor programming to go this route...
well, this works codepad.viper-7.com/iIzWP7 the variables don't have to be returned... going with an array as you suggested is not the best solution too...
btw @Jakub, it is based on a example featured on php.net/manual/en/language.variables.scope.php in Example #1 Using global so I suggest that you notify the php.net team about their poor programming too...
1
function func ()
{
    return array (
        'x' => 1,
        'y' => 3,
        'z' => 42
    );
}


extract (func ());

echo $x, $y, $z;

or

function func ()
{
    return array (
         1,
         3,
         42
    );
}

list ($x, $y, $z) = func ();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.