Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.

Similar presentations


Presentation on theme: "PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server."— Presentation transcript:

1 PHP

2 What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software PHP is free to download and use

3 Why PHP? PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource: www.php.netwww.php.net PHP is easy to learn and runs efficiently on the server side

4 Basic PHP Syntax PHP script always starts with.

5 Comments in PHP use // to make a one-line comment or /* and */ to make a comment block

6 PHP Variables Variables in PHP starts with a $ sign Variable names are case sensitive <?php $txt="Hello World!"; $x=16; $myCar="Volvo"; ?>

7 PHP Variable Scope The scope of a variable is the portion of the script in which the variable can be referenced. PHP has four different variable scopes: local global static parameter

8 Local Scope A variable declared within a PHP function is local and can only be accessed within that function The script above will not produce any output because the echo statement refers to the local scope variable $a, which has not been assigned a value within this scope

9 Global Scope Global variables can be accessed from any part of the script that is not inside a function Just use the global keyword The script above will output 15

10 The example above can be rewritten as this

11 Static Scope When a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted. To do this, use the static keyword when you first declare the variable <?php $a = 5; $b = 10; static $rememberMe; function myTest() { global $a, $b; $b = $a + $b; } myTest(); echo $b; ?>

12 Parameters or arguments A parameter is a local variable whose value is passed to the function by the calling code ارجومنت الدوال function myTest($para1,$para2,...) { // function code }

13 String Variables in PHP Hello World

14 The Concatenation Operator The concatenation operator (.) is used to put two string values together Hello World! What a nice day!

15 The strlen() function The strlen() function is used to return the length of a string. 12

16 The strpos() function The strpos() function is used to search for a character/text within a string 6 first character position in the string is 0, and not 1

17

18

19

20 Arithmetic Operators

21 Assignment Operators

22 Incrementing/Decrementing Operators

23 Comparison Operators

24 Logical Operators

25 Array Operators

26 PHP If...Else Statements

27

28 "; echo "Have a nice weekend!"; echo "See you on Monday!"; } ?>

29

30 PHP Switch Statement

31 PHP Arrays An array stores multiple values in one single variable. $cars=array("Saab","Volvo","BMW","Toyota"); OR $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota";

32 Saab and Volvo are Swedish cars.

33 Associative Arrays In this example we use an array to assign ages to the different persons: $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); OR $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34";

34 Peter is 32 years old.

35 Multidimensional Arrays In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. $families = array ( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) ); Array ( [Griffin] => Array ( [0] => Peter [1] => Lois [2] => Megan ) [Quagmire] => Array ( [0] => Glenn ) [Brown] => Array ( [0] => Cleveland [1] => Loretta [2] => Junior ) )

36 Add this code to the previous example echo "Is ". $families['Griffin'][2]. " a part of the Griffin family?"; The out will be Is Megan a part of the Griffin family?

37 PHP Looping - While Loops "; $i++; } ?> The number is 1 The number is 2 The number is 3 The number is 4 The number is 5

38 The do...while Statement "; } while ($i The number is 2 The number is 3 The number is 4 The number is 5 The number is 6

39 PHP Looping - For Loops "; } ?> The number is 1 The number is 2 The number is 3 The number is 4 The number is 5

40 The foreach Loop "; } ?> one two three

41 PHP Functions The real power of PHP comes from its functions. In PHP, there are more than 700 built-in functions My name is Kai Jim Refsnes

42 "; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?> My name is Kai Jim Refsnes. My sister's name is Hege Refsnes. My brother's name is Stale Refsnes.

43 "; } echo "My name is "; writeName("Kai Jim","."); echo "My sister's name is "; writeName("Hege","!"); echo "My brother's name is "; writeName("Ståle","?"); ?> My name is Kai Jim Refsnes. My sister's name is Hege Refsnes! My brother's name is Ståle Refsnes?

44 PHP Functions - Return values 1 + 16 = 17

45 PHP Forms and User Input Name: Age: "welcome.php" looks like this: Welcome ! You are years old. Welcome John! You are 28 years old.

46 The PHP $_GET and $_POST variables Name: Age: The "welcome.php" file can now use the $_GET variable to collect form data (the names of the form fields will automatically be the keys in the $_GET array): Welcome. You are years old! When the user clicks the "Submit" button, the URL sent to the server could look something like this: http://www.w3schools.com/welcome.ph p?fname=Peter&age=37

