PHP 1 Introduction Temasek Polytechnic
Background Open Source Apache License Free to redistribute with/without source code http://www.apache.org/license.txt Backed by Zend Corporation http://www.zend.com PHP web site http://www.php.net
Background Runs on many OS platform Linux, Mac OS X, Windows, Unix(es) Runs on many web server Apache, IIS, PWS, Xitami, AOL Server, and many more
PHP vs. Others ASP and ASP.NET Runs on Microsoft Windows OS and IIS web server only For other platform, available from Sun and not free (for ASP) JSP Longer development cycle Complexities of Enterprise level development ColdFusion Easy to use Not free
Background Open Source Software Free (money and mostly freedom) Mostly with liberal license http://www.opensource.org Free Software Free (money and freedom) Guarantee free and will always be free http://www.fsf.org
Background Many software that built the Internet is either Open Source or Free Servers: Email server: Sendmail, Exim, Postfix Web server: Apache, Tomcat Others: BIND (DNS Server) Programming GNU C/C++, Perl, Python, PHP (tons more) Database MySQL, PostgreSQL Web Application and Framework Zope, Nuke, JBoss Operating System Linux, FreeBSD
Client - Server Model In a client-server model, two computers work together to perform a task. A client computer request some information from a server computer. The server computer returns its information, and the client acts on it. The client requests a Web page from the server Client Server The server returns the Web page to the client
Client - Server Interaction for PHP The Web server plays a more active role when an PHP page is requested by the client. Client requests PHP file Server located the PHP file, parses it, removing PHP scripts & replacing it with HTML text Client Server Server returns HTML text to client
Starting PHP PHP syntax is derived from C, Perl, UNIX shell script Similarities with other syntaxes from other popular programming languages such as Java, Javascript, C, C++, Perl
PHP Pages HTML with PHP code embedded in it PHP code is enclosed in <?php // PHP code here?> Other form <? // PHP code here?>
PHP and HTML Combining PHP code and HTML <html> <head><title>set mood</title></head> <? $daytime = date("g", time()); if ($daytime < 12) {?> <body bgcolor="yellow"> <? } elseif($daytime >= 12) {?> <body bgcolor="blue"> <? }?> </body> </html>
Output as HTML Use the print or echo statement Example: <? print( Hello world! ); echo( Hello world! ); echo <h1>hello world!</h1> ; print Hello world!<br> ;?>
Statements and semicolons Statement A group or collection of code that performs a task when executed Semicolons All PHP statement MUST end with a semicolon ( ; ) Example: <?php phpinfo();?>
Comments Comments starts with either // (anything after this symbol is ignored) # (anything after this symbol is ignored) /* (anything inside this block is ignored) */ Examples: <?php // Prints the configuration info phpinfo();?>
Case Sensitivity PHP is not case sensitive, except PHP variable, however, is case sensitive Example the following two are the same print( This page rocks ); PriNt( This page rocks ); Example functions below are the same <?php PHPInfo(); phpinfo();?>
Variable You don t need to declare variable Variable name starts with $ Must start with alphabet or underscore ( _ ) Variable is case sensitive Example these three are not the same $name = Rasmus ; $Name = Rasmus ; $NaMe = Rasmus ; Example these are not legal $3stooges $?what // starts with? $store the total // space // starts with number
Number Integers Example: $a = 1000; $a = -999; Floats Example: $a = 1.234; $a = 1.2e3; $a = 7E-10;
Strings Represents free form text Uses either single quotes ( ) or double quotes ( ) Example: $a = Jack ; $b = Jill ; $c = This web site rocks!!! ; $d = What s up ; $e = I quote, Kopi take away ;
Strings Concatenation combining two or more strings Use. Example: $a = Hey ; $b = Jude ; $c = $a. $b; // Results: Hey Jude
Single vs. Double quotes Double quotes evaluates the variable within the quotes Single quotes doesn t Example: $a = it ; $b = Let $a be ; // results Let it be $c = Let $a be ; // results Let $a be
Boolean Boolean datatype has only two values: true or false Boolean value is not case insensitive Example: $a = True; // assigns TRUE to $a Integer can be converted to Boolean 0 means FALSE Non-zero means TRUE
Operators +, -, /, *, % ++, -- ==,!=! &&, <, >, <=, >= Arithmetic Increment, decrement Equality and inequality Logical NOT Logical AND and OR Less or greater than
Operators and its usage Example $a = 0; $a = $a + 1; // $a => 1 $a += 1; // same as above $a++; // same as above $b = 5 % 2; // yields 1 We ll discuss other operators in Control Structures section
Operators Precedence Which gets evaluated first? *, /, % +, - Example: $a = 2 + 3 * 4; // $a => 14 $a = (2 + 3) * 4; // $a => 20
Control Structures Selection and repetition are the building blocks of programming logic. Branch / Selection Selecting next instruction(s) based on a condition if, elseif, else Loop / Repetition Repeating next instruction(s) based on a condition while for foreach
Branch / Selection Uses boolean algebra to control the flow of execution A == B false true
if, elseif, else Construct if (condition 1) // statement elseif (condition 2) // statement else // statement Condition should evaluate to Boolean true or false elseif and else are optional
if, elseif, else Example: if ($phone == 98888888 ) print Lucky phone number ; elseif ($phone == 94444444 ) print Unlucky phone number ; else print Regular phone number ;
Conditional Test Test that yields boolean true or false Operators used: == for equality test!= for inequality test <, >, >=, <= for less, greater, exclusive and inclusive && the AND logical test the OR logical test
Binary logic AND OR true && true = true true && false = false false && true = false false && false = false true true = true true false = true false true = true false false = false
Conditional branching Example: if ($bp >= 80 && $bp <= 120) print Healthy blood pressure Example: $married = True; if (! $married) print Single ; Example: if ($age >= 60 $income < 30000 print Get social Security allowance
Block of code If if, elseif, and else is followed by more than one statement, curly braces must be used to group the statements together Failure to do so will likely not to cause syntax error, but logic error Example: if ($status == false) { $count++; print Login failed ; header( Location: index.htm ); }
Conclusion PHP basic syntax PHP control structure Branching Looping next week PHP shares some syntax similarities with other languages such as Java, Perl, C, and Unix shell script