1 / 29

PHP

PHP. PHP Language. A recursive acronym: P HP H ypertext P reprocessor A scripting language designed for creating dynamic and data-driven Web pages

pflatt
Download Presentation

PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Technology Park, BDU

  2. PHP Language • A recursive acronym: PHP Hypertext Preprocessor • A scripting language designed for creating dynamic and data-driven Web pages • PHP is a server-side scripting language; it is parsed and interpreted on the server side of a Web application and the resulting output is sent to the browser. (VBScript and JavaScript are mostly client-side). • Designed to work with the MySQL database system, but can also connect to other database systems. Technology Park, BDU

  3. PHP Language • PHP and MySQL are open-source; they can be obtained free-of-charge and the source codes are available for development. • www.php.net • www.mysql.com • You will need to install PHP to work with your MS IIS or Apache Web server. • PHP and MySQL are becoming increasingly popular in recent years because it is free of charge. Other software packages like Microsoft ASP.NET are expensive for small and medium enterprises. Technology Park, BDU

  4. A Simple PHP Example • PHP scripts are enclosed by the <?php And ?> tags. • Can simply use <? for the opening tag. • All PHP statements end with a semicolon (unless it ends the closing tag on the same line)‏ • Comments can be marked by #, // or /* */ <HTML> <HEAD> <TITLE>PHP Example</TITLE> </HEAD> <BODY><? echo "<b>Welcome</b>"; // print the string here ?> </BODY></HTML> Technology Park, BDU http://147.8.210.143/php/example1.php

  5. Including Files • You can include common files (like header, footer, and navigation bars) in PHP. <? include("header.inc") ?> Technology Park, BDU

  6. Variables in PHP • Variable names start with the dollar sign $ • You can use letters and underscore for variable names. • The first character after $ cannot be a number • Variable names are case-sensitive • For example: <? $name = "Michael"; ?> <? $i = 1; ?> Technology Park, BDU

  7. Variables in PHP • The three main types of variables in PHP: • Scalar • Array • Object • Scalar can be Integer, Double, Boolean, or String • When you assign a value to a variable, its data type is also assigned. Technology Park, BDU

  8. Array • Arrays can be created with integers or strings as keys. <?php$arr = array("UK" => "London", 12 => 56);echo $arr["UK"]; // Londonecho $arr[12];    // 56?> Technology Park, BDU

  9. Object • Use the class statement to define a class. • Use the new statement to create an object. <?phpclass test{   function do_something()   {       echo "Do something";    }}$my_test = new test;$my_test->do_something();?> Technology Park, BDU

  10. Basic Operators • Assignment/Arithmetic operators = + - * / % ++ -- += -= *= /= • Comparison operators == (equal value)‏ === (identical value and data type)‏ != or <> < > <= >= • Logical operators ! && || Technology Park, BDU

  11. String Concatenation • Same as Perl • . concatenate strings • .= concatenate and assign • Example: $word1 = "Play"; $full_string = $word1 . "Station"; Technology Park, BDU

  12. String Functions • There are a lot of string functions in PHP that you can use. For example: • int strlen(string str)‏ • string strtoupper(string str)‏ • string strtolower(string str)‏ • int strcmp(string str1, string str2)‏ • int strcasecmp(string str1, string str2)‏ • string strstr(string src, string target)‏ • int strpos(string src, string target [, int offset])‏ • You can find them in the PHP manual: http://www.php.net/manual/en/ Technology Park, BDU

  13. Conditions • boolean variable: TRUE(1) or FALSE(0)‏ • if (EXPR) { STATEMENTS; } • elseif (EXPR) { STATEMENTS; } • else { STATEMENTS; } • switch-case Technology Park, BDU

  14. Loops • while ( EXPR ) { STATEMENTS; } • do { STATEMENTS; } while (EXPR); • for ( INIT_EXPR; COND_EXPR; LOOP_EXPR ) { STATEMENTS; } • foreach (ARRAY as VARIABLE) { STATEMENTS; } • break, continue Technology Park, BDU

  15. Alternative Syntax • It is possible to write control statements and loops using the following “colon” format: • <?php if ($a == 5): echo "A is equal to 5"; endif; ?> Technology Park, BDU

  16. User-defined Functions • Functions can be defined using the function keyword. • <?phpfunction function_name($arg_1, $arg_2, ...){   statements;   return $some_value; // optional }?> Technology Park, BDU

  17. Mixing HTML and PHP Codes • PHP codes can be easily inserted anywhere in an HTML page. • You can even mix the codes together, usually to avoid writing too many “echo” statements with escape characters. <? if ($i == 1) { ?> <h2>The condition is true</h2> <center><b>$i is 1</b></center><? } else { ?> <h2>The condition is false</h2> <center><b>$i is not 1</b></center><? } ?> HTML PHP Technology Park, BDU

  18. Shortcut for Writing Echos • Instead of writing <? echo expression ?> • You can use the following shortcut <?= expression ?> • This is useful for inserting expressions and variables quickly into HTML pages. Technology Park, BDU

  19. Working with HTML Forms • You can easily get the variables submitted by an HTML form using the following (assume the form input is called “name”: • $_POST['name'] // post method • $_GET['name'] // get method • $name /* easier, but Register Globals must be set to ON in PHP config */ Technology Park, BDU

  20. Working with HTML Forms • It is common to put the form and the results of different requests in the same file. <HTML> <HEAD><TITLE>PHP FORM TEST</TITLE></HEAD> <BODY> <? if (!isset($name) || $name == "") { ?> <FORM METHOD="post"> Your name: <INPUT TYPE="text" NAME="name"> Your age: <INPUT TYPE="text" NAME="age"> <INPUT TYPE="submit"> </FORM> <? } else { echo "Your name is $name<BR>"; echo "Your age is $age"; } ?> </BODY> </HTML> If name is empty or not defined, then show the form If name is not empty, i.e., when the user has entered something, then show the results Technology Park, BDU http://localhost/php/example2.php

  21. PHP and MySQL • PHP is designed to work with the MySQL database. However, it can also connect to other database systems such as Oracle, Sybase, etc., using ODBC. Technology Park, BDU

  22. Example <HTML> <BODY> <?php $db = mysql_connect("localhost", "root“,””); mysql_select_db("mydb", $db); $result = mysql_query("SELECT * FROM employees",$db); printf("First Name: %s<br>\n", mysql_result($result,0,"first")); printf("Last Name: %s<br>\n", mysql_result($result,0,"last")); printf("Address: %s<br>\n", mysql_result($result,0,"address")); printf("Position: %s<br>\n",mysql_result($result,0,"position")); mysql_free_result($result); mysql_close($db); ?> </BODY> </HTML> Technology Park, BDU

  23. Useful PHP Functions for MySQL • mysql_connect(host, username [,password]); • Connects to a MySQL server on the specified host using the given username and/or password. Returns a MySQL link identifier on success, or FALSE on failure. • mysql_select_db(db_name [,resource])‏ • Selects a database from the database server. Technology Park, BDU

  24. Useful PHP Functions for MySQL • mysql_query(SQL, resource); • Sends the specified SQL query to the database specified by the resource identifier. The retrieved data are returned by the function as a MySQL result set. • mysql_result(result, row [,field]); • Returns the contents of one cell from a MySQL result set. The field argument can be the field name or the field’s offset. • mysql_fetch_array(result [,result_type])‏ • Fetch a result row as an associative array, a numeric array, or both. The result type can take the constants MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. Technology Park, BDU

  25. Useful PHP Functions for MySQL • mysql_free_result(result)‏ • Frees the result set • mysql_close(resource)‏ • Closes the connection to the database. Technology Park, BDU

  26. Error Handling • If there is error in the database connection, you can terminate the current script by using the die function. • For example: $db = mysql_connect("localhost", "root“, “”) or die("Could not connect : " . mysql_error()); mysql_select_db("my_database") or die("Could not select database"); $result = mysql_query($query) or die("Query failed"); Technology Park, BDU

  27. Example: Looping through the Cells <?php /* Connecting, selecting database */ $link = mysql_connect("mysql_host", "mysql_user", mysql_password")‏ or die("Could not connect : " . mysql_error()); echo "Connected successfully"; mysql_select_db("my_database") or die("Could not select database"); /* Performing SQL query */ $query = "SELECT * FROM my_table"; $result = mysql_query($query) or die("Query failed : " . mysql_error()); /* Printing results in HTML */ echo "<table>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; Loop through each row of the result set Loop through each element in a row Technology Park, BDU

  28. Example: Looping through the Cells /* Free resultset */ mysql_free_result($result); /* Closing connection */ mysql_close($link); ?> Technology Park, BDU

  29. Thank You Technology Park, BDU

More Related