This page demonstrates 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: 2/14/2011
-->
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<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 as an integer, now is used as a float
$div = $x / $y;
print "$x + $y = $sum<br/>\n $x / $y = $div<br/>\n";
print "Formatted result: $n / $m = ".number_format($div, 2)."<br/>\n";

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

// booleans
print "$n &lt; $m = ".($n < $m)."<br/>\n";
print "$m &lt; $n = ".($m < $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";
 }


// 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 "results of gettype are system-dependent, don't apply string comparison!!!!<br />\n";
print "gettype($n) = ".gettype($n)."<br/>\n";
print "gettype($n &lt; $m) = ".gettype($n < $m)."<br/>\n";
print "gettype($monkeys) = ".gettype($monkeys)."<br/><br />\n";

print "print_r is used to print a value of any type<br />\n";
print_r($monkeys);
print "<br/><br />\n";
print "var_dump gives you the most exact type info and the value<br />";
var_dump($monkeys, $n < $m);
print "<br/><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";


// arrays need to be passed by reference if they need to be changed
function addOneToArrayElements(&$thearray) {
  foreach($thearray as $key => $value) {
    $thearray[$key] = $value + 1;
  }
}

$arr = array (2,4,-2);

print_r($arr);

addOneToArrayElements($arr);

print_r($arr);

?>
</p>
</body>
</html>

http://csci1101sp10.morris.umn.edu/~elenam/tmp4657sp11/php_basics.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.