<!--DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1138.51">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; color: #336699; background-color: #ffffff}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; color: #669933; background-color: #ffffff}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; color: #669933; background-color: #ffffff; min-height: 17.0px}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Courier; color: #cc3333; background-color: #ffffff}
span.s1 {color: #336699}
span.s2 {color: #669933}
span.s3 {color: #cc3333}
</style>
</head>
<body>
<p class="p1"><?php</p>
<p class="p2">class <span class="s1">Point </span>{ }</p>
<p class="p1"><span class="s2">class </span>Dimension <span class="s2">{ }</span></p>
<p class="p1"><span class="s2">class </span>Rectangle <span class="s2">{ }</span></p>
<p class="p3"><br></p>
<p class="p1">$items <span class="s2">= array(</span>true<span class="s2">, </span>false<span class="s2">, </span>null<span class="s2">, </span>23<span class="s2">, </span>0<span class="s2">, -</span>26<span class="s2">, </span>4.21<span class="s2">, </span>0.0<span class="s2">, -</span>3.76<span class="s2">,</span></p>
<p class="p2"> <span class="s3">'hello'</span>, <span class="s3">''</span>, array(<span class="s1">1</span>, <span class="s1">2</span>, <span class="s1">3</span>), array(<span class="s3">''</span>, <span class="s3">''</span>, <span class="s3">''</span>), array(),</p>
<p class="p2"> new <span class="s1">stdClass</span>(), new <span class="s1">Point</span>(), new <span class="s1">Dimension</span>(), new <span class="s1">Rectangle</span>());</p>
<p class="p3"><br></p>
<p class="p4"><span class="s2">echo </span>'<table cellpadding="4" border="1"></p>
<p class="p4"> <tr></p>
<p class="p4"> <th>syntax</th></p>
<p class="p4"> <th>value</th></p>
<p class="p4"> <th>type</th></p>
<p class="p4"> <th>empty</th></p>
<p class="p4"> <th>boolean</th></p>
<p class="p4"> </tr>' <span class="s2">. </span>"\n"<span class="s2">;</span></p>
<p class="p3"><br></p>
<p class="p2">foreach(<span class="s1">$items </span>AS <span class="s1">$item</span>)</p>
<p class="p2">{</p>
<p class="p1"><span class="s2"> </span>$booleanValue <span class="s2">= (boolean)</span>$item<span class="s2">;</span></p>
<p class="p2"> <span class="s1">$empty </span>= (empty(<span class="s1">$item</span>) ? <span class="s3">'EMPTY' </span>: <span class="s3">' '</span>);</p>
<p class="p1"><span class="s2"> </span>$type <span class="s2">= </span>gettype<span class="s2">(</span>$item<span class="s2">);</span></p>
<p class="p4"><span class="s2"> </span><span class="s1">$syntax </span><span class="s2">= </span>'if((boolean)'<span class="s2">;</span></p>
<p class="p3"><br></p>
<p class="p1"><span class="s2"> </span>$val<span class="s2">;</span></p>
<p class="p3"><br></p>
<p class="p1"><span class="s2"> if(</span>$type <span class="s2">== </span>boolean<span class="s2">)</span></p>
<p class="p2"> {</p>
<p class="p1"><span class="s2"> </span>$val <span class="s2">= (</span>$booleanValue <span class="s2">? </span><span class="s3">'true' </span><span class="s2">: </span><span class="s3">'false'</span><span class="s2">);</span></p>
<p class="p1"><span class="s2"> </span>$syntax <span class="s2">.= (</span>$val <span class="s2">. </span><span class="s3">')'</span><span class="s2">);</span></p>
<p class="p2"> }</p>
<p class="p2"> else if(<span class="s1">$type </span>== <span class="s3">'NULL'</span>)</p>
<p class="p2"> {</p>
<p class="p2"> <span class="s1">$val </span>= <span class="s3">'null'</span>;</p>
<p class="p2"> <span class="s1">$syntax </span>.= <span class="s3">'null)'</span>;</p>
<p class="p2"> }</p>
<p class="p1"><span class="s2"> else if(</span>$type <span class="s2">== </span>double <span class="s2">&& !</span>$booleanValue<span class="s2">)</span></p>
<p class="p2"> {</p>
<p class="p2"> <span class="s1">$val </span>= <span class="s3">'0.0'</span>;</p>
<p class="p2"> <span class="s1">$syntax </span>.= <span class="s3">'0.0)'</span>;</p>
<p class="p2"> }</p>
<p class="p2"> else if(<span class="s1">$type </span>== <span class="s1">string</span>)</p>
<p class="p2"> {</p>
<p class="p2"> <span class="s1">$val </span>= <span class="s3">'\'' </span>. <span class="s1">$item </span>. <span class="s3">'\''</span>;</p>
<p class="p1"><span class="s2"> </span>$syntax <span class="s2">.= (</span>$val <span class="s2">. </span><span class="s3">')'</span><span class="s2">);</span></p>
<p class="p2"> }</p>
<p class="p2"> else if(<span class="s1">$type </span>== <span class="s3">'array'</span>)</p>
<p class="p2"> {</p>
<p class="p1"><span class="s2"> </span>$val <span class="s2">= </span>$item<span class="s2">;</span></p>
<p class="p4"><span class="s2"> </span><span class="s1">$syntax </span><span class="s2">.= </span>'$array)'<span class="s2">;</span></p>
<p class="p2"> }</p>
<p class="p2"> else if(<span class="s1">$type </span>== <span class="s3">'object'</span>)</p>
<p class="p2"> {</p>
<p class="p1"><span class="s2"> </span>$val <span class="s2">= </span>get_class<span class="s2">(</span>$item<span class="s2">);</span></p>
<p class="p1"><span class="s2"> </span>$syntax <span class="s2">.= (</span><span class="s3">'$' </span><span class="s2">. </span>strtolower<span class="s2">(</span>$val<span class="s2">) . </span><span class="s3">')'</span><span class="s2">);</span></p>
<p class="p2"> }</p>
<p class="p2"> else</p>
<p class="p2"> {</p>
<p class="p1"><span class="s2"> </span>$val <span class="s2">= </span>$item<span class="s2">;</span></p>
<p class="p1"><span class="s2"> </span>$syntax <span class="s2">.= (</span>$val <span class="s2">. </span><span class="s3">')'</span><span class="s2">);</span></p>
<p class="p2"> }</p>
<p class="p3"><br></p>
<p class="p4"><span class="s2"> echo </span>' <tr style="color: ' <span class="s2">. (</span><span class="s1">$booleanValue </span><span class="s2">? </span>'#006600' <span class="s2">: </span>'#880000'<span class="s2">) . </span>';"></p>
<p class="p4"> <td><code>' <span class="s2">. </span><span class="s1">$syntax </span><span class="s2">. </span>'</code></td></p>
<p class="p4"> <td>' <span class="s2">. </span><span class="s1">$val </span><span class="s2">. </span>'</td></p>
<p class="p4"> <td>' <span class="s2">. </span><span class="s1">$type </span><span class="s2">. </span>'</td></p>
<p class="p4"> <td>' <span class="s2">. </span><span class="s1">$empty </span><span class="s2">. </span>'</td></p>
<p class="p4"> <td>' <span class="s2">. (</span><span class="s1">$booleanValue </span><span class="s2">? </span>'TRUE' <span class="s2">: </span>'FALSE'<span class="s2">) . </span>'</td></p>
<p class="p4"> </tr>' <span class="s2">. </span>"\n"<span class="s2">;</span></p>
<p class="p2">}</p>
<p class="p3"><br></p>
<p class="p4"><span class="s2">echo </span>'</table>' <span class="s2">. </span>"\n"<span class="s2">;</span></p>
<p class="p3"><br></p>
<p class="p1">?></p>
</body>
</html>
-->
<?php
class Point { }
class Dimension { }
class Rectangle { }
$items = array(true, false, null, 23, 0, -26, 4.21, 0.0, -3.76,
'hello', '', array(1, 2, 3), array('', '', ''), array(),
new stdClass(), new Point(), new Dimension(), new Rectangle());
echo '<table cellpadding="4" border="1">
<tr>
<th>syntax</th>
<th>value</th>
<th>type</th>
<th>empty</th>
<th>boolean</th>
</tr>' . "\n";
foreach($items AS $item)
{
$booleanValue = (boolean)$item;
$empty = (empty($item) ? 'EMPTY' : ' ');
$type = gettype($item);
$syntax = 'if((boolean)';
$val;
if($type == boolean)
{
$val = ($booleanValue ? 'true' : 'false');
$syntax .= ($val . ')');
}
else if($type == 'NULL')
{
$val = 'null';
$syntax .= 'null)';
}
else if($type == double && !$booleanValue)
{
$val = '0.0';
$syntax .= '0.0)';
}
else if($type == string)
{
$val = '\'' . $item . '\'';
$syntax .= ($val . ')');
}
else if($type == 'array')
{
$val = $item;
$syntax .= '$array)';
}
else if($type == 'object')
{
$val = get_class($item);
$syntax .= ('$' . strtolower($val) . ')');
}
else
{
$val = $item;
$syntax .= ($val . ')');
}
echo ' <tr style="color: ' . ($booleanValue ? '#006600' : '#880000') . ';">
<td><code>' . $syntax . '</code></td>
<td>' . $val . '</td>
<td>' . $type . '</td>
<td>' . $empty . '</td>
<td>' . ($booleanValue ? 'TRUE' : 'FALSE') . '</td>
</tr>' . "\n";
}
echo '</table>' . "\n";
?>
PHP 的 变量类型
最新推荐文章于 2021-03-22 19:52:10 发布