Sunday, May 27, 2007

PHP Language Summary

This is a summary of the PHP language.

Some nice things i found about PHP:

* No need to purchase additional ActiveX controls for email, gif manipulation, pdf handling, etc. Batteries included.
* Runs well on both IIS and Apache, Windows and Linux. Cross-platform power.
* Good performance. PHP is faster than VBScript and JScript, based on the Bagley benchmarks.
* Even if you are a VB programmer, provided you have some Javascript experience, you are already half-way up the learning curve, as PHP looks like Javascript with $ signs in front of variables.
* For those of you who miss ADO (i did), there’s ADOdb, a database abstraction library that works like ADO. And ASP2PHP is an automated VB to PHP converter.
* I recommend this PHP installer for IIS (ok, ok, I admit wrote it). The default PHP installer at php.net is not tuned for high performance IIS usage - this one is.


Feature PHP
Example (print 1 to 10 on a web page)


for ($i=1; $i<=10; $i++) print “$i ”;

?>

Script Tags
If short tags are enabled in PHP.ini:
If ASP tags are enabled in PHP.ini:
<% %><%= return expression %>
End of statement semicolon (;) is compulsory
Comments
// PHP comment 1
/* This is a

multi-line

Comment */
# PHP comment 3
Variables Prefixed by $
Need to declare variables? No, and every variable is preinitialised to type NULL, which is equivalent to false, the empty string and 0.$AVAR = 123;
Loosely Typed Variables Yes. This means that the type of a variable can be changed at run-time.
To change variable type, use typecasting or settype( )
Case-Sensitivity Yes for variables, no for function-names and reserved words
Strings Delimited by single-quotes, double-quotes or PERL heredoc style.
$avar = 'this is a string';

$avar = "this is a string";

$avar = <<
String Concatenation The . (dot) symbol.$s = ‘A’ . ‘B’;
String Evaluation Yes, variables embedded in strings are evaluated if string delimited by double-quotes.$a = “A”;
$b = “$a B”;
/* now $b == ‘A B’ */
Common string constants Line feed: “\n”
Carriage return: “\r”Note that single-quoted ‘n’ is not converted to a line-feed for PHP. Only double-quoted strings are evaluated.
HTML encoding functions htmlspecialchars($str) Converts < > & and ” to their HTML entity equivalents, eg. <>urlencode( ) All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.urldecode( )All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.
Regular Expressions Built-in.if (preg_match (”/php/i”, $str))
print (”php found”);
Dates
$adate = time();

$adate=mktime(0,0,0,1,30,2000);print date('d-m-Y',$adate);
Arrays Declare with
$avar = array(1,’two’);For indexing use [ ]. Arrays begin with zero element.Use sizeof(array) function to get size of array.Arrays can be iterated over using:
$val = reset($arr);

$max = sizeof($arr);

for ($i=0; $i<$max; $i++) { print "$val "; $val = next($arr); }/* or more simply */ foreach ($arr as $element) print "$element ";
Associative Arrays Yes
$avar = array();

$avar['newton']='isaac';

echo $avar['newton'];

Note: You can append new elements to arrays with
$avar[] = ’string to add’;

True and False Has constants true and false.The following also evaluate to false:
0   /* zero */

"" /* empty string */

"0" /* string with zero */
Equality
== true if equal

!= not equal=== true if equal & same type

!== not identical

Note that some functions can return 0 or false (false is typically used to indicate failure) depending on the context. For these functions, you will need to use === to determine whether 0 or false was returned. Some problematic functions: strpos, strrpos, strstr, stristr, strrchr

Assignment statements Allows C style shortcut assignments. For example, to append a string to a variable:$avar .= ‘append to end’;
Sending HTML to Browser print $avar;
echo “The avar variable ‘$avar’ is a string”;
If statements
if (strlen($avar) == 0) {

$avar = "abc";

} else

$avar .= 'end';
While statements
while ($a > 0) {$a -= 1;}
For loops
for ($i=0,$m=9; $i<$m; $i++){  print $i;  }
Switch/Case
switch($aColor) {

case 'red': do1;break;

case 'green': do2;break;

default: do3; break;

}

No comments: