4

in bash there is something like

$_

which is a temporary variable that stores the last argument of previous command

is there a similar variable or construct in PHP or how can I access the last used variable in the script?

this would be very handy for simple debugging functions or if you need a function that you have to add in several places at your code.

Or you could add something like:

do_some_more($_); 

in several places in your code

rubo77
  • 19,527
  • 31
  • 134
  • 226

1 Answers1

0

There is no such feature in PHP.

$_ is just like any other variable.

$_ = 'hello world';

echo $_; // hello world
Casper André Casse
  • 573
  • 2
  • 8
  • 20
  • sure, but that was not the question. I tried $_ before I asked here – rubo77 Nov 05 '13 at 12:29
  • 2
    @rubo The answer is *"There is no such feature in PHP."* – deceze Nov 05 '13 at 12:32
  • I could imagin a construct that finds out with the help of debug_backtrace like here: http://stackoverflow.com/a/19788805/1069083 – rubo77 Nov 05 '13 at 12:37
  • @rubo Maybe so, and if you write a custom PHP extension you may be able to add a "real" `$_` to PHP. But the point is that there's no such thing built in, and as several people pointed out in the comments, the exact function of bash's `$_` is hardly applicable to PHP to begin with. – deceze Nov 05 '13 at 14:24
  • How would such a custom PHP extension look like? – rubo77 Nov 05 '13 at 17:15
  • 1
    @rubo If you have to ask, don't even go there. And define unambiguously how exactly it should work to begin with (see http://stackoverflow.com/questions/19788891/how-do-i-get-the-last-used-variable-in-php/19788977#comment29415589_19788891). – deceze Nov 05 '13 at 17:57