PHP Quick Reference Study Guide
Comments In PHP
#this is a comment, to EOL
//this is also a comment, to EOL
/* This is also a comment
For multi lines
*/
Variables
Must start with (A-Za-z) or _(underscore)
like:
$blah
$_blah
$H4×0rs
Illegial Variables
$7337
$*UCK
Variables in PHP are loosely typed, and do not need to be declared prior to use.
So the following are all legal statements
$shane = “Shane Is Leet”;
$shane = 10 * 4 + 3;
Variable types in php are interchangeable, so I used the variable $shane as a string in one line, and then as an integer in another, and php dosen’t mind at all.
PHP does support indirect variable references, but I personally don’t use them. Here is a brief example:
$var1 = “Shane”;
$$var1 = “Who Is The 7337″;
print $Shane; # this will output “Who Is The 7337″
You can use as many levels of indirection as you can stomach.
Variable Management
- isset()
- unset()
- empty()
isset() example:
if(isset($shane) {
print ‘$shane is set’;
}
unset() example:
unset($shane);
if(!isset($shane)) {
echo ‘$shane is unset’;
}
empty() is typically used to check form values, the variables value is converted to a bool, then checked for true/false, example:
if (empty($shane)) {
print ‘Error: $shane is empty’;
}
This prints the error message if $shane dosen’t contain a value that evaluates to true.
Data Types
PHP Suports, Integer, Hex (like 0xABCD) - Values (-100)
Floating Point like: 3.14, +0.1e-4, -1600.3, 22.6E42
Strings:
PHP supports strings, beginning and ending with single or double quotes, and allows embedding of variables like in the previous examples.
When using single quotes, you lose some of the escape characters support when using double quotes
Double Quote Escape Characters:
- \n #newline
- \t #Tab
- \” #double quote
- \\ #backslash
- \0 #ASCII 0 (null)
- \r #linefeed
- \$ #the character $
- \(Octal # ) #ex \70 would be the letter 8
- \x(hex #) # like \0×32 is the letter 2
Single Quote Escape Characters:
- \’ #single uote
- \\ #backslash
Null
Null is a data type with only one value: the NULL value. Use it thusly:
$shane = NULL;
Arrays
Straitforward, array(1, 2, 3) or array(0 => 1, 1 => 2, 2 =>3)
$var1 = array(”name” => “Shane”, “age” => “28″);
echo $var1[”name”]; #prints “Shane”
There is quite a lot to arrays in php, so for more info check Arrays In PHP
To Iterate across the array, I typically use foreach()
$players = array(”Shane”, “Craig”, “Sandra”, “Meme”);
foreach($players as $key => $value) {
print “#$key = $value\n”;
}
Output is:
#0 = Shane
#1 = Craig
#2 = Sandra
#3 = Meme
You can also traverse the array using list() and each(), which I will not go over here.
More to come later.
3 comments.
Breaking Moore’s Law Nets Computer Industry 10-20 »« Clinton vs. Bush