Find out name of the variable passed to function

Uses debug_backtrace() to trace to current code line and find variable name. Read more.

php
fork add to bundle source
function get_variable_name( $var, $with_dollar = true ) { $backtrace = debug_backtrace()[0]; $fh = fopen($backtrace['file'], 'r'); $line = 0; while (++$line <= $backtrace['line']) { $code = fgets($fh); } preg_match('/' . __FUNCTION__ . '\s*\(\s*(\$([a-z_0-9]+))/misu', $code, $name); return $with_dollar ? $name[1] : $name[2]; }

usage

load_function('get_variable_name'); $possible_variable_option = []; echo get_variable_name($possible_variable_option); // possible_variable_option
← how to use this in app?
create log ...inout