Basic syntax
Comments
// for single line
/* */ for multiple line
# for shell style comments
Variables
-starts with $
-may contain
strings
numbers
arrays
-is case sensitive
Variable variables
$foo = ‘bar’;
$$foo = ‘baz’;
Variable reference
$black = & $white
-unset a variable that is aliased
unset($white);
//$black still = “snow”
Global variables inside a fxn
function foo(){
global $a;
$a += 2;
}
Static variable
exit and return
-exit(“db fail”);
-die(“db fail”);
-$db = @mysql_connect(“localhost”, $USERNAME,$PASS)
or die(“db fail”);
Constants
-define(‘PUBLISHER’, “O’Reily”);
Strings
String concat
‘therer were’ . $n . ‘ ducks.’ ;
Quotes
‘some’
“some”
Here document
$s = <<< END
Foo
END;
Print strings
echo "hello";
print("hello") //returns bool
printf()
'%.2f', 27.452 //27.45
'%d %x', 214,214 //214 d6
'%03d',7 //007
'%02d/%04y' //15/2004
'%.2f%%',2.1//2.10%
'$%5.2f',4.1 //$% 4.10
sprintf: save instead print
print_r()
var_dump
String manipulation
strlen()
trim()/ltrim/rtrim
strtolower() strtoupper()
ucfirst() ucwords()
String fxn fon html
htmlspecialchars()
htmlentities()
strip_tags()
get_meta_tags()
raw_url_encode()
raw_url_decode()
url_encode()
url_decode()
For sql
add_slash()
Comparison
$1 == $2
strcmp($1,$2); //retuns -1,0,1
strcasecmp() //conv to lower case
strnatcmp() //natural order
soundex($str) //sounds alike
metaphone($str)
similar_text($str1,$str2[,pcnt]);
levenshtein($str1,$str2[,$cost_ins, $cost_rep,$cost_del]);
Manipulate string
substr(string,start[, lengh]);
substr_count(big_str, small_str);
substr_replace(ori, new, start[, len]);
strrev($str); //reverse string
str_repeat($str, count);
str_pad(topad,len[,with[,padtype]]};
Decomp string
$array=explode(sep,str[,limit]};
implode(sep,array); //same join
Tokenizing
$str="fred,flint,35,wilma";
$token=strtok($str,",");
while($token != false){
echo ("$token ");
$token = strtok(",");
}
// fred flint 35 wilma
sscanf(str,temp[, var1,var2...]);
String search
strpos(large_str, small_str);
strrpos(large_str, small_str);
strstr(large_str, small_str);
Decompose url
$array=parse_url(url);
Arrays
-define
$person[0] = ‘Edison’;
$person[‘1’] = ‘Wankel’;
$person = array(
‘Edison’,’Wankel’);
$person = array(
‘0’ => ‘Edison’,
‘1’ => ‘Winkel’);
-use
foreach ($person as $name){
echo “Hello, $name\n”;
}
Class
Define Class
class Person {
var $name = ”;
function name($new = null) {
if (! is_null($new)){
$this->name = $new;
}
return $this->name;
}
}
Use Class
$ed = new Person;
$ed->name(‘Edison’);
printf(“Hello, %s\n”,$ed->name;
Operators
-Arithmetic
+
–
*
/
%
++
—
-Assignment
=
+=
-=
*=
/=
%=
-Comparison
==
=== // of same type
!=
!== // of same type
>
<
>=
<=
-Logical
&&
||
!
Condition
-if
if (condition) {
statements;
}else{
statements;
}
-switch
switch (expression) {
case label1:
statements;
break;
case label2:
statements;
break;
default:
statements;
break;
}
Looping
-while
while (condition) {
statements;
}
-do while
do {
statements;
} while (condition);
-for
for (int i=0; i<5; i++) {
statements;
}
exit and return
-exit(“db fail”);
-die(“db fail”);
-$db = @mysql_connect(“localhost”, $USERNAME,$PASS)
or die(“db fail”);
Include code
Include 'header.html'; ?>
Require 'design.inc';
header(); ?>
content
Footer(); ?>
-allow_url_fopen in php.ini
-include_once/require_once
-qet_included_files()
Embedding in web pages
-Use echo
<?php echo "hello"; ?>
-Use PHP Script
<script lanquage="php"> echo "hello!"; <!script>
-echo directly
<?= "hello"; ?>