Jump to content

Recommended Posts

how would I do so?

I did some googling and found this..


<?php 
function addandsubtract ($firstvalue, $secondvalue){ 
    $firstreturnvalue = ($firstvalue + $secondvalue); 
    $secondreturnvalue = ($firstvalue - $secondvalue); 
    $myarray = array (); 
    $myarray[0] = $firstreturnvalue; 
    $myarray[1] = $secondreturnvalue; 
    return $myarray; 
} 
$myarray = array (); 
$myarray = addandsubtract (10, 3); 
echo $myarray[0] . "<br />";  
echo $myarray[1];  
?>

is there a way possible to do t his using less code or...?


echoing each entry of the array in the function, outside of the function

 



<?php

$one = 20;
$two = 25;
$three = 30;

function variable($a, $b, $c){
$a = $b + 10;
$b = $c + 15;
return $b;
}

$variable = variable($one, $two, $three);

echo $variable;

?>

 

the above echos the value for $b

 


<?php

$one = 20;
$two = 25;
$three = 30;

function variable($a, $b, $c){
$a = $b + 10;
$b = $c + 15;
return array($a, $b);
}

$variable = variable($one, $two, $three);

echo $variable;

?>

 

that echos Array

 

print_r shows both the $a and $b values, how would I echo just the $b value or just the $a value?

 

Thanks

Ahhh I see I see, I don't know why that didnt register to me :P

 

now is it possible to echo it without doing

 


$variable = variable($one, $two, $three);

 

just curious as to if its possible, thank you for the help btw, youre great :D

Ahhh I see I see, I don't know why that didnt register to me :P

 

now is it possible to echo it without doing

 


$variable = variable($one, $two, $three);

 

just curious as to if its possible, thank you for the help btw, youre great :D

I do not understand what you mean. Can you rephrase that question?

I mean do I have to use

 


$variable = variable($one, $two, $three);

 

to access the array values the are being returned in the function?

 

because to access what is being returned in the function I would have to do $variable[0], $variable[1], etc

If you don't want to deal with arrays another way to return more than one variable with a function is to use by reference with the ampersand symbol in front of the arguments of the function:

 

function return_multiple(&$variable1, &$variable2, &$variable3) {

    $variable1 = 'something';

    .....

}

 

Call like this: return_multiple($var1, $var2, $var3)

 

That will copy values into the variables you entered as arguments.

Nope. & indicates that instead of working on a copy of the variable, work on the variable directly, allowing us to manipulate its value.

 

I think you're making the problem look more complicated than it actually is:

 

function addandsubtract($firstvalue, $secondvalue) {
   return array($firstvalue + $secondvalue, $firstvalue - $secondvalue);
}

$myarray = addandsubtract(10, 3);

echo "$myarray[0]<br />$myarray[1]";

I did not write this

 

<?php 
function addandsubtract ($firstvalue, $secondvalue){ 
    $firstreturnvalue = ($firstvalue + $secondvalue); 
    $secondreturnvalue = ($firstvalue - $secondvalue); 
    $myarray = array (); 
    $myarray[0] = $firstreturnvalue; 
    $myarray[1] = $secondreturnvalue; 
    return $myarray; 
} 
$myarray = array (); 
$myarray = addandsubtract (10, 3); 
echo $myarray[0] . "<br />";  
echo $myarray[1];  
?>

 

It was just one of the very few results I found when I was googling how to return in a function,trying to a find a solution to my problem

 

I think you have a basic misconception of what the arguments passed to function actually do. Due to variable scope, if the arguments aren't explicitly passed to the function, they are not available to the function. And no, I'm not going to advocate the use of 'global' to circumvent that.

 

This will do nothing, as the values of the variables are not available within the function.

$var1 = 10;
$var2 = 20;
$var3 = 30;

function AddEmUp() {
$total = ($var1 + $var2 + $var3);
return $total;
}
echo AddEmUp();

 

Likewise this will do nothing (except throw a warning), because even though the arguments are defined, they are not passed to the function when it is called.

$var1 = 10;
$var2 = 20;
$var3 = 30;

function AddEmUp($var1, $var2, $var3) { // arguments are defined, but do not take the values of variables above
$total = ($var1 + $var2 + $var3); // same as above. These vars are only relevant within the scope of the function
return $total;
}
echo AddEmUp(); // no values passed to the function as arguments

 

Only when the arguments are passed to the function in the function call does the function have the values available to it.

$var1 = 10;
$var2 = 20;
$var3 = 30;

function AddEmUp($var1, $var2, $var3) { // arguments are defined, but do not take the values of variables above because of scope
$total = ($var1 + $var2 + $var3); // same as above. These vars are only relevant within the scope of the function
return $total;
}
echo AddEmUp($var1, $var2, $var3); // previously assigned values are now passed to the function as arguments
// will echo '60'

/****  OR ****/
$result = AddEmUp($var1, $var2, $var3); // $result will now have a value of 60.

 

A clearer, less confusing way to write the function that clearly shows the separation would be like this:

$var1 = 10;
$var2 = 20;
$var3 = 30;

function AddEmUp($arg1, $arg2, $arg3) {
$total = ($arg1 + $arg2 + $arg3);
return $total;
}
echo AddEmUp($var1, $var2, $var3);

 

Hopefully that clears things up a little bit.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.