1

I want to write a condition where I want to know if my function needs a return value or it can be executed as a procedure. Basically it should look like:

foo($x) {
    $x++;
    echo $x;
    if(is_return_needed()) {
        return $x;
    }
}

where is_return_needed() is the condition if a return value is needed.

And how it should work:

echo foo(50); // should print AND return 51
bar(foo(50)); // should print AND return 51 to bar() function
foo(50); // should only print the value, because the returned value will not be used

And please don't tell me there's no reason to do this. I know I can send an additional boolean argument to the function which will be the condition, but is there a better way to achieve this?

Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
  • why don't you return $x in any case? – csupnig Jun 10 '12 at 22:00
  • 3
    Exactly, if the return value won't be used it won't make any problem to the code. Making some weird context checking will surely slow down your code. – Zefiryn Jun 10 '12 at 22:01
  • and if you really want something like that, just do: `function foo($x, $return_needed=false) { ... return ($return_needed) ? $x : null ; }`, but as @Zefiryn said, it just doesn't matter... – giorgio Jun 10 '12 at 22:06
  • 1
    @Vlakarados there is no need to do this. PHP's function implementation works this way without our help, this "optimization" on userspace functions side is useless. – ioseb Jun 10 '12 at 22:07
  • @giorgio he said he don't want to use extra argument for that. I personally do not know how this could be checked. – Zefiryn Jun 10 '12 at 22:08
  • possible duplicate of [Caller function in PHP 5?](http://stackoverflow.com/questions/190421/caller-function-in-php-5) – kapa Jun 10 '12 at 22:19
  • @Zefiryn and ioseb I just wonder if returning a value will affect performance in any way - memory usage or load times, if it does, then it is a problem as I must iterate huge amounts of data with pretty complicated processing for each iteration. As I tried to do some benchmarking and if the function returns a huge string(50000+ characters) load times are about 10-15% higher than without returning anything, that's why it bothers me. Could someone please check if they will have the same situation or is it something I'm doing wrong? – Sergey Telshevsky Jun 10 '12 at 23:31
  • @bažmegakapa thank you, haven't found it before posting, will look in to it – Sergey Telshevsky Jun 10 '12 at 23:32
  • @Zefiryn excuse me, read over it... – giorgio Jun 11 '12 at 11:15

1 Answers1

1

Returning an object (or a large string), PHP will not make a copy of that object/string. That means returning alone, will not slow down your application. I found an article that explains it pretty well.

If the function can avoid building this large object at all, it will become faster. If you change the result of the function outside, or change it just before returning, it will become slower (is has to do the copy then). Building a long string with always adding a small part is very expensive, because every time it has to allocate a new big block of memory.

That said, we would have to see your code, to understand the slowdown you described. Returning a result only sometimes, is a very bad advice, every developer using your code will have a hard time to understand this behaviour. Sooner or later your application will become unstable if you use this often. Actually i find it even dangerous to return mixed typed values, as PHP often does itself.

martinstoeckli
  • 23,430
  • 6
  • 56
  • 87