Type conversions : Casting « Data Type « PHP






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

           
       








Related examples in the same category

1.Casting a variable
2.Casting the return value
3.Change the data type
4.Changing Type by Casting
5.Converting to in parentheses before our variable name
6.Maintaining the integrity of the data and outputting the end result.
7.Force to change the data type by adding (int), (integer), (float), (double), or (real) in front of the variable
8.When the first expression is a string, the type of the variable will depend on the second expression.
9.convert to float
10.Typecasting
11.Casting a Variable
12.Type conversion: Approach 2
13.Type conversion: Approach 1
14.Type conversion: approach 3
15.Convert String to Date