"PHP 함수 trace()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(다른 사용자 한 명의 중간 판 5개는 보이지 않습니다)
1번째 줄: 1번째 줄:
;PHP 함수 trace
;PHP 함수 trace


==소스 코드==
==v2==
<source lang='php'>
<syntaxhighlight lang='php'>
function trace($obj) {
<?php
function trace() {
ob_start();
ob_start();
if( is_null($obj) || is_bool($obj) ) {
foreach( func_get_args() as $o ) {
var_dump($obj);
if( empty($o) || is_bool($o) ) {
echo PHP_EOL;
var_dump($o); echo PHP_EOL;
continue;
}
if( is_string($o) || is_numeric($o) ) {
echo $o.PHP_EOL;
continue;
}
print_r($o);
}
}
elseif( is_string($obj) || is_numeric($obj) ) {
$res = ob_get_clean();
echo $obj.PHP_EOL;
if( php_sapi_name() == 'cli' ) echo $res;
}
else echo "<xmp>$res</xmp>";
else {
}
print_r($obj);
</syntaxhighlight>
}
<syntaxhighlight lang='php'>
$result = ob_get_clean();
trace(null);
if( php_sapi_name() == 'cli' ) {
trace(0);
echo $result;
trace('');
return;
# NULL
}
# int(0)
echo "<xmp>$result</xmp>";
# 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
# (
# )
</syntaxhighlight>
 
==v1==
<syntaxhighlight lang='php'>
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);
}
}
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:35 기준 최신판

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 }}