INTERNET PROGRAMMING Software Engineering Branch / 4 th Class Computer Engineering Department University of Technology
OUTLINES PHP Basic 2
ARCHITECTURE OF INTERNET database mysql server-side programming CGI, PHP ASP Perl JSP Java Servlets network Protocols Application client-side programming HTML CSS JavaScript (AJAX) XML 3
COMMON GATEWAY INTERFACE (CGI) CGI was the first server-side processing solution. Problem: CGI program for example acts as a gateway to a database, a new DB connection has to be established for each request which results in a very poor performance. 4
JAVA SERVLETS A Java servlet is a Java class that has to extend the abstract HTTPServlet class. The Java servlet class is loaded by a servlet container and relevant requests (based on a servlet binding) are forwarded to the servlet instance for further processing. 5
JAVA SERVLETS Problem: (e.g. HTML) has to be defined within the servlet, not easy to share tasks between web designer and programmer. 6
JAVASERVER PAGES (JSP) JavaServer Pages (JSP) is a technology that helps software developers create dynamically generated web pages based on HTML, XML, or other document types. Released in 1999 by Sun Microsystems, JSP is similar to PHP, but it uses the Java programming language. 7
HYPERTEXT PREPROCESSOR (PHP) PHP is a server-side scripting language designed for web development but also used as a generalpurpose programming language. 8
HYPERTEXT PREPROCESSOR (PHP) 9
SERVER-SIDE SCRIPTING LANGUAGES There are a number of server-side scripting languages available, including: 1. ASP (*.asp) 2. ActiveVFP (*.avfp) 3. ASP.NET (*.aspx) 4. C (*.c, *.csp) via CGI 5. ColdFusion Markup Language (*.cfm) 6. Go (*.go) 7. Groovy Server Pages (*.gsp) 8. Java (*.jsp) via JavaServer Pages 9. JavaScript using Server-side JavaScript (*.ssjs, *.js) (example: Node.js) 10. Lua (*.lp *.op *.lua) 11. Perl via the CGI.pm module (*.cgi, *.ipl, *.pl) 12. PHP (*.php) 13. R (*.rhtml) - (example: rapache) 14. Python (*.py) (examples: Pyramid, Flask, Django) 15. Ruby (*.rb, *.rbw) (example: Ruby on Rails) 16. SMX (*.smx) 17. Lasso (*.lasso) 18. Tcl (*.tcl) 19. WebDNA (*.dna,*.tpl) 20. Progress WebSpeed (*.r,*.w) 10
HTML, CSS, JAVASCRIPT, XML & PHP DOCUMENTS HTML to define the content of web pages. CSS to specify the layout of web pages. JavaScript to program the behavior of web pages. AJAX to update parts of a web page, without reloading whole page. XML to carry data. PHP to execute on the server. 11
WHAT IS PHP? PHP is an acronym for "PHP: Hypertext Preprocessor. PHP is a widely-used, open source scripting language. PHP scripts are executed on the server. PHP is free to download and use. PHP files can contain text, HTML, CSS, JavaScript, and PHP code. PHP code are executed on the server, and the result is returned to the browser as plain HTML. PHP files have extension ".php. 12
WHAT CAN PHP DO? PHP can generate dynamic page content. PHP can create, open, read, write, delete, and close files on the server. PHP can collect form data. PHP can send and receive cookies. PHP can add, delete, modify data in your database. PHP can be used to control user-access. PHP can encrypt data. 13
PHP SYNTAX <html> <body> <h1>my first PHP page</h1> <?php echo "Hello World!";?> </body> </html> 14
COMMENTS IN PHP <html> <body> <?php // This is a single-line comment # This is also a single-line comment /* This is a multiple-lines comment block that spans over multiple lines */?> </body> </html> 15
PHP NOT CASE-SENSITIVE <html> <body> <?php ECHO "Hello World!<br>"; echo "Hello World!<br>"; EcHo "Hello World!<br>";?> </body> </html> 16
PHP VARIABLE ARE CASE-SENSITIVE <html> <body> <?php $color = "red"; echo "My car is ". $color. "<br>"; echo "My house is ". $COLOR. "<br>"; echo "My boat is ". $color. "<br>";?> </body> </html> 17
PHP DATA TYPES PHP supports the following data types: 1. String 2. Integer 3. Float (floating point numbers - also called double) 4. Boolean 5. Array 6. Object 7. NULL 8. Resource 18
DECLARING PHP VARIABLES <html> <body> <?php $txt = "Hello world!"; $x = 5; $y = 10.5; echo $txt; echo "<br>"; echo $x; echo "<br>"; echo $y;?> </body> </html> 19
PHP OPERATORS PHP divides the operators in the following groups: 1. Arithmetic operators 2. Assignment operators 3. Comparison operators 4. Increment/Decrement operators 5. Logical operators 6. String operators 7. Array operators 20
PHP ARITHMETIC OPERATORS 21
PHP ASSIGNMENT OPERATORS 22
PHP COMPARISON OPERATORS 23
PHP INCREMENT / DECREMENT OPERATORS 24
PHP LOGICAL OPERATORS 25
PHP STRING OPERATORS 26
PHP ARRAY OPERATORS 27
PHP CONDITIONAL STATEMENTS In PHP we have the following conditional statements: 1. if statement - executes some code only if a specified condition is true 2. if...else statement - executes some code if a condition is true and another code if the condition is false 3. if...else if...else statement - specifies a new condition to test, if the first condition is false 4. switch statement - selects one of many blocks of code to be executed 28
PHP CONDITIONAL STATEMENTS <html> <body> <?php $t = date("h"); echo "<p>the hour (of the server) is ". $t; echo ", and will give the following message:</p>"; if ($t < "10") { echo "Have a good morning!"; } elseif ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; }?> </body> </html> 29
PHP CONDITIONAL STATEMENTS <html> <body> <?php $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, or green!"; }?> </body> </html> 30
PHP LOOPS In PHP, we have the following looping statements: 1. while - loops through a block of code as long as the specified condition is true 2. do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true 3. for - loops through a block of code a specified number of times 4. foreach - loops through a block of code for each element in an array 31
PHP FUNCTIONS <html> <body> Hege Refsnes. Born in 1975 Stale Refsnes. Born in 1978 Kai Jim Refsnes. Born in 1983 <?php function familyname($fname, $year) { echo "$fname Refsnes. Born in $year <br>"; } familyname("hege","1975"); familyname("stale","1978"); familyname("kai Jim","1983");?> </body> </html> 32
PHP FUNCTIONS <html> <body> 5 + 10 = 15 7 + 13 = 20 2 + 4 = 6 <?php function sum($x, $y) { $z = $x + $y; return $z; } echo "5 + 10 = ". sum(5,10). "<br>"; echo "7 + 13 = ". sum(7,13). "<br>"; echo "2 + 4 = ". sum(2,4);?> </body> </html> 33
PHP ARRAYS <html> <body> <?php $cars = array("volvo", "BMW", "Toyota"); $arrlength = count($cars); for($x = 0; $x < $arrlength; $x++) { }?> echo $cars[$x]; echo "<br>"; </body> </html> 34
PHP SORTING ARRAYS we will go through the following PHP array sort functions: 1. sort() - sort arrays in ascending order 2. rsort() - sort arrays in descending order 3. asort() - sort associative arrays in ascending order, according to the value 4. ksort() - sort associative arrays in ascending order, according to the key 5. arsort() - sort associative arrays in descending order, according to the value 6. krsort() - sort associative arrays in descending order, according to the key 35
PHP SORTING ARRAYS <html> <body> <?php $numbers = array(4, 6, 2, 22, 11); sort($numbers); 2 4 6 11 22 $arrlength = count($numbers); for($x = 0; $x < $arrlength; $x++) { echo $numbers[$x]; echo "<br>"; }?> </body> </html> 36
REFERENCES 37
Next Lecture PHP 38