Asked  9 Months ago    Answers:  5   Viewed   45 times

I have a PHP function that returns something:

function myfunction() {
   $array = array('one', 'two', 'three', 'four');

   foreach($array as $i) {
     echo $i;
   }
}

And another function where I want to pass return values from the function above:

function myfunction2() {
   //how to send myfunction()'s output here? I mean:
   //echo 'onetwothreefour';
   return 'something additional';
}

I guess it will look something like myfunction2(myfunction) but I don't know PHP too much and I couldn't make it work.

 Answers

48

Yes, you just need

return myFunction();
Wednesday, March 31, 2021
 
Terry
answered 9 Months ago
94

At little late to the party, but for future reference, you can use the new ... operator (v5.6+) to explode the array inline:

$packed = pack("c*", ...$a);
Saturday, May 29, 2021
 
daiscog
answered 7 Months ago
67

You can't do it directly; you have to create a function that takes a va_list:

#include <stdarg.h>

static void exampleV(int b, va_list args);

void exampleA(int a, int b, ...)    // Renamed for consistency
{
    va_list args;
    do_something(a);                // Use argument a somehow
    va_start(args, b);
    exampleV(b, args);
    va_end(args);
}

void exampleB(int b, ...)
{
    va_list args;
    va_start(args, b);
    exampleV(b, args);
    va_end(args);
}

static void exampleV(int b, va_list args)
{
    ...whatever you planned to have exampleB do...
    ...except it calls neither va_start nor va_end...
}
Friday, June 4, 2021
 
RompelStompel
answered 7 Months ago
89

You can only return one value. But you can use an array that itself contains the other two values:

return array($uid, $sid);

Then you access the values like:

$ids = ids();
echo $ids[0];  // uid
echo $ids[1];  // sid

You could also use an associative array:

return array('uid' => $uid, 'sid' => $sid);

And accessing it:

$ids = ids();
echo $ids['uid'];
echo $ids['sid'];
Monday, June 28, 2021
 
Ula
answered 6 Months ago
Ula
22

add these code into the top of your script

@set_magic_quotes_runtime(false);
ini_set('magic_quotes_runtime', 0);
Wednesday, August 18, 2021
 
Warrior
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share