Function proposal

php.internals

AnteD

21 years ago
Hi guys... Yes I know that this is a small function and yes I know it can be copy-paste with every new project you do but from the first day I've been using PHP until today this function follows me everywhere and I now that everyone is using it one way or the other for debugging...... the problem is when you want to debug a piece of code separately or use it in one script then you have to find the function...copy paste to that script or write it again just for that script...and so on...it would be great if it could be in the php core.... I have a very basic knowledge of C so implementing this function would be a nightmare for me but for someone who knows C and the inner "mysteries" of the Zend Engine :) this is a piece of cake.... Ok...here it is...don't laugh :) /** * Prints the variable in HTML format... * * @param mixed $var * @param bool $return return or print the var * @return string */ function print_pre($var, $return = false) { $retval = »<pre>«.print_r($var, true).</pre>«; if($return) return $retval; print($retval); } ok....let the shooting begin :)

Benj Carson

21 years ago
Hmm, it's no GOTO, but oddly enough we have virtually exactly the same function and use it everywhere too. The only differences are we call ours pre_r() and we add the following check against php_sapi_name() so that the function behaves sanely when called from the cli: function pre_r($mixed, $return) { if ( php_sapi_name() == "cli" ) { if ( $return ) return print_r($mixed, true); print_r($mixed); } else { if ( $return ) return "<pre>" . print_r($mixed, true) . "</pre>"; echo "<pre>"; print_r($mixed); echo "</pre>"; } } While I don't know if it *needs* to be in the core, if it were I'd certainly use it. My $0.02, Benj Carson

Sean Coates

21 years ago
AnteD wrote:
> Yes I know that this is a small function and yes I know it can be > copy-paste with every new project you do but from the first day I've > been using PHP until today this function follows me everywhere and I now > that everyone is using it one way or the other for debugging......
... The proper place to ask for new features is at http://bugs.php.net/. Click "report a bug" and choose "Feature/Change Request" as the "Type of bug". S

Derick Rethans

21 years ago
On Mon, 16 May 2005, Sean Coates wrote:
> AnteD wrote: > > Yes I know that this is a small function and yes I know it can be copy-paste > > with every new project you do but from the first day I've been using PHP > > until today this function follows me everywhere and I now that everyone is > > using it one way or the other for debugging...... > ... > > The proper place to ask for new features is at http://bugs.php.net/. Click > "report a bug" and choose "Feature/Change Request" as the "Type of bug".
But don't bother - this was requested before and denied. Derick

Sara Golemon

21 years ago
> > > Yes I know that this is a small function and yes I know it can be
copy-paste
> > > with every new project you do but from the first day I've been using
PHP
> > > until today this function follows me everywhere and I now that
everyone is
> > > using it one way or the other for debugging...... > > ... > > > > The proper place to ask for new features is at http://bugs.php.net/.
Click
> > "report a bug" and choose "Feature/Change Request" as the "Type of bug". > > But don't bother - this was requested before and denied. >
If you really-really want it on your own build, just apply this short patch. Index: ext/standard/basic_functions.c =================================================================== RCS file: /repository/php-src/ext/standard/basic_functions.c,v retrieving revision 1.713 diff -u -r1.713 basic_functions.c --- ext/standard/basic_functions.c 2 May 2005 12:12:04 -0000 1.713 +++ ext/standard/basic_functions.c 16 May 2005 18:00:25 -0000 @@ -2762,23 +2762,29 @@ /* }}} */ -/* {{{ proto mixed print_r(mixed var [, bool return]) +/* {{{ proto mixed print_r(mixed var [, bool return[, bool pretags]]) Prints out or returns information about the specified variable */ PHP_FUNCTION(print_r) { zval *var; - zend_bool i = 0; + zend_bool i = 0, pretags = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &var, &i) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|bb", &var, &i, &pretags) == FAILURE) { RETURN_FALSE; } if (i) { php_start_ob_buffer (NULL, 0, 1 TSRMLS_CC); } + if (pretags) { + php_printf("<pre>\n"); + } zend_print_zval_r(var, 0 TSRMLS_CC); + if (pretags) { + php_printf("\n</pre>\n"); + } if (i) { php_ob_get_buffer (return_value TSRMLS_CC); php_end_ob_buffer (0, 0 TSRMLS_CC);

Greg Beaver

21 years ago
AnteD wrote:
> /** > * Prints the variable in HTML format... > * > * @param mixed $var > * @param bool $return return or print the var > * @return string > */ > function print_pre($var, $return = false) { > $retval = »<pre>«.print_r($var, true).</pre>«; > if($return) return $retval; > print($retval); > } > > ok....let the shooting begin :)
the xdebug extension has very nice html-ized var_dump(). Use that on your devel machine and you'll never need your print_pre again. Otherwise I find it easier to simply do this: <?php echo '<pre>'; var_dump($blah); echo '</pre>'; ?> than to write a function and make sure it is always there. Alternately, view source from your browser works great for grabbing var_dump()/print_r() output. Greg

Marcus Boerger

21 years ago
Hello AnteD, Friday, May 13, 2005, 1:29:36 PM, you wrote:
> Hi guys...
> Yes I know that this is a small function and yes I know it can be > copy-paste with every new project you do but from the first day I've > been using PHP until today this function follows me everywhere and I now > that everyone is using it one way or the other for debugging......
> the problem is when you want to debug a piece of code separately or use > it in one script then you have to find the function...copy paste to that > script or write it again just for that script...and so on...it would be > great if it could be in the php core....
> I have a very basic knowledge of C so implementing this function would > be a nightmare for me but for someone who knows C and the inner > "mysteries" of the Zend Engine :) this is a piece of cake....
> Ok...here it is...don't laugh :)
> /** > * Prints the variable in HTML format... > * > * @param mixed $var > * @param bool $return return or print the var > * @return string > */ > function print_pre($var, $return = false) { > $retval = »<pre>«.print_r($var, true).</pre>«; > if($return) return $retval; > print($retval); > }
> ok....let the shooting begin :)
Why not simply write this function to a seperate file and auto prepend it by appropriate ini setting to any script you run - no more change needed :-)
-- Best regards, Marcus mailto:mail@marcus-boerger.de

AnteD

21 years ago
Thanx guys for the advices.... Respect... Ante Marcus Boerger wrote: