Demonstration of PHP datatypes


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP datatypes and operations
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 1/31/07
-->
<html>
<head>
<title>
Examples of PHP datatypes and operations
</title>
</head>
<body>
<h2>This page demonstrates PHP datatypes</h2>
<p>
<?php
// integers 
$n = 2;
$m = 3;
$sum = $n + $m;

// printing
print "$n + $m = $sum<br/>\n";
echo "$n + $m", " = ", $sum, "<br/>\n"; // multiple arguments

$prod = $n * $m;
$div = $n / $m;
$remainder = $n % $m;
print "$n * $m = $prod<br/>\n $n / $m = $div<br/>\n $n % $m = $remainder<br/>\n";

// float type 
$x = 3.14;
$y = 2e-2;

print "\$x = $x, \$y = $y<br/>\n";
$sum = $x + $y; // $sum was used for an integer, now is used for a float
$div = $x / $y;
print "$x + $y = $sum<br/>\n $x / $y = $div<br/>\n";

// adding an integer and a float gives you a float
$sum = $x + $m;
print "$x + $m = $sum<br/>\n";

// booleans
print "$n < $m = ".($n < $m)."<br/>\n";
print "$m < $n = ".($m < $n)."<br/>\n";

// strings 
$greeting = "Hello";
$monkeys = "5 little monkeys";

// string concatenation 
print $greeting.", ".$monkeys."<br/>\n";

// comparison
print "5 == $monkeys".(5 == $monkeys)."<br/>\n";
print "5 === $monkeys".(5 === $monkeys)."<br/>\n";

// typecasting
print "\"$monkeys\" converted to different types:<br/>\n";
print "int: ".(int)$monkeys."<br/>\n";
print "boolean: ".(boolean)$monkeys."<br/>\n"; // or (bool)
print "float: ".(float)$monkeys."<br/><br/>\n";

// automatic type conversion:
$sum = $m + $monkeys; // since $monkeys starts with 5, it's added as 5
print "$sum<br/>\n";
$sum = $m + $greeting1; // $greeting1 doesn't start with a number, added as 0
print "$sum<br/>\n";

// the result of gettype is system-dependent
print "gettype($n) = ".gettype($n)."<br/>\n";
print "gettype($n < $m) = ".gettype($n < $m)."<br/>\n";
print "gettype($monkeys) = ".gettype($monkeys)."<br/>\n";

print_r($monkeys);
print "<br/>\n";
var_dump($monkeys, $n < $m);
print "<br/>\n";

// predefined boolean typechecking functions
print "(is_string(5)) = ".(is_string(5))."<br/>\n";
print "(is_string(\"5\")) = ".(is_string("5"))."<br/>\n";


// unsetting variables and checking whether they are set
if (isset($n)) print "\$n is set<br/>\n";

if (isset($banana)) print "\$banana is set<br/>\n";

unset($n);

if (isset($n)) print "\$n is set<br/>\n";

// empty() tests if a variable equals to false
$zero = 0;
$false = false;
$emptystr = "";
print "empty($zero) = ".empty($zero)."<br/>\n";
print "empty($false) = ".empty($false)."<br/>\n";
print "empty($emptystr) = ".empty($emptystr)."<br/>\n";

// arrays are one-dimensional associative 
$numbers = array(1, 2, 3);
print_r($numbers);
print "<br/>\n";

for ($i = 0; $i < count($numbers); ++$i) {
	print $numbers[$i]."<br/>\n";
}

$numbers[5] = 5;
unset ($numbers[1]);
print_r($numbers);
print "<br/>\n";

foreach($numbers as $number) {
	print $number."<br/>\n";
}

$stuff = array("One", true, 3);
print_r($stuff);
print "<br/>\n";

$schedule = array("Monday" => "sleep", "Tuesday" => "go to class", "Sunday" => "have fun");
print_r($schedule);
print "<br/>\n";

print $schedule["Sunday"];
print "<br/>\n";

foreach($schedule as $key => $value) {
	print "On $key I $value <br/>\n";
}

// 2-D arrays
$twod = array(array(1, 2, 3), array("apple" => "red", "banana" => "yellow"));
print_r($twod);
print "<br/>\n";

print $twod[1]["banana"];
print "<br/>\n";

function add2($var) {
	return $var + 2;
}

print add2(5);
print "<br/>\n";

// pass by reference
function changevar(&$var) {
	$var += 2;
}

$n = 5;
changevar($n);
print $n."<br/>\n";

// the syntax for if-else statement
if ($n < $m) {
	print "Case 1<br/>\n";
} elseif ($n == $m) {
	print "Case 2<br/>\n";
} else {
	print "Case 3<br/>\n";
}

?>
</p>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/4657_spring07/php/datatypes.php

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.