PHP/Data Type/Casting

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

Casting a Variable

<html>
<head>
<title>Casting a variable</title>
</head>
<body>
<?php
$undecided = 3.14;
$holder = ( double ) $undecided;
print gettype( $holder ) ; 
print " -- $holder<br>";   
$holder = ( string ) $undecided;
print gettype( $holder );  
print " -- $holder<br>";   
$holder = ( integer ) $undecided;
print gettype( $holder );  
print " -- $holder<br>";   
$holder = ( double ) $undecided;
print gettype( $holder );  
print " -- $holder<br>";   
$holder = ( boolean ) $undecided;
print gettype( $holder );  
print " -- $holder<br>";   
?>
</body>
</html>



Casting the return value

 
class TextInput {
    public function __toString() {
        return (string) $this->label;
    }
}



Change the data type

 
<?php
$a = 123;
echo "is_int($a) = " . (is_int($a) ? "true" : "false") . "\n";
$a = "123";
echo "is_int((int)$a) = " . (is_int((int)$a) ? "true" : "false") . "\n";
?>



Changing Type by Casting

 
<html>
<body>
<div>
<?php
    $undecided = 3.14;
    $holder = ( double ) $undecided;
    print gettype( $holder ) ;
    print " -- $holder<br />";
    $holder = ( string ) $undecided;
    print gettype( $holder ); 
    print " -- $holder<br />";
    $holder = ( integer ) $undecided;
    print gettype( $holder ); 
    print " -- $holder<br />";
    $holder = ( double ) $undecided;
    print gettype( $holder ); 
    print " -- $holder<br />";
    $holder = ( boolean ) $undecided;
    print gettype( $holder ); 
    print " -- $holder<br />"; 
?>
</div>
</body>
</html>



Converting to in parentheses before our variable name

 
<?
    $bool = true;
    print "Bool is set to $bool\n";
    $bool = false;
    print "Bool is set to ";
    print (int)$bool;
?>



Convert String to Date

<HTML>
<BODY>
<FORM METHOD="POST" ACTION="CallMe.php">
    <TABLE>
    
    <TR>
    Date:<BR>
    <TD><INPUT TYPE="TEXT" NAME="DATE" VALUE="<?php echo $DATE ?>"></TD>
    </TR>
    
    <TR>
    <TD ALIGN="CENTER">+</TD>
    </TR>
    
    <TR>
    <TD><INPUT TYPE="TEXT" NAME="OFFSET" VALUE="<?php echo $OFFSET ?>"></TD>
    </TR>
    
    <TR>
    <TD ALIGN="CENTER"><INPUT TYPE="SUBMIT" VALUE="="></TD>
    </TR>
    
    <TR>
    <TD><INPUT TYPE="TEXT" NAME="RESULT" 
      VALUE="<?php echo date("M j, Y",(strtotime($DATE)+60*60*24*($OFFSET))) ?>" disabled></TD>
    </TR>
    
    </TABLE>
</FORM>
</BODY>
</HTML>



convert to float

 
<?php
     $val = 45678945939390393678976;
     echo $val + 5;
?>



Force to change the data type by adding (int), (integer), (float), (double), or (real) in front of the variable

 
or value or by using the function intval() or floatval()
<?php 
$a = 123; 
echo "is_int($a) = " . (is_int($a) ? "true" : "false") . "\n"; 
$a = "123"; 
echo "is_int((int)$a) = " . (is_int((int)$a) ? "true" : "false") . "\n"; 
?>



Maintaining the integrity of the data and outputting the end result.

 
<?php
  $myint = 10;
  echo $myint . "<br />";
  echo (int) $myint . "<br />";
  $myint = 10 / 3;
  echo (int) $myint . "<br />"; 
  
  $thenumber = 9.99 * 1.07;
  echo "$" . $thenumber . "<br />"; 
  echo "$" . sprintf ("%.2f", $thenumber);
?>



Typecasting

 
<?php
$a = "10";
$b = (int)$a;
echo "gettype($a) = " . gettype($a) . "\n";
echo "gettype($b) = " . gettype($b) . ", \$b = $b\n";
$a = array(5,4,5);
$b = (int)$a;
echo "gettype($a) = " . gettype($a) . "\n";
echo "gettype($b) = " . gettype($b) . ", \$b = $b\n";
?>



Type conversion: Approach 1

<?
$dog_count = intval (strval (doubleval("101 Dalmatians")));
echo ($dog_count);
?>



Type conversion: Approach 2

<?
$dog_count = (int) (string) (double) "101 Dalmatians";
echo ($dog_count);
?>



Type conversion: approach 3

<?
$dog_count = "101 Dalmatians"
settype($dog_count, "double");
echo ($dog_count);
settype($dog_count, "string");
echo ($dog_count);
settype($dog_count, "int");
echo ($dog_count);
?>



Type conversions

<?
$type_examples[0] = 123;   
$type_examples[1] = 3.14159;
$type_examples[2] = "a string";
$type_examples[3] = "9.990 (begins with number)";
$type_examples[4] = array(9,8,7);
   
print("<TABLE><TR>");
print("<TH>Original</<TH>");
print("<TH>(int)</<TH>");
print("<TH>(double)</<TH>");
print("<TH>(string)</<TH>");
print("<TH>(array)</<TH></TR>");
   
for ($index = 0; $index < 5; $index++){
    print("<TR><TD>$type_examples[$index]</TD>");
    $converted_var =
       (int) $type_examples[$index];
    print("<TD>$converted_var</TD>");
    $converted_var =
       (double) $type_examples[$index];
    print("<TD>$converted_var</TD>");
    $converted_var =
       (string) $type_examples[$index];
    print("<TD>$converted_var</TD>");
    $converted_var =
       (array) $type_examples[$index];
    print("<TD>$converted_var</TD></TR>");
  }
print("</TABLE>");
?>



When the first expression is a string, the type of the variable will depend on the second expression.

 
<?
$foo = 1 + "10.5"; // $foo is double (11.5)
$foo = 1 + "-1.3e3"; // $foo is double (-1299)
$foo = 1 + "bob-1.3e3"; // $foo is integer (1)
$foo = 1 + "bob3"; // $foo is integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is integer (11)
$foo = 1 + "10 Little Piggies"; // $foo is integer (11)
$foo = "10.0 pigs " + 1; // $foo is integer (11)
$foo = "10.0 pigs " + 1.0; // $foo is double (11)
?>