";
$y = 3.2; // float
echo $y . "
";
$x = $x + $y; // float
echo $x . "
"; // to get variable's type, use a gettype(var) function
echo gettype($x) . "
";
$x = $x . " dollars"; // string, concatenation with a dot (.)
echo "\$x is $x and is of type " , gettype($x), "
"; // use a comma separate
echo "\$x is $x and is of type " . gettype($x) . "
"; // use a string concatenation
$mystring = "hello";
echo "
To instruct the interpreter to not execute \$mystring as a variable
but simply treat it as a string and display it on the screen,
use an escape character \\, for example, \\\$mystring
";
echo "\$mystring = " . $mystring . "
";
echo "Use an escape character \\, \"$mystring\"" . "
";
echo "\$mystring[1] = ", $mystring[1], "
";
echo "
";
$x = 1234;
echo var_dump($x), "
"; // var_dump() return both type and content of a variable
if (is_int($x))
echo $x . " is an int" . "
";
else
echo $x . " is not an int
";
if (is_float($x))
echo $x . " is a float" . "
";
else
echo $x . " is not a float
";
if (is_numeric($x))
echo $x . " is a numeric" . "
";
else
echo $x . " is not a numeric
";
if (is_string($x))
echo $x . " is a string" . "
";
else
echo $x . " is not a string
";
echo "
";
$t = "98765";
echo "\$t = \"$t\"" . "
";
if (is_int($t))
echo $t . " is an int";
else
echo $t . " is not an int";
echo "
";
echo "cast : \$t = (int)\$t
";
$t = (int)$t;
if (is_int($t))
echo $t . " is an int";
else
echo $t . " is not an int";
echo "
";
echo "cast : \$t = (float)\$t
";
$t = (float)$t;
if (is_float($t))
echo $t . " is a float";
else
echo $t . " is not a float";
echo "
";
// Other ways to display things on the screen
//
// The print function is used to create simple unformatted output.
// (similar to the echo function)
//
// PHP borrows the printf function from C.
//
// The print doesn't require parentheses
// whereas printf requires
$str = "PHP is fun";
$sub = substr($str, 7, 3); // string operations
print $str{2}; // display a character in string at the position 2 (index starts at 0)
print "
";
printf($sub);
printf("
");
printf(strtolower($str) . "
");
printf(strtoupper($str) . "
");
$day = "Tuesday";
$high = 57;
printf("The high on %7s was %3d
", $day, $high); // syntax similar to C
printf("%5.2f", $high);
// %7s -- a character string field of 7 characters
// %3d -- an integer field of 3 digits
// %5.2f -- a float or double field in xx.xx format
?>