PHP/Language Basics/Variable Type

Материал из Web эксперт
Перейти к: навигация, поиск

Changing the Type of a Variable with settype()

<html>
<head>
<title>Changing the type of a variable with settype()</title>
</head>
<body>
<?php
$undecided = 3.14;
print gettype( $undecided ); 
print " -- $undecided<br>";  
settype( $undecided, string );
print gettype( $undecided ); 
print " -- $undecided<br>";  
settype( $undecided, integer );
print gettype( $undecided ); 
print " -- $undecided<br>";  
settype( $undecided, double );
print gettype( $undecided ); 
print " -- $undecided<br>";  
settype( $undecided, boolean );
print gettype( $undecided ); 
print " -- $undecided<br>";  
 ?>
</body>
</html>



gettype() and settype()

<html>
<head>
     <title>Gettype() and Settype() Usage</title>
</head>
<body>
<?php
     $my_double = 100;
     $my_string = 1;
   
     print("\$my_double"s data type is... (" . gettype($my_double) .") <br />");
     print("\$my_string"s data type is... (" . gettype($my_string) .") <br />");
   
     settype($my_string, "string");
     settype($my_double, "double");
   
     print("\$my_double"s data type is... (" . gettype($my_double) .") <br />");
     print("\$my_string"s data type is... (" . gettype($my_string) .") <br />");
?>
</body>
</html>



Testing the Type of a Variable

<html>
<head>
<title>Testing the type of a variable</title>
</head>
<body>
<?php
$testing = 5;
print gettype( $testing ); // integer
print "<br>";
$testing = "five";
print gettype( $testing ); // string
print("<br>");
$testing = 5.0;
print gettype( $testing ); // double
print("<br>");
$testing = true;
print gettype( $testing ); // boolean
print "<br>";
?>
</body>
</html>