How to get a variable name as a string in PHP

0 votes

i have this PHP code:

$FooBar = "a string";

i then need a function like this:

print_var_name($FooBar);

which prints:

FooBar

Any Ideas how to achieve this? Is this even possible in PHP?

Sep 29, 2020 in Laravel by kartik
• 37,510 points
2,058 views

1 answer to this question.

0 votes

Hello @kartik,

You can use this:

function varName( $v ) {
    $trace = debug_backtrace();
    $vLine = file( __FILE__ );
    $fLine = $vLine[ $trace[0]['line'] - 1 ];
    preg_match( "#\\$(\w+)#", $fLine, $match );
    print_r( $match );
}

$foo = "knight";
$bar = array( 1, 2, 3 );
$baz = 12345;

varName( $foo );
varName( $bar );
varName( $baz );

?>

// Returns
Array
(
    [0] => $foo
    [1] => foo
)
Array
(
    [0] => $bar
    [1] => bar
)
Array
(
    [0] => $baz
    [1] => baz
)

Hope it helps!!

Thank you!!


answered Sep 29, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

How to get route action name in laravel?

Hello @kartik, To get action name, you need ...READ MORE

answered Sep 28, 2020 in Laravel by Niroj
• 82,880 points
4,319 views
0 votes
1 answer

How to generating a random password in php?

Hello @kartik, Try this (use strlen instead of count, because count on a ...READ MORE

answered Sep 29, 2020 in Laravel by Niroj
• 82,880 points
607 views
0 votes
1 answer

How to detect a mobile device in PHP?

Hello @kartik, Here is the code: <?php $useragent=$_SERVER['HTTP_USER_AGENT']; if(preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( ...READ MORE

answered Sep 29, 2020 in Laravel by Niroj
• 82,880 points
2,278 views
0 votes
1 answer

How to Check for a Specific Type of Object in PHP?

Hello @kartik, Use: bool is_a ( object $object ...READ MORE

answered Oct 29, 2020 in Laravel by Niroj
• 82,880 points
741 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
3,717 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,880 points
2,927 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,880 points
836 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
773 views
0 votes
1 answer

How to get a list of registered route paths in Laravel?

Hello, Route::getRoutes() returns a RouteCollection. On each element, you can ...READ MORE

answered Mar 31, 2020 in Laravel by Niroj
• 82,880 points
3,288 views
+1 vote
1 answer

How to load blade or php content into a view via ajax/jquery in laravel?

Hello @kartik, Assuming you're using jQuery... create a route ...READ MORE

answered Apr 14, 2020 in Laravel by Niroj
• 82,880 points
27,073 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP