2

I have a function defined like this: function test($var) { ... }.

Inside this function, I want to determine if I passed a variable or a value to it when I called it, for example:

  • test('hello world') should determine that I passed a string value.
  • test($hello_world) should determine that I passed a variable called $hello_world

This function just outputs the content that was passed and tells its data type, so, I currently get something like: String: "hello world" but I am looking to get something like String ($hello_world): "hello world".

How can I check inside function test in order to determine what was passed so that I change the output as required?

Kobus Myburgh
  • 1,114
  • 1
  • 17
  • 46
  • 3
    Out of curiosity, what's the use case for this? I feel like this might be an example of an XY problem – Brett Gregson Jan 31 '20 at 08:52
  • Like Brett, I can't think of a scenario where this would be useful. What are you going to use this idea for? You say "to determine what was passed so that I change the output as required"...but surely that depends on the _content_ of what was passed, not whether it came via a variable or not. – ADyson Jan 31 '20 at 08:57
  • 1
    FWIW I'm pretty sure it isn't possible - when you see `$var` in your function, all you know is what's in it, not how, syntactically, it got there. You're asking the runtime to know about a compile-time piece of info. And also your code _shouldn't_ care - a well-written function should be self-contained and re-usable, and therefore independent of the context in which it was invoked. – ADyson Jan 31 '20 at 09:00
  • Does this answer your question? [original variable name passed to function?](https://stackoverflow.com/questions/4503443/original-variable-name-passed-to-function) (Note: this is an old question, but I very much doubt the right answer changed.) – Jeto Jan 31 '20 at 09:27
  • @BrettGregson: the use case is that this function is a debug function, which I use similar to Laravel's `dd()` function to output some debug info on the screen. There could be a several places where this could be used when I debug a function, for example: `$data['current_application']` and `$data['previous_application']` These data structures are the same except but contains minor data differences (application ID, for example) and when I output both on the screen, I want a quick way to see which is which. – Kobus Myburgh Jan 31 '20 at 13:02
  • @ADyson, as explained in the comment to Brett Gregson, I am calling it as a debug output function, and I output several data structures that are very much the same, exceptfor minor data differences. So - without having to trace each application ID to see which is which, I wanted to see ID 1 is previous application, and ID 2 is current application. But these IDs are not necessarily that the previous one will have an ID smaller than the current one (e,g., you open an application, cancel it, then open a new one, then cancel it, then re-open the first one). – Kobus Myburgh Jan 31 '20 at 13:05
  • 1
    @KobusMyburgh here's a really bad proof of concept https://3v4l.org/YKSAj – Brett Gregson Jan 31 '20 at 13:19
  • 1
    @KobusMyburgh sounds more like a case for outputting a stack trace, so you can see which code called the function (and then you can check that code to see whether that code uses hard-coding or a variable as input to the function), and/or using an actual debugger such as XDebug to step through the code. Again you're asking the runtime to tell you something which is actually a syntax issue and as such is dealt with at build-time, not run-time. as Greg's demo suggests, what you're asking is basically impossible (e.g. the demo cannot tell a hard-coded number from a variable). – ADyson Jan 31 '20 at 13:44

1 Answers1

1

As a part of the solution you can use gettype function to know the type of the variable:

function test($var) {
    return gettype($var);
}


echo test('string')."\n";
echo test(true)."\n";
echo test([1, 2, 3])."\n";
$variable = 'test';
echo test($variable)."\n";

// Will output:
// string
// boolean
// array
// string

But I don't think there is a way of knowing whether it's a variable or not. It will print the type of the content of that var as you can see in the last execution. Once you are in the function that content is always a variable to that function.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • 3
    This is not what the OP wants. – nice_dev Jan 31 '20 at 09:03
  • 2
    @vivek_23 to be fair the answer does clarify that when it says " I don't think there is a way of knowing whether it's a variable or not". Although arguably that might be sufficient for the answer, since the OP doesn't appear to be interested in any of the rest of what's being suggested. – ADyson Jan 31 '20 at 09:03
  • @ADyson Then it should be a comment. – nice_dev Jan 31 '20 at 09:04
  • 1
    @vivek_23 maybe. Although "It's not possible, sorry" might be a perfectly correct answer to the question (ideally backed up with a bit of an explanation). – ADyson Jan 31 '20 at 09:06
  • Well, I was going to delete it as I only tried to help him with what I could but I got an upvote at the last moment... I leave it for now but if you still think I should delete it just tell me one more time and I'll do it. – jeprubio Jan 31 '20 at 09:10
  • Thanks - no, I wanted to see the variable name, and it does not appear to be possible. Thanks! I will use a second parameter to output there. – Kobus Myburgh Jan 31 '20 at 12:59