Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get user declared variables in PHP?

Tags:

php

get_defined_vars is about to (citation):

return a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables

well, for my debugging task, I need only those user-defined. Is there php-built-in or supplement function?

EDIT: Ok I didn't made clear what exactly I was after, here is little example:

<?php
/*
this script is included, and I don't have info
about how many scripts are 'above' and 'bellow' this*/


//I'm at line 133
$user_defined_vars = get_user_defined_vars();
//$user_defined_vars should now be array of names of user-defined variables
//what is the definition of get_user_defined_vars()?

?>
like image 824
Miloš Đakonović Avatar asked Nov 29 '12 15:11

Miloš Đakonović


1 Answers

Yes you can:

<?php
// Start
$a = count(get_defined_vars());

/* Your script goes here */
$b = 1;

// End
$c = get_defined_vars();
var_dump(array_slice($c, $a + 1));

Will return:

array(1) {
  ["b"]=>
  int(1)
}
like image 130
eisberg Avatar answered Sep 28 '22 04:09

eisberg