<html> <head> <title>Hello</title> </head> <body> <?php print 'Hello, World!'; ?> </body> </html>
<html> <head> <title>Hello</title> </head> <body> <?php $name = "Joe"; print "Hello, $name"; print 'Hello, $name'; ?> </body> </html>
<?php $mystring = "12"; $myinteger = 20; print $mystring + $myinteger; ?>
<?php $mystring = "wombat"; $myinteger = (integer)$mystring ?>
<?php $mystr = "Jello, world?"; $mystr{0} = "H"; $mystr{12} = "!"; print $mystr; ?>
<?php $foo = 1; if (isset($foo)) { echo "Foo is set\n"; } else { echo "Foo is not set\n"; } if (isset($bar)) { echo "Bar is set\n"; } else { echo "Bar is not set\n"; } ?>
Name | Value |
HTTP_REFERER | If the user clicked a link to get the current page, this will contain the URL of the previous page they were at, or it will be empty if the user entered the URL directly. |
HTTP_USER_AGENT | The name reported by the visitor's browser |
PATH_INFO | Any data passed in the URL after the script name |
PHP_SELF | The name of the current script |
REQUEST_METHOD | Either GET or POST |
QUERY_STRING | Includes everything after the question mark in a GET request |
<?php if (isset($_SERVER['HTTP_REFERER'])) { print "The page you were on previously was {$_SERVER['HTTP_REFERER']}<br />"; } else { print "You didn't click any links to get here<br />"; } ?> <a href="refer.php">Click me!</a>
print `ls`;
<?php function multiply($num1, $num2) { $total = $num1 * $num2; return $total; } $mynum = multiply(5, 10); ?>
<?php function square1($number) { return $number * $number; } $val = square1($val); function square2(&$number) { $number = $number * $number; } square2($val); ?>
<?php function &square1($number) { return $number * $number; } $val =& square1($val); ?>
<?php function doFoo($Name = "Paul") { return "Foo $Name!\n"; } doFoo(); doFoo("Paul"); doFoo("Andrew"); ?>
<?php function some_func($a, $b) { for ($i = 0; $i < func_num_args(); ++$i) { $param = func_get_arg($i); echo "Received parameter $param.\n"; } } function some_other_func($a, $b) { $param = func_get_args(); $param = join(", ", $param); echo "Received parameters: $param.\n"; } some_func(1,2,3,4,5,6,7,8); some_other_func(1,2,3,4,5,6,7,8); ?>
<?php function foo() { $bar = "wombat"; } $bar = "baz"; foo(); print $bar; ?>
<?php $func = "sqrt"; if (is_callable($func)) { print $func(49); } ?>
<?php $myarray = array("Apples", "Oranges", "Pears"); $size = count($myarray); print_r($myarray); print $myarray[0]; ?>
<?php $myarray = array("a"=>"Apples", "b"=>"Oranges", "c"=>"Pears"); var_dump($myarray); ?>
array(3) { ["a"]=> string(6) "Apples" ["b"]=> string(7) "Oranges" ["c"]=> string(5) "Pears" }
$array["a"]
<?php for ($i = 0; $i < count($array); ++$i) { print $array[$i]; } ?> <?php while (list($var, $val) = each($array)) { print "$var is $val\n"; } foreach($array as $val) { print $val; } foreach ($array as $key => $val) { print "$key = $val\n"; } ?>
<?php $toppings1 = array("Pepperoni", "Cheese", "Anchovies", "Tomatoes"); $toppings2 = array("Ham", "Cheese", "Peppers"); $inttoppings = array_intersect($toppings1, $toppings2); $difftoppings = array_diff($toppings1, $toppings2); $bothtoppings = array_merge($toppings1, $toppings2); var_dump($inttoppings); var_dump($difftoppings); var_dump($bothtoppings); ?>
<?php function endswithy($value) { return (substr($value, -1) == 'y'); } $people = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe"); $withy = array_filter($people, "endswithy"); var_dump($withy); ?>
<?php $needle = "Sam"; $haystack = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe"); if (in_array($needle, $haystack)) { print "$needle is in the array!\n"; } else { print "$needle is not in the array\n"; } ?>
ksort
sort by keys, asort
by values. <?php $capitalcities['England'] = 'London'; $capitalcities['Wales'] = 'Cardiff'; $capitalcities['Scotland'] = 'Edinburgh'; ksort($capitalcities); var_dump($capitalcities); asort($capitalcities); var_dump($capitalcities); ?>
<?php $array["a"] = "Foo"; $array["b"] = "Bar"; $array["c"] = "Baz"; $array["d"] = "Wom"; $str = serialize($array); $strenc = urlencode($str); print $str . "\n"; print $strenc . "\n"; ?> <?php $arr = unserialize(urldecode($strenc)); var_dump($arr); ?>
<?php class dog { public function bark() { print "Woof!\n"; } } class poodle extends dog { public function bark() { print "Yip!\n"; } } $poppy = new poodle; $poppy->bark(); ?>
class dog { public $Name; public function bark() { print "Woof!\n"; } } $poppy->Name = "Poppy"; //not this way: $poppy->$Name = "Poppy";
this
.function bark() { print "{$this->Name} says Woof!\n"; }
class poodle extends dog { public function bark() { print "Yip!\n"; } public function __construct($DogName) { parent::__construct($DogName); print "Creating a poodle\n"; } public function __destruct() { print "{$this->Name} is no more...\n"; parent::__destruct(); } }
clone
keyword copies all member variables and then calls __clone
. <?php abstract class dog { public function __clone() { echo "In dog clone\n"; } } class poodle extends dog { public $Name; public function __clone() { echo "In poodle clone\n"; parent::__clone(); } } $poppy = new poodle(); $poppy->Name = "Poppy"; $rover = clone $poppy; ?>
<?php class employee { static public $NextID = 1; public $ID; public function __construct() { $this->ID = self::$NextID++; } } $bob = new employee; $jan = new employee; $simon = new employee; print $bob->ID . "\n"; print $jan->ID . "\n"; print $simon->ID . "\n"; print employee::$NextID . "\n"; ?>
<form action="someform.php" method="GET"> Name: <input type="text" name="Name" value="Jim" /><br /> Password: <input type="password" name="Password" maxlength="10" /><br /> Age range: <select name="Age"> <option value="Under 16">Under 16</option> <option value="16-30" SELECTED>16-30</option> <option value="31-50">31-50</option> <option value="51-80">51-80</option> </select><br /><br /> Life story:<br /> <textarea name="Story" rows="10" cols="80">Enter your life story here</textarea><br /><br /> <input type="radio" name="FaveSport" value="Tennis"> Tennis</input> <input type="radio" name="FaveSport" value="Cricket"> Cricket</input> <input type="radio" name="FaveSport" value="Baseball"> Baseball</input> <input type="radio" name="FaveSport" value="Polo"> Polo</input> <br /> <input type="checkbox" name="Languages[]" value="PHP" CHECKED> PHP</input> <input type="checkbox" name="Languages[]" value="CPP"> C++</input> <input type="checkbox" name="Languages[]" value="Delphi"> Delphi</input> <input type="checkbox" name="Languages[]" value="Java"> Java</input> <br /><input type="submit" /> </form>would be handled with:
<?php $_GET['Languages'] = implode(', ', $_GET['Languages']); $_GET['Story'] = str_replace("\n", "<br />", $_GET['Story']); print "Your name: {$_GET['Name']}<br />"; print "Your password: {$_GET['Password']}<br />"; print "Your age: {$_GET['Age']}<br /><br />"; print "Your life story:<br />{$_GET['Story']}<br /><br />"; print "Your favourite sport: {$_GET['FaveSport']}<br />"; print "Languages you chose: {$_GET['Languages']}<br />"; ?>
setcookie
to set and _COOKIE
to read. <?php if (!isset($_COOKIE['Ordering'])) { setcookie("Ordering", $_POST['ChangeOrdering'], time() + 31536000); } ?> <form method="POST" action="mbprefs.php"> Reorder messages: <select name="ChangeOrdering"> <option value="DateAdded ASC">Oldest first <option value="DateAdded DESC">Newest first <option value="Title ASC">By Title, A-Z <option value="Title DESC">By Title, Z-A </select> <input type="submit" value=" Save Settings "> </form>
session_start()
. $_SESSION['var']
session_destroy()
<?php //Connect to the database mysql_connect("localhost", "phpuser", "alm65z"); mysql_select_db("phpdb"); //Send a query and get resutls $result = mysql_query("SELECT * FROM usertable"); $numrows = mysql_num_rows($result); print "There are $numrows people in usertable\n"; //Use a variable in the query, just place it in. $result = mysql_query( "SELECT ID FROM webpages WHERE Title = '$SearchCriteria';"); //Free memory mysql_free_result($result); //Close connection mysql_close(); ?>
This talk available at http://jmvidal.cse.sc.edu/talks/php/
Copyright © 2009 José M. Vidal
.
All rights reserved.
06 February 2008, 10:45AM