Logo  

CS479/579 - Web Programming II

Displaying ./code/PHP/simple/types.php

<?php
//Basic PHP types:
// Boolean:
$x = true;
$y = false;

// Use === operator for explicit equality test.  !== for explicit inequality
// Both typ and value must match.
if ($y === false) { echo "{$y} is false\n"; }

// Integers:

$x = 1; // decimal
$x = 0777; // octal
$x = 0xfff;
$x = 0b1111;

$x = (int)"123.5";
echo "$x\n";

// Float: (in PHP floats and ints are distinct)

$x = 1.5;
$x = 1.23E6;
printf("%7.2f\n", $x);

// Strings
echo "$x double quoted\n";
echo '$x single quoted'. "\n";
echo <<< LABEL
$x this is part of the string\n
LABEL;

// Arrays
// Numeric indices or key indices (dictionary / associative array):

$a = [1, 2, 3];
var_dump($a);

$a = array(1, 2, 3);
var_dump($a);

$a = array(20);
var_dump($a);

$a = ['value', 'key2'=>'value2'];
var_dump($a);

class Name {
  public $public = "visible outside of the class";
  private $private = "Visible only inside of the class";
  protected $protected = "Visible also to classes extending this class";

  private $name = null;

  // This is the constructor function:
  function __construct($value) {
    $this->name = $value;
  }

  function setname($value) {
    $this->name = $value;
  }
  function getname() {
    return $this->name;
  }
}

$name = new Name("Steve");

echo $name->getname() . "\n";
$name->setname("Bob");
echo $name->getname() . "\n";
echo $name->public . "\n";
?>