I was wondering if PHP can do this as there seems to be no good solution to it yet:
p($i)
and it will print
$i is 5
and
p(1 + 2)
will print
1 + 2 is 3
and
p($i * 2) => $i * 2 is 10
p(3 * factorial(3)) => 3 * factorial(3) is 18
C and Ruby both can do it… in C, it can be done by stringification, and in Ruby, there is a solution using p{'i'} or p{'1 + 2'} (by passing the block with the binding over, to do an eval)… I wonder in PHP, is it possible too?
I think it could be done by taking a backtrace then loading and tokenizing the file that calls
p(). I wouldn’t call it a “good” solution though.Of course you could stringify it yourself…