47 When to use method="get"? When using method="get" in HTML forms, all variable names and values are displayed in the URL

48 PHP $_POST Function The predefined $_POST variable is used to collect values from a form sent with method="post“. Name: Age: When the user clicks the "Submit" button, the URL will look like this: http://www.w3schools.com/welcome.php The "welcome.php" file can now use the $_POST variable to collect form data (the names of the form fields will automatically be the keys in the $_POST array): Welcome ! You are years old. When to use method="post"? Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

49 The PHP $_ REQUEST Variable The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods. Welcome ! You are years old.

50 PHP Date() Function "; echo date("Y.m.d"). " "; echo date("Y-m-d"); ?> 2009/05/11 2009.05.11 2009-05-11 Tomorrow is 2009/05/12

51 PHP Include File Assume that you have a standard header file, called "header.php". To include the header file in a page, use the include() function: You can insert the content of one PHP file into another PHP file before the server executes it, with the include() or require() function. The two functions are identical in every way, except how they handle errors: include() generates a warning, but the script will continue execution require() generates a fatal error, and the script will stop

52 Example Assume that you have a standard header file, called "header.php". To include the header file in a page, use the include() function: Welcome to my home page! Some text.

53 Assume we have a standard menu file, called "menu.php", that should be used on all pages Home Tutorials References Examples About Us Contact Us All pages in the Web site should include this menu file. Here is how it can be done: Welcome to my home page. Some text.

54 If you look at the source code of the page above (in a browser), it will look like this: Home Tutorials References Examples About Us Contact Us Welcome to my home page! Some text.

55 File handling

56

57 The following example generates a message if the fopen() function is unable to open the specified file:

58 Closing a File The fclose() function is used to close an open file:

59 Check End-of-file The feof() function checks if the "end-of-file" (EOF) has been reached. The feof() function is useful for looping through data of unknown length. Note: You cannot read from files opened in w, a, and x mode! if (feof($file)) echo "End of file";

60 Reading a File Line by Line The example below reads a file line by line, until the end of file is reached: "; } fclose($file); ?>

61 Reading a File Character by Character The example below reads a file character by character, until the end of file is reached:

62 Create an Upload-File Form To allow users to upload files from a form can be very useful. Look at the following HTML form for uploading files: Filename:

63 Create The Upload Script The "upload_file.php" file contains the code for uploading a file: 0) { echo "Error: ". $_FILES["file"]["error"]. " "; } else { echo "Upload: ". $_FILES["file"]["name"]. " "; echo "Type: ". $_FILES["file"]["type"]. " "; echo "Size: ". ($_FILES["file"]["size"] / 1024). " Kb "; echo "Stored in: ". $_FILES["file"]["tmp_name"]; } ?>

64 $_FILES["file"]["name"] - the name of the uploaded file $_FILES["file"]["type"] - the type of the uploaded file $_FILES["file"]["size"] - the size in bytes of the uploaded file $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server $_FILES["file"]["error"] - the error code resulting from the file upload

65 Restrictions on Upload The user may only upload.gif or.jpeg files and the file size must be under 20 kb: 0) { echo "Error: ". $_FILES["file"]["error"]. " "; } else { echo "Upload: ". $_FILES["file"]["name"]. " "; echo "Type: ". $_FILES["file"]["type"]. " "; echo "Size: ". ($_FILES["file"]["size"] / 1024). " Kb "; echo "Stored in: ". $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; } ?>

66 Saving the Uploaded File To store the uploaded file we need to copy it to a different location: 0) { echo "Return Code: ". $_FILES["file"]["error"]. " "; } else { echo "Upload: ". $_FILES["file"]["name"]. " "; echo "Type: ". $_FILES["file"]["type"]. " "; echo "Size: ". ($_FILES["file"]["size"] / 1024). " Kb "; echo "Temp file: ". $_FILES["file"]["tmp_name"]. " "; if (file_exists("upload/". $_FILES["file"]["name"])) { echo $_FILES["file"]["name"]. " already exists. "; } else { move_uploaded_file($_FILES["file"]["tm p_name"], "upload/". $_FILES["file"]["name"]); echo "Stored in: ". "upload/". $_FILES["file"]["name"]; }}} else { echo "Invalid file"; } ?>

67


Download ppt "PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server."

Similar presentations


Ads by Google