PHP tags
<?php ?>
Text Output
echo "something";
Variable
echo "something"; //$ in front, can not begin with a number, case sensitive
Constant
define("name", "value");
Format number
echo sprintf("%01.2f", 25); //25-->25.00
echo number_format(25000, 2); //25000-->25,000.00
Single-quoted Vs double-quoted strings
$age=12; $result1="age"; $result2='age'; //resut1:12 resutl2: $age
Joining string, or concatenation (dot . or .=). To get Hello World!
echo 'Hello'." ".'World';
Format a date ( August 10, 2006 –> 2006/08/10)
echo date("Y/m/d");
Array-Create. index starts with 0, use one of the following ways to create.
$pets[0]="unicorn"
$pets = array("dragon", "unicorn", "tiger");
$pets[] ="dragon"; $pets[] ="unicorn"; $pets[] ="tiger";
$capitals =array("CA" => "Sacramento", "TX" => "Austin", "OR" => "Salem" );
Array-Viewing.
print_r($pets);
var_dump($pets);
Array-Removing.
$pets[2]="";
unset ($pets[4]);
Array-Sorting.
sort("pets"); //To sort an array that has numbers as keys and reassign keys
asort("pets"); //To sort an array that has strings as keys
Array-Getting Values.
list ($firstvar, $secondvar)=$pets;
//To get value from an array that has numbers as keys and put them in variables
extract("pets");
//To copy all the values from an array into variables named from the corresponding keys. The sample from the book is wrong, since no key was assigned in $shirtInfo
current($arrayname) next($arrayname) previous($arrayname) end($arrayname) reset($arrayname);
//Manually iterate through an array
foreach($capitals as $state =>$city)
//walks through the array one value at a time.