Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6531077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:50:03+00:00 2026-05-25T09:50:03+00:00

I just threw this together to help in debugging some PHP scripts. As you

  • 0

I just threw this together to help in debugging some PHP scripts. As you can see, it is sloppy but I am going to improve it some more.

My debug function has 2 variables passed in, a variable name and a variable value.

Is it possible to just pass in the variable and somehow get the name of the variable without manually doing it like I have it set now?

The Function

<?php
function debug($varname, $var)
{
    echo '<br>' . $varname;

    // $var is a STRING
    if (is_string($var)) {
        echo ' (string) = ' . $var . '<br>';

    // $var is an ARRAY
    } elseif (is_array($var)) {
        echo ' (array) = <pre>';
        print_r($var);
        echo '</pre><br>';

    // $var is an INT
    } elseif (is_int($var)) {
        echo ' (int) = ' . $var . '<br>';

    // $var is an OBJECT
    } elseif (is_object($var)) {
        echo ' (object) = <pre>';
        var_dump($var);
        echo '</pre><br>';
    }
}

The Test

$testString = 'just a test!';
$testArray = array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
    );
$testInt = 1234567890;

$testObject = new stdClass;
$testObject->someVar1 = 'testing123';
$testObject->someVar2 = '321gnitset';

debug('$testString', $testString);
debug('$testArray', $testArray);
debug('$testInt', $testInt);
debug('$testObject', $testObject);
?>

The Result…

$testString (string) = just a test!

$testArray (array) = 
Array
(
    [key1] => value1
    [key2] => value2
    [key3] => value3
)



$testInt (int) = 1234567890

$testObject (object) = 
object(stdClass)#1 (2) {
  ["someVar1"]=>
  string(10) "testing123"
  ["someVar2"]=>
  string(10) "321gnitset"
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:50:03+00:00Added an answer on May 25, 2026 at 9:50 am

    If you want to know the name, why not just pass a string constant of the name of the variable, and use global array to access it (for procedural programs)

    function foo($var) {
        echo $var; //name of the variable
        echo $GLOBALS[$$var]; //value of the variale
    }
    
    $bar = 'a string';
    foo('bar');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Try this sample code I threw together to illustrate a point: <?php $url =
if ConvertAll throw an exception on one element, can i just skip this element
I've just started debugging my first three line long android app and I can't
I was originally going to make this a longer question, but I feel like
This just started happening three weeks or so ago. The content of my website
I've got a c# windows form app I threw together. It's fairly simple:\ inputs:
While just playing with jQuery/JavaScript I ran across this problem. I could capture Alt
I know there's an easy way of doing this, but my recursion abilities are
I was wondering if someone had already use this three technologies together. I know
I'm trying to do this: !!! just learned I cant submit images because I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.