PHP 함수 trace()

PHP 함수 trace

1 v2[ | ]

<?php
function trace() {
	ob_start();
	foreach( func_get_args() as $o ) {
		if( empty($o) || is_bool($o) ) {
			var_dump($o); echo PHP_EOL;
			continue;
		}
		if( is_string($o) || is_numeric($o) ) {
			echo $o.PHP_EOL;
			continue;
		}
		print_r($o);
	}
	$res = ob_get_clean();	
	if( php_sapi_name() == 'cli' ) echo $res;
	else echo "<xmp>$res</xmp>";
}
trace(null);
trace(0);
trace('');
# NULL
# int(0)
# string(0) ""

trace(true);
trace(false);
# bool(true)
# bool(false)

trace([]);
# array(0) {
# }
trace([1,2]);
# Array
# (
#     [0] => 1
#     [1] => 2
# )
trace(['name'=>'Fido','age'=>2]);
# Array
# (
#     [name] => Fido
#     [age] => 2
# )

trace(new stdClass());
# stdClass Object
# (
# )

2 v1[ | ]

function trace($obj) {
        if( php_sapi_name() != 'cli' ) {
                echo '<xmp>';
                print_r($obj);
                echo '</xmp>';
                return;
        }
        if( is_bool($obj) ) {
                var_dump($obj);
                echo PHP_EOL;
                return;
        }
        if( is_string($obj) || is_numeric($obj) ) {
                echo $obj.PHP_EOL;
                return;
        }
        print_r($obj);
}

3 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}