php-general Digest 8 Jan 2012 15:53:09 - Issue 7640
Topics (messages 316211 through 316219):
Variable Troubleshooting Code
316211 by: Donovan Brooke
316212 by: David Courtin
Strange foreach reference issue
316213 by: Tim Behrendsen
316214 by: Stephen
316215 by: Matijn Woudt
316216 by: Tim Behrendsen
316217 by: Stephen
316218 by: Tim Behrendsen
316219 by: Adi Mutu
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
--
---BeginMessage---
Hello!,
I work in another language mostly and often develop while displaying
variables (post,get,and defined) and their values at the bottom of the
page or in specific places. So, I thought I'd forward my PHP version as
an effort of good Karma to the list perhaps! ;-)
Below is 2 simple functions that are helpful for troubleshooting while
developing. Just place this code into a .php file and require it at the
top of any PHP page. Then, at the bottom of the page, or in a specific
(more pertinent) location, call the functions with something like this:
?PHP
//troubleshooting code
print 'br /bTesting:/bp';
print htmlentities(list_formvars());
print htmlentities(list_vars(get_defined_vars()));
print '/p';
?
-
Optionally, you can call only specific naming conventions of your
variables (if you use them).. ie:
print htmlentities(list_vars(get_defined_vars(),'t_'));
The above will display all defined vars such as:
t_name=value
t_city=value
t_address=value
etc..
Code:
---
/*
FUNCTION NAME: list_formvars
INPUT: optional begins with var
OUTPUT: Name = Value br /
Name = Value br /
USE: For troubleshooting code
Example Use:
list_formvars();
list_formvars('f_a');
*/function list_formvars($pmatch = null) {
print br /b'get' Vars:/bbr /;
foreach ($_GET as $key = $value) {
if (isset($pmatch)) {
if (substr($key,0,strlen($pmatch)) == $pmatch) {
print $key = $valuebr /;
}
} else {
print $key = $valuebr /;
}
}
print br /b'post' Vars:/bbr /;
foreach ($_POST as $key = $value) {
if (isset($pmatch)) {
if (substr($key,0,strlen($pmatch)) == $pmatch) {
print $key = $valuebr /;
}
} else {
print $key = $valuebr /;
}
}
}/*
FUNCTION NAME: list_vars
INPUT: get_defined_vars(),begins with match
OUTPUT: Name = Value br /
Name = Value br /
USE: For troubleshooting code
Example Use:
list_vars(get_defined_vars());
list_vars(get_defined_vars(),'t_');
*/function list_vars($a_vars,$pmatch = null) {
print br /b'defined' Vars:/bbr /;
foreach ($a_vars as $key = $value) {
if (isset($pmatch)) {
if (substr($key,0,strlen($pmatch)) == $pmatch) {
print $key = $valuebr /;
}
} else {
print $key = $valuebr /;
}
}
}
Cheers,
Donovan
P.S. Always open to good criticism if you peeps see something that can
be written better.. this is about my 3rd PHP project only... so, still
heavily learning ;-)
--
D Brooke
---End Message---
---BeginMessage---
Hi,
some pretty natives php functions exists to do the job :
var_export — Outputs or returns a parsable string representation of a variable
debug_zval_dump — Dumps a string representation of an internal zend value to
output
var_dump — Dumps information about a variable
print_r — Prints human-readable information about a variable
echo 'pre';
print_r( array | object );
echo '/pre';
Regards.
Le 7 janv. 2012 à 19:00, Donovan Brooke a écrit :
Hello!,
I work in another language mostly and often develop while displaying
variables (post,get,and defined) and their values at the bottom of the page
or in specific places. So, I thought I'd forward my PHP version as an effort
of good Karma to the list perhaps! ;-)
Below is 2 simple functions that are helpful for troubleshooting while
developing. Just place this code into a .php file and require it at the top
of any PHP page. Then, at the bottom of the page, or in a specific (more
pertinent) location, call the functions with something like this:
?PHP
//troubleshooting code
print 'br /bTesting:/bp';
print htmlentities(list_formvars());
print htmlentities(list_vars(get_defined_vars()));
print '/p';
?
-