The document provides an introduction to PHP, including:
- PHP is a scripting language originally designed for web pages and runs on most operating systems.
- PHP syntax is quite easy if familiar with C-type languages, and it is designed to output to browsers but can also create CLI apps.
- Variables, constants, naming conventions, data types, and basic control structures like if/else, while loops and foreach loops are discussed.
- Combining PHP with XHTML is covered, recommending using functions and an object-oriented approach.
- User input via forms is mentioned, linking to a resource on processing forms with PHP.
3. Three-tiered Web Site: LAMP Client User-agent: Firefox Server Apache HTTP Server example request GET / HTTP/1.1 Host: www.tamk.fi User-Agent: Mozilla/5.0 (Mac..) ... response Database MySQL PHP
4. Java EE Architecture (x)html / xml Applet Client Application J2EE Application Server Web Container Servlets JSP JSPs Servlets EJB Container RMI/IIOP JNDI JTA JDBC JMS JavaMail JAF Session Beans Entity Beans Msg-Driven Beans RMI/IIOP JNDI JTA JDBC JMS JavaMai l JAF HTTP(S) JDBC JavaMail RMI IIOP JNDI JMS DB Java-Application CORBA Server Message Queue Directory Service Client Mail Server
5. Server Side Techniques Server side scripting requires installation on the server side Typically client siis only xhtml and it unaware that the xhtml was produced by a server side script Does not require any installations or add-ons on the client
7. Client Side Techniques Requires that the client supports the technique JavaScript, Applet, Flash…
8. Web Application Frameworks A web application framework is a software framework that is designed to support the development of dynamic websites, Web applications and Web services. Numerous frameworks available for many languages
9. Web App vs. Web Site? What’s the difference between Web App and Web Site? Rich Internet Application?, AJAX?, Thin Client? Full application running in your browser or just a web site?
11. Introduction to PHP PHP is a computer scripting language. Originally designed for producing dynamic web pages Appeared in 1995 PHP Group is responsible for the language, no formal specification Free software Runs on most operating systems and platforms URL: http://www.php.net
13. Introduction to PHP Syntax PHP has quite easy syntax, if you are familiar with any c-type language It has all the same structures that you are familiar with other programming languages PHP is designed to output to browser, but it is possible to do also CLI apps.
14. Example <?php print "What is your name?"; $name = trim(fgets(STDIN)); print "Hello " . $name; ?>
15. Variables Variables in PHP are represented by a dollar sign PHP supports eight types: boolean, integer, float, double, array, object, resource and NULL
16. Example (php.net) <?php $a_bool = TRUE; // a boolean $a_str = "foo"; // a string $a_str2 = 'foo'; // a string $an_int = 12; // an integer echo gettype($a_bool); // prints out: boolean echo gettype($a_str); // prints out: string // If this is an integer, increment it by four if (is_int($an_int)) { $an_int += 4; } // If $bool is a string, print it out // (does not print out anything) if (is_string($a_bool)) { echo "String: $a_bool"; } ?>
17. Naming Variables Case-sensitivity Start with letter or _ After that you can have numbers, letters and _ $var = 'Bob'; $Var = 'Joe'; print "$var, $Var"; $4site = 'not yet'; $_4site = 'not yet';
18. Constants You cannot alter the value of constant after declaration define(CONSTANT, "value"); print CONSTANT;
19. Magic Constants PHP has lot of predefined variables Also predefined constants: __LINE__ __FILE__ __FUNCTION__ __CLASS__ __METHOD__ See: http://fi.php.net/manual/en/language.constants.predefined.php
20. Scope <?php $a = "Pekka"; print ”My name is " . $a; ?>
21. Scope <?php $a = "Pekka"; function Test() { print $a; } print ”My name is ”; Test(); ?>
22. Scope <?php $a = "Pekka"; function Test() { global $a; print $a; } print ”My name is ”; Test(); ?>
23. Control Structures If, else, elseif, switch while, do-while, for foreach break, continue
25. Strings Single quoted: 'this is a $variable' Double quoted: "this is a $variable" Heredoc: $str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD;
36. IF <?php if ($a > $b) { echo "a is bigger than b"; } else { echo "a is NOT bigger than b"; } if ($a > $b) { echo "a is bigger than b"; } elseif ($a == $b) { echo "a is equal to b"; } else { echo "a is smaller than b"; } ?>
37. While and Do-While <?php $a=0; while($a<10){ print $a; $a++; } $i = 0; do { print $i; } while ($i > 0); ?>
38. For for ($i = 1; $i <= 10; $i++) { print $i; }