PHP Quick Reference Study Guide

Posted on February 17, 2008 by admin.
Categories: PHP Programming.

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.

del.icio.us Slashdot Digg Facebook Technorati StumbleUpon Yahoo Ask

3 comments.

Clinton vs. Bush

Posted on by Shane.
Categories: Uncategorized.

I had a bit of a hard time when I first read this in an email, however the original author was good enough to point to a respectable authority on the subject which could verify their information. Today, most Americans think that their current President is one of the worst in history, while the one before that was the best ever. So, which of our past two Presidents had more of a positive effect on our Nation’s economy? The answer may surprise you. How about a side-by-side comparison:
(more…)

del.icio.us Slashdot Digg Facebook Technorati StumbleUpon Yahoo Ask

1 comment.