Basic PHP Things

[A Starter-Kit for Brother Judkins' class]

Tag Description Example
.php extension for any file containing php index.php
<?php open a php block <?php
?> close a php block ?>
echo to display/print/output something on a page echo "hello world";
; Each code line in PHP must end with a semicolon to separate one set of instructions from another. echo "hi Mom";
include to place/drop/include the contents of one file into another file include("nav.php");
$var Variable: something that varies; a container to hold a changeable value; you may name a variable almost anything you'd like, but it should start with a lowercase letter and should not contain spaces or symbol characters. The value of the variable may be text (string), a number or a true/false; text (string) must be in quotation marks.
$name = "Walter Cronkite";
$birthyear = 1916;
$journalist = true;
		
2 single line comment // note to self
/* … */ multi-line comment
/* if($name=="Walter Cronkite"){
	echo "Hello Mr. Cronkite";
} */
= a single equal sign assigns a value $age = 21;
== a double equal sign checks the value if($age == 21){…}
++ a double plus sign adds one to the current value $counter++;
. string concatenation operator; if you need to add strings together, use a "."
$fn = "Barbara";
$ln = "Wawa";
echo $fn." ".$ln;
if used to execute code only if a certain condition is met
if($age > 21){
	echo "old";
}
else used with an if statement to execute code when the condition is not met.
if($age > 21){
	echo "old";
} else {
	echo "baby";
}
$array A 'super variable' because it can hold more than one piece of data at a time. It can even hold other arrays. It's a lot like a filing cabinet. $students = array("holly","mike","cameron");
while The easiest loop to understand. Code is executed as long as a condition is met.
$i=0;
while($i<50){
	echo $i;
	$i++;
}
for The 2nd easiest loop to understand. Similar to the while loop, but counter and counter++ are moved inside the function
for($i=0;$i<7;$i++){
	echo "$i potato";
}
foreach A loop designed specifically for working with arrays. Code is executed once for each direct child of the array. Each time the code executes, array items are stored in a variable one at a time.
foreach($array as $variable){
	echo $variable;
}
rand Generates a random whole number between $x and $y
rand($x, $y);
date Generates a time/date related text string. See php.net/manual for lots of examples.
sort Sorts an array alphanumerically by its 0th drawer sort($students);