Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print variable name and its value 1

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
I've searched quite extensively for some way of printing a variable's name and value as a debugging aid, to save the following code insertions:
Code:
<?php
  echo 'VarName: ' . $VarName;
?>
I'd prefer to be able to write a function so that the call could simply be:
Code:
<?php
  debug_var($VarName);
?>
I'm sure this should be quite straightforward but I haven't been able to work it out so far, so your advice would be much appreciated!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Code:
function debug_var($var){
 global ${$var};
 echo $var ." has value ".${$var};
}
does that do what you want?

of course you pass the name of the variable to the function
Code:
$somevariable = "somevalue";
debug_Var("somevariable");
 
That's exactly what I wanted - many thanks. I had actually found some similar code but was passing the variable (including dollar sign) into the function and thought it was erroneous code. What effect do the braces have in the function body?

Is it possible to detect the type of the variable passed into the function as I would like to print the contents of booleans and arrays?

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I'm not sure if you saw my related question:
Is it possible to detect the type of the variable passed into the function as I would like to print the contents of booleans and arrays?

I've tried using var_export inside the debug_var function, but it always returns null.
Code:
  function debug_var($var){
   global ${$var};
   $var_info = var_export(${$var}, TRUE);
   echo $var_info;
   echo $var . ': ' . ${$var} . '<br />';
  }

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Oh, I forgot to say thank you for the clarification. I've read the "Variable variables" link and now understand what you mean.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
works for me
this code
Code:
<?
 function debug_var($var){
   global ${$var};
   $var_info = var_export(${$var}, TRUE);
  echo "<pre>";
   echo $var_info;
   echo "<br/>";
   
   echo $var . ': ' . ${$var} . '<br />';
   echo "</pre>";
  }
  class user {
  	var $username;
	function user() {
		$this->username = "john smith";
	}
  }
   
  $array = array("one", "two", "Three");
  $bool = true;
  $string = "this is a string";
  $float = 3500023423.432;
  $int = 69;
    
 	debug_var("array");
	debug_var("bool");
	debug_var("string");
	debug_var("float");
	debug_var("int");
	
  ?>
gives this result
Code:
array (
  0 => 'one',
  1 => 'two',
  2 => 'Three',
)
array: Array

true
bool: 1

'this is a string'
string: this is a string

3500023423.43
float: 3500023423.43

69
int: 69
 
oops
forgot to add the object

Code:
$object = new user;
debug_var($object);
gives
Code:
user::__set_state(array(
   'username' => 'john smith',
))
object: Object id #1
 
Thanks jpadie. You are quite right - it does work as you describe.

The reason it wasn't working for me was because my "debug_var" calls were from within a function and the variables I was debugging were local to that function.

Is there anyway of getting around this issue?

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
i thought that might be the case. you could pass the value to the function as well as the name.

but then you've not got much benefit over the normal route you first posted that you were trying to avoid!

get_defined_vars() captures everything that is in scope when it is called but it will have the data you are looking for.

otherwise, have you tried using an IDE? Like phpedit, for example.
 
jpadie, many thanks for all your assistance in this thread.

I think you're right - it's probably time for me to move from a glorified text editor to a proper IDE, seeing as I'm getting into PHP a bit more now!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I'm probably going to try and convince my company to purchase Zend Studio Professional. What do you use? PHPEdit or something else?

I've been using WeBuilder.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
i predominantly use dreamweaver + notepad.
i have licences for phpedit and a couple of others but i have not had the time to dig into them in detail.

i beta tested zend studio a while back but it murdered my system resources. i have not tried it since then.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top