PHP/Data Type/Casting

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

Casting a Variable

   <source lang="html4strict">

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

      </source>
   
  


Casting the return value

   <source lang="html4strict">

class TextInput {

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

}

 </source>
   
  


Change the data type

   <source lang="html4strict">

<?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"; ?>

 </source>
   
  


Changing Type by Casting

   <source lang="html4strict">

<html> <body>

<?php

   $undecided = 3.14;
   $holder = ( double ) $undecided;
   print gettype( $holder ) ;
   print " -- $holder
"; $holder = ( string ) $undecided; print gettype( $holder ); print " -- $holder
"; $holder = ( integer ) $undecided; print gettype( $holder ); print " -- $holder
"; $holder = ( double ) $undecided; print gettype( $holder ); print " -- $holder
"; $holder = ( boolean ) $undecided; print gettype( $holder ); print " -- $holder
";

?>

</body> </html>

 </source>
   
  


Converting to in parentheses before our variable name

   <source lang="html4strict">

<?

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

?>

 </source>
   
  


Convert String to Date

   <source lang="html4strict">

<HTML> <BODY> <FORM METHOD="POST" ACTION="CallMe.php">

Date:
<INPUT TYPE="TEXT" NAME="DATE" VALUE="<?php echo $DATE ?>">
+
<INPUT TYPE="TEXT" NAME="OFFSET" VALUE="<?php echo $OFFSET ?>">
<INPUT TYPE="SUBMIT" VALUE="=">
<INPUT TYPE="TEXT" NAME="RESULT" VALUE="<?php echo date("M j, Y",(strtotime($DATE)+60*60*24*($OFFSET))) ?>" disabled>

</FORM> </BODY> </HTML>


      </source>
   
  


convert to float

   <source lang="html4strict">

<?php

    $val = 45678945939390393678976;
    echo $val + 5;

?>

 </source>
   
  


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

   <source lang="html4strict">

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"; ?>

 </source>
   
  


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

   <source lang="html4strict">

<?php

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

?>

 </source>
   
  


Typecasting

   <source lang="html4strict">

<?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"; ?>

 </source>
   
  


Type conversion: Approach 1

   <source lang="html4strict">

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


      </source>
   
  


Type conversion: Approach 2

   <source lang="html4strict">

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

      </source>
   
  


Type conversion: approach 3

   <source lang="html4strict">

<? $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); ?>

      </source>
   
  


Type conversions

   <source lang="html4strict">

<? $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(""); print("");

for ($index = 0; $index < 5; $index++){

print("");
   $converted_var =
      (int) $type_examples[$index];
print("");
   $converted_var =
      (double) $type_examples[$index];
print("");
   $converted_var =
      (string) $type_examples[$index];
print("");
   $converted_var =
      (array) $type_examples[$index];
print("");
 }
print("
Original</"); print("(int)</"); print("(double)</"); print("(string)</"); print("(array)</
$type_examples[$index]$converted_var$converted_var$converted_var$converted_var
");

?>

      </source>
   
  


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

   <source lang="html4strict">

<? $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) ?>

 </source>