PHP Syntax and other Notes
Note: this Scribble is buggy - the security filters are having a field day with the embedded PHP, expect missing keywords, etc.
-
The following code displays each element on a new line by calling the user-defined function myPrint() with each element of $array:
function myPrint($value) { echo "$value"; }
array_walk($array, myPrint);
-
The funky comment syntax for functions is known as "phpDoc " - some IDE's use it somehow.
-
Send a proper mixed mode (text+HTML) email: http://www.webmasterbase.com/article/679/50 - and search deja.com - I made a post a while ago.
-
OR vs OR: || is comparison; | is a bitwise OR. (NB those are pipes, not "L" or "1"
$str = <<
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
-
A shortcut to spit out (echo) the value of a variable: (rats, code sample got eaten by the ScribbleWeb filters...) (Similar to ASP)
-
isset() (returns BOOL)
-
CONCATENATE to a string: "$mystring.='Add this to mystring';"
-
Error Log: error_log will put an entry in the system error log! Why didn't I know this earlier?
-
$HTTP_SESSION_VARS and $_SESSION are different variables. $_SESSION is superglobal, HSV is not.
-
Objects: (http://stackoverflow.com/questions/3037526/where-we-use-object-operator-in-php)
-> is used when you want to call a method on an instance or access an instance property.
:: is used when you want to call a static method, access a static variable, or call a parent class's version of a method within a child class.
-
Table 6.3. Bitwise Operators - From PHP manual.
Example Name Result
$a & $b And Bits that are set in both $a and $b are set.
$a | $b Or Bits that are set in either $a or $b are set.
$a ^ $b Xor Bits that are set in $a or $b but not both are set.
~ $a Not Bits that are set in $a are not set, and vice versa.
$a << $b Shift left Shift the bits of $a $b steps to the left (each step means "multiply by two")
$a >> $b Shift right
Shift the bits of $a $b steps to the right (each step means "divide by two")
Modulus: $a % $b
-
A sample on creating a while loop that evaluates after the loop:
do {
$str2 = $str1;
$str1 = eregi_replace("(^| )(what|which|why|how|who|where|how|is|are|were|the|a|it|of|do|you|your|please|enter)[ ?]", " ", $str2);
} while ($str1 != $str2);
-
Paged result class example (e.g. automate by database result browsing): http://www.sitepoint.com/print/php-paging-result-sets
-
Timeouts and memory limits from inside the .php file:
ini_set ( "max_execution_time", "240"); // MUST be larger than the timeout (in the source below)
ini_set ( "memory_limit", "130M");
-
Set error reporting from inside the PHP script (note: some errors will not be reported this way and should still be set in php.ini):
ini_set("error_reporting", E_ALL);
ini_set("display_errors", "On");
-
Error Constants and their decimal value: http://us3.php.net/manual/en/errorfunc.constants.php - NOTICE = 8, E_DEPRECATED = 8192, etc.
-
Syntax check can be done w/command line: php -l test-class.php
tags: php, notes, syntax, examples, cheatsheet
>>
Leonard Chan's Homepage
>>
Scribble Web
>> PHP Syntax and other Notes