COMPUTER LEARNING CENTER
WWW.HUUKHANG.COM
Löu yù raèng, PHP töï ñoäng nhaän bieát giaù trò chuoãi ñaèng sau soá seõ khoâng ñöôïc chuyeån
sang kieåu döõ lieäu soá nhö tröôøng hôïp treân.
Ngoaøi ra, baïn coù theå söû duïng haøm settype ñeå chuyeån ñoåi döõ lieäu naøy sag döõ lieäu khaùc,
ví duï chuùng ta khai baùo nhö ví duï 2-3 (settype.php).
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Change DataType of Variable</h4>
<?php
$var="12-ABC";
$check=true;
echo
$var;
echo
"<br>";
echo
$check;
echo
"<br>";
settype($var,"integer");
echo
$var;
echo
"<br>";
settype($check,"string");
echo
$check;
?>
</BODY>
</HTML>
3.2.
Kieåm tra kieåu döõ lieäu cuûa bieán
Ñeå kieåm tra kieåu döõ lieäu cuûa bieán, baïn söû duïng caùc haøm nhö sau:
is_int ñeå kieåm tra bieán coù kieåu integer, neáu bieán coù kieåu integer thì haøm seõ traû veà giaù
trò laø true (1). Töông töï, baïn coù theå söû duïng caùc haøm kieåm tra töông öùng vôùi kieåu döõ
lieäu laø is_array, is_bool, is_callable, is_double, is_float, is_int, is_integer, is_long,
is_null, is_numeric, is_object, is_real, is_string. Chaúng haïn, baïn khai baùo caùc haøm naøy
nhö ví duï 2-4 (check.php).
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Check DataType of Variable</h4>
<?php
$sotrang=10;
$record=5;
$check = true;
$strSQL="select * from tblCustomers";
$myarr = array("first", "last", "company");
$myarrs[2];
$myarrs[0]="Number
0";
$myarrs[1]="Number
1";
$myarrs[2]="Number
2";
echo
is_array($myarr);
echo
"<br>";
echo
is_bool($record);
?>
</BODY>
Giaùo vieân: Phaïm Höõu Khang