What Is Php

5,978 views

Published on

Introduction of PHP andmysql

Published in: Technology
1 Comment
8 Likes
Statistics
Notes
  • Thanks... I m from sri lanka how r u Dear
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
5,978
On SlideShare
0
From Embeds
0
Number of Embeds
984
Actions
Shares
0
Downloads
451
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide

What Is Php

  1. 1. What is PHP? <ul><li>PHP stands for PHP: Hypertext Preprocessor </li></ul><ul><li>PHP is a server-side scripting languagePHP scripts are executed on the server. </li></ul><ul><li>PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) </li></ul><ul><li>PHP is an open source software (OSS) </li></ul>
  2. 2. Why PHP? <ul><li>PHP runs on different platforms (Windows, Linux, Unix, etc.) </li></ul><ul><li>PHP is compatible with almost all servers used today (Apache, IIS, etc.) </li></ul><ul><li>PHP is FREE to download from the official PHP resource: www.php.net </li></ul><ul><li>PHP is easy to learn </li></ul>
  3. 3. Where to Start? <ul><li>Install an Apache server on a Windows or Linux machine </li></ul><ul><li>Install PHP on a Windows or Linux machine </li></ul><ul><li>Install MySQL on a Windows or Linux machine </li></ul>
  4. 4. <ul><li>Download PHP for free here: http://www.php.net/downloads.php </li></ul><ul><li>Download MySQL for free here: http://www.mysql.com/downloads/index.html </li></ul><ul><li>Download Apache for free here: http://httpd.apache.org/download.cgi </li></ul>
  5. 5. Basic PHP Syntax <ul><li>A PHP scripting block always </li></ul><ul><ul><li>starts with <?php and </li></ul></ul><ul><ul><li>ends with ?> </li></ul></ul><ul><li>Example </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>…… .…. </li></ul></ul><ul><ul><li>……… .. </li></ul></ul><ul><ul><li>?> </li></ul></ul>
  6. 6. What is a PHP File? <ul><li>A PHP file normally contains </li></ul><ul><ul><li>HTML tags, just like an HTML file </li></ul></ul><ul><ul><li>some PHP scripting code </li></ul></ul>
  7. 7. Combining HTML and PHP <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>A PHP script including HTML</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><b> </li></ul><ul><li>&quot;hello world- HTML&quot;; </li></ul><ul><li><?php </li></ul><ul><li>echo &quot;hello world-PHP&quot;; </li></ul><ul><li>?> </li></ul><ul><li></b> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  8. 8. Comments in PHP <ul><li>In PHP, we use </li></ul><ul><ul><li>// or # to make a single-line comment </li></ul></ul><ul><ul><li>/* and */ to make a large comment block. </li></ul></ul>
  9. 9. Example <ul><ul><ul><ul><li><html> </li></ul></ul></ul></ul><ul><ul><ul><ul><li><body> </li></ul></ul></ul></ul><ul><ul><ul><ul><li><?php </li></ul></ul></ul></ul><ul><ul><ul><ul><li>//This is a single-line comment </li></ul></ul></ul></ul><ul><ul><ul><ul><li>/* </li></ul></ul></ul></ul><ul><ul><ul><ul><li>This is </li></ul></ul></ul></ul><ul><ul><ul><ul><li>a comment </li></ul></ul></ul></ul><ul><ul><ul><ul><li>block </li></ul></ul></ul></ul><ul><ul><ul><ul><li>*/ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>?> </li></ul></ul></ul></ul><ul><ul><ul><ul><li></body> </li></ul></ul></ul></ul><ul><ul><ul><ul><li></html> </li></ul></ul></ul></ul>
  10. 10. Variables in PHP <ul><li>Variables are used for storing a values, like text strings, numbers or arrays. </li></ul><ul><li>When a variable is set it can be used over and over again in your script </li></ul><ul><li>All variables in PHP start with a $ sign symbol. </li></ul><ul><li>The correct way of setting a variable in PHP: </li></ul><ul><ul><li>$var_name = value; </li></ul></ul><ul><ul><li>Eg: $name=“Sunil”; </li></ul></ul>
  11. 11. Example <ul><ul><li><?php </li></ul></ul><ul><ul><li>$txt = &quot;Hello World!&quot;; </li></ul></ul><ul><ul><li>$number = 16; </li></ul></ul><ul><ul><li>echo $txt; </li></ul></ul><ul><ul><li>echo $number; </li></ul></ul><ul><ul><li>?> </li></ul></ul>
  12. 12. Variable Naming Rules <ul><li>A variable name must start with a letter or an underscore &quot;_&quot; </li></ul><ul><li>A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ ) </li></ul><ul><li>A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString) </li></ul>
  13. 13. PHP String <ul><li>String variables are used for values that contains character strings. </li></ul><ul><li>Example 1: </li></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>$txt=&quot;Hello World&quot;; </li></ul></ul></ul><ul><ul><ul><li>echo $txt; </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><ul><li>The output of the code above will be: </li></ul></ul><ul><ul><li>Hello World </li></ul></ul>
  14. 14. PHP String <ul><li>Example 2: </li></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>$txt1=&quot;Hello World&quot;; </li></ul></ul></ul><ul><ul><ul><li>$txt2=&quot;1234&quot;; </li></ul></ul></ul><ul><ul><ul><li>echo $txt1 . &quot; &quot; . $txt2; </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><li>The output of the code above will be: </li></ul><ul><li>Hello World 1234 </li></ul>
  15. 15. PHP String <ul><li>Example 2: </li></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>echo strlen(&quot;Hello world!&quot;); </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><li>The output of the code above will be: </li></ul><ul><li>12 </li></ul>
  16. 16. PHP Operators <ul><li>Arithmetic Operators </li></ul><ul><ul><li>+ Addition </li></ul></ul><ul><ul><li>- Subtraction </li></ul></ul><ul><ul><li>* Multiplication </li></ul></ul><ul><ul><li>/ Division </li></ul></ul><ul><ul><li>% Modulus (division remainder) </li></ul></ul><ul><ul><li>++ Increment </li></ul></ul><ul><ul><li>-- Decrement </li></ul></ul>
  17. 17. PHP Operators <ul><li>Comparison Operators </li></ul><ul><li>== is equal to 5==8 returns false </li></ul><ul><li>!= is not equal 5!=8 returns true </li></ul><ul><li>> is greater than 5>8 returns false </li></ul><ul><li>< is less than 5<8 returns true </li></ul><ul><li>>= is greater than or equal to 5>=8 returns false </li></ul><ul><li><= is less than or equal to 5<=8 returns true </li></ul>
  18. 18. Conditional Statements <ul><li>If...Else Statement </li></ul><ul><li>If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement. </li></ul><ul><li>Syntax </li></ul><ul><ul><ul><li>if (condition) </li></ul></ul></ul><ul><ul><ul><li>code to be executed if condition is true; </li></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><li>code to be executed if condition is false; </li></ul></ul></ul>
  19. 19. Conditional Statements <ul><li>Example 1: </li></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>$d=date(&quot;D&quot;); </li></ul></ul><ul><ul><li>if ($d==&quot;Fri&quot;) </li></ul></ul><ul><ul><li>echo &quot;Have a nice weekend!&quot;; </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>echo &quot;Have a nice day!&quot;; </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  20. 20. Conditional Statements <ul><li>Example 2: </li></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>$d=date(&quot;D&quot;); </li></ul></ul><ul><ul><li>if ($d==&quot;Fri&quot;) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>echo &quot;Hello!<br />&quot;; </li></ul></ul><ul><ul><li>echo &quot;Have a nice weekend!&quot;; </li></ul></ul><ul><ul><li>echo &quot;See you on Monday!&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  21. 21. Conditional Statements <ul><li>If you want to execute some code if one of several conditions are true use the elseif statement </li></ul><ul><li>Syntax </li></ul><ul><ul><li>if (condition) </li></ul></ul><ul><ul><li>code to be executed if condition is true; </li></ul></ul><ul><ul><li>elseif (condition) </li></ul></ul><ul><ul><li>code to be executed if condition is true; </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>code to be executed if condition is false; </li></ul></ul>
  22. 22. Conditional Statements <ul><li>Example 3: </li></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>$d=date(&quot;D&quot;); </li></ul></ul><ul><ul><li>if ($d==&quot;Fri&quot;) </li></ul></ul><ul><ul><li>echo &quot;Have a nice weekend!&quot;; </li></ul></ul><ul><ul><li>elseif ($d==&quot;Sun&quot;) </li></ul></ul><ul><ul><li>echo &quot;Have a nice Sunday!&quot;; </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>echo &quot;Have a nice day!&quot;; </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  23. 23. PHP Switch Statement <ul><li>If you want to select one of many blocks of code to be executed, use the Switch statement. </li></ul><ul><li>The switch statement is used to avoid long blocks of if..elseif..else code. </li></ul>
  24. 24. PHP Switch Statement <ul><li>Syntax </li></ul><ul><ul><ul><li>switch (expression) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>case label1: </li></ul></ul></ul><ul><ul><ul><li>code to be executed if expression = label1; </li></ul></ul></ul><ul><ul><ul><li>break; </li></ul></ul></ul><ul><ul><ul><li>case label2: </li></ul></ul></ul><ul><ul><ul><li>code to be executed if expression = label2; </li></ul></ul></ul><ul><ul><ul><li>break; </li></ul></ul></ul><ul><ul><ul><li>default: </li></ul></ul></ul><ul><ul><ul><li>code to be executed </li></ul></ul></ul><ul><ul><ul><li>if expression is different from both label1 and label2; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  25. 25. PHP Switch Statement <ul><li>Example: </li></ul><ul><ul><ul><li><html> </li></ul></ul></ul><ul><ul><ul><li><body> </li></ul></ul></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>$x=2; </li></ul></ul></ul><ul><ul><ul><li>switch ($x) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>case 1: </li></ul></ul></ul><ul><ul><ul><li>echo &quot;Number 1&quot;; </li></ul></ul></ul><ul><ul><ul><li>break; </li></ul></ul></ul><ul><ul><ul><li>case 2: </li></ul></ul></ul><ul><ul><ul><li>echo &quot;Number 2&quot;; </li></ul></ul></ul><ul><ul><ul><li>break; </li></ul></ul></ul><ul><ul><ul><li>case 3: </li></ul></ul></ul><ul><ul><ul><li>echo &quot;Number 3&quot;; </li></ul></ul></ul><ul><ul><ul><li>break; </li></ul></ul></ul><ul><ul><ul><li>default: </li></ul></ul></ul><ul><ul><ul><li>echo &quot;No number between 1 and 3&quot;; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><ul><ul><li></body> </li></ul></ul></ul><ul><ul><ul><li></html> </li></ul></ul></ul>
  26. 26. Exercise : <ul><li>Assign values to two variables. Use comparison operators to test whether the first value is </li></ul><ul><ul><li>The same as the second </li></ul></ul><ul><ul><li>Less than the second </li></ul></ul><ul><ul><li>Greater than the second </li></ul></ul><ul><ul><li>Less than or equal to the second </li></ul></ul><ul><li>Print the result of each test to the browser. </li></ul><ul><li>Change the values assigned to your test variables and run the script again. </li></ul>
  27. 27. PHP Arrays <ul><li>What is an array? </li></ul><ul><ul><li>When working with PHP, sooner or later, you might want to create many similar variables. </li></ul></ul><ul><ul><li>Instead of having many similar variables, you can store the data as elements in an array. </li></ul></ul><ul><ul><li>Each element in the array has its own ID so that it can be easily accessed. </li></ul></ul>
  28. 28. PHP Arrays : Example 1 <ul><li><?php </li></ul><ul><li>$names[0] = &quot;Peter&quot;; </li></ul><ul><li>$names[1] = &quot;Quagmire&quot;; </li></ul><ul><li>$names[2] = &quot;Joe&quot;; </li></ul><ul><li>echo $names[1] . &quot; and &quot; . $names[2] . </li></ul><ul><li>&quot; are &quot;. $names[0] . &quot;'s neighbors&quot;; </li></ul><ul><li>?> </li></ul><ul><li>The code above will output: </li></ul><ul><li>Quagmire and Joe are Peter's neighbors </li></ul>
  29. 29. PHP Arrays : Example 2 <ul><li><?php </li></ul><ul><li>$names = array(&quot;Peter&quot;,&quot;Quagmire&quot;,&quot;Joe&quot;); </li></ul><ul><li>echo $names[1] . &quot; and &quot; . $names[2] . </li></ul><ul><li>&quot; are &quot;. $names[0] . &quot;'s neighbors&quot;; </li></ul><ul><li>?> </li></ul><ul><li>The code above will output: </li></ul><ul><li>Quagmire and Joe are Peter's neighbors </li></ul>
  30. 30. Multidimensional Arrays <ul><li>In a multidimensional array, </li></ul><ul><ul><li>each element in the main array can also be an array. </li></ul></ul><ul><ul><li>each element in the sub-array can be an array, and so on </li></ul></ul>
  31. 31. Multidimensional Arrays <ul><li>In a multidimensional array, </li></ul><ul><ul><li>each element in the main array can also be an array. </li></ul></ul><ul><ul><li>each element in the sub-array can be an array, and so on </li></ul></ul>
  32. 32. PHP Looping <ul><li>Looping statements in PHP are used to execute the same block of code a specified number of times. </li></ul>
  33. 33. PHP Looping <ul><li>Looping statements in PHP are used to execute the same block of code a specified number of times. </li></ul><ul><li>In PHP we have the following looping statements: </li></ul><ul><ul><li>while - loops </li></ul></ul><ul><ul><li>do...while - loops </li></ul></ul><ul><ul><li>for - loops </li></ul></ul><ul><ul><li>foreach - loops </li></ul></ul>
  34. 34. The while Statement <ul><li>The while statement will execute a block of code if and as long as a condition is true. </li></ul><ul><li>Syntax </li></ul><ul><ul><li>while (condition) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>statement 1; </li></ul></ul><ul><ul><li>statement 2; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  35. 35. The while Statement <ul><li>Example </li></ul><ul><li> . </li></ul><ul><ul><li>. </li></ul></ul><ul><ul><li>. </li></ul></ul><ul><ul><li>$i=1; </li></ul></ul><ul><ul><li>while($i<=5) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>echo &quot;The number is &quot; . $i . &quot;<br />&quot;; </li></ul></ul><ul><ul><li>$i++; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>. </li></ul></ul><ul><ul><li>. </li></ul></ul><ul><ul><li>. </li></ul></ul>
  36. 36. do...while Statement <ul><li>The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true. </li></ul><ul><li>Syntax </li></ul><ul><ul><li>do </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>code to be executed; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>while (condition); </li></ul></ul>
  37. 37. do...while Statement <ul><li>Example </li></ul><ul><ul><ul><li>. </li></ul></ul></ul><ul><ul><ul><li>. </li></ul></ul></ul><ul><ul><ul><li>$i=0; </li></ul></ul></ul><ul><ul><ul><li>do </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>$i++; </li></ul></ul></ul><ul><ul><ul><li>echo &quot;The number is &quot; . $i . &quot;<br />&quot;; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>while ($i<5); </li></ul></ul></ul><ul><ul><ul><li>. </li></ul></ul></ul><ul><ul><ul><li>. </li></ul></ul></ul>
  38. 38. The for Statement <ul><li>The for statement is used when you know how many times you want to execute a statement or a list of statements. </li></ul><ul><li>Syntax </li></ul><ul><ul><ul><li>for (initialization; condition; increment) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>code to be executed; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  39. 39. The for Statement <ul><li>Example </li></ul><ul><ul><ul><li>. </li></ul></ul></ul><ul><ul><ul><li>. </li></ul></ul></ul><ul><ul><ul><li>for ($i=1; $i<=5; $i++) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>echo &quot;Hello World!<br />&quot;; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>. </li></ul></ul></ul><ul><ul><ul><li>. </li></ul></ul></ul>
  40. 40. The foreach Statement <ul><li>The foreach statement is used to loop through arrays. </li></ul><ul><li>For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop, you'll be looking at the next element. </li></ul><ul><li>Syntax </li></ul><ul><ul><ul><li>foreach (array as value) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>code to be executed; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  41. 41. The foreach Statement <ul><li>Example </li></ul><ul><li>The following example demonstrates a loop that will print the values of the given array: </li></ul><ul><ul><ul><li><html> </li></ul></ul></ul><ul><ul><ul><li><body> </li></ul></ul></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>$arr=array(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;); </li></ul></ul></ul><ul><ul><ul><li>foreach ($arr as $value) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>echo &quot;Value: &quot; . $value . &quot;<br />&quot;; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><ul><ul><li></body> </li></ul></ul></ul><ul><ul><ul><li></html> </li></ul></ul></ul>
  42. 42. PHP Functions <ul><li>A function is a block of code that can be executed whenever we need it. </li></ul><ul><li>Creating PHP functions: </li></ul><ul><ul><li>All functions start with the word &quot;function()&quot; </li></ul></ul><ul><ul><li>Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number) </li></ul></ul><ul><ul><li>Add a &quot;{&quot; - The function code starts after the opening curly brace </li></ul></ul><ul><ul><li>Insert the function code </li></ul></ul><ul><ul><li>Add a &quot;}&quot; - The function is finished by a closing curly brace </li></ul></ul>
  43. 43. Example 1 <ul><li>A simple function that writes my name when it is called: </li></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>function writeMyName() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>echo “Nimal&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>writeMyName(); </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  44. 44. Example 2 <ul><ul><ul><li><html> </li></ul></ul></ul><ul><ul><ul><li><body> </li></ul></ul></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>function writeMyName() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>echo &quot;Kai Jim Refsnes&quot;; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>echo &quot;Hello world!<br />&quot;; </li></ul></ul></ul><ul><ul><ul><li>echo &quot;My name is &quot;; </li></ul></ul></ul><ul><ul><ul><li>writeMyName(); </li></ul></ul></ul><ul><ul><ul><li>echo &quot;.<br />That's right, &quot;; </li></ul></ul></ul><ul><ul><ul><li>writeMyName(); </li></ul></ul></ul><ul><ul><ul><li>echo &quot; is my name.&quot;; </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><ul><ul><li></body> </li></ul></ul></ul><ul><ul><ul><li></html> </li></ul></ul></ul>
  45. 45. Example 3 <ul><li>The following example will write different first names, </li></ul><ul><ul><ul><li><html> </li></ul></ul></ul><ul><ul><ul><li><body> </li></ul></ul></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>function writeMyName($fname) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>echo $fname . &quot; .<br />&quot;; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>echo &quot;My name is &quot;; </li></ul></ul></ul><ul><ul><ul><li>writeMyName(“Nimal&quot;); </li></ul></ul></ul><ul><ul><ul><li>echo &quot;My name is &quot;; </li></ul></ul></ul><ul><ul><ul><li>writeMyName(“Kamal&quot;); </li></ul></ul></ul><ul><ul><ul><li>echo &quot;My name is &quot;; </li></ul></ul></ul><ul><ul><ul><li>writeMyName(“Amal&quot;); </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><ul><ul><li></body> </li></ul></ul></ul><ul><ul><ul><li></html> </li></ul></ul></ul>
  46. 46. Example 4 <ul><li>The following function has two parameters </li></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>function writeMyName($fname,$lname) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>echo $fname . &quot; Refsnes&quot; . $lname . &quot;<br />&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>echo &quot;My name is &quot;; </li></ul></ul><ul><ul><li>writeMyName(“Nimal&quot;,“Amarasinghe&quot;); </li></ul></ul><ul><ul><li>echo &quot;My name is &quot;; </li></ul></ul><ul><ul><li>writeMyName(“Kamal&quot;,“Perera&quot;); </li></ul></ul><ul><ul><li>echo &quot;My name is &quot;; </li></ul></ul><ul><ul><li>writeMyName(“Amal&quot;,“Silva&quot;); </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  47. 47. PHP Functions - Return values <ul><li>Functions can also be used to return values. </li></ul><ul><li>Example </li></ul><ul><ul><ul><li><html> </li></ul></ul></ul><ul><ul><ul><li><body> </li></ul></ul></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>function add($x,$y) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>$total = $x + $y; </li></ul></ul></ul><ul><ul><ul><li>return $total; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>echo &quot;1 + 16 = &quot; . add(1,16); </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><ul><ul><li></body> </li></ul></ul></ul><ul><ul><ul><li></html> </li></ul></ul></ul>
  48. 48. PHP Forms <ul><li>The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. </li></ul>
  49. 49. Example <ul><li>Welcome.htm </li></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li><form action=&quot;welcome.php&quot; method=&quot;post&quot;> </li></ul></ul><ul><ul><li>Name: <input type=&quot;text&quot; name=&quot;name&quot; /> </li></ul></ul><ul><ul><li>Age: <input type=&quot;text&quot; name=&quot;age&quot; /> </li></ul></ul><ul><ul><li><input type=&quot;submit&quot; /> </li></ul></ul><ul><ul><li></form> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  50. 50. Example <ul><li>welcome.php </li></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li>Welcome <?php echo $_POST[&quot;name&quot;]; ?>.<br /> </li></ul></ul><ul><ul><li>You are <?php echo $_POST[&quot;age&quot;]; ?> years old. </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  51. 51. PHP $_GET <ul><li>The $_GET variable is used to collect values from a form with method=&quot;get&quot;. </li></ul><ul><li>Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) </li></ul><ul><li>It has limits on the amount of information to send (max. 100 characters). </li></ul>
  52. 52. Example <ul><ul><ul><li><form action=&quot;welcome.php&quot; method=&quot;get&quot;> </li></ul></ul></ul><ul><ul><ul><li>Name: <input type=&quot;text&quot; name=&quot;name&quot; /> </li></ul></ul></ul><ul><ul><ul><li>Age: <input type=&quot;text&quot; name=&quot;age&quot; /> </li></ul></ul></ul><ul><ul><ul><li><input type=&quot;submit&quot; /> </li></ul></ul></ul><ul><ul><ul><li></form> </li></ul></ul></ul><ul><li>Welcome.php </li></ul><ul><ul><ul><li>Welcome <?php echo $_GET[&quot;name&quot;]; ?>.<br /> </li></ul></ul></ul><ul><ul><ul><li>You are <?php echo $_GET[&quot;age&quot;]; ?> years old! </li></ul></ul></ul>
  53. 53. Note: <ul><li>When using the $_GET variable all variable names and values are displayed in the URL. </li></ul><ul><li>So this method should not be used when sending passwords or other sensitive information! </li></ul><ul><li>The HTTP GET method is not suitable on large variable values; the value cannot exceed 100 characters. </li></ul>
  54. 54. PHP $_POST <ul><li>The $_POST variable is used to collect values from a form with method=&quot;post&quot;. </li></ul><ul><li>Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. </li></ul>
  55. 55. Example <ul><ul><li><form action=&quot;welcome.php&quot; method=&quot;post&quot;> </li></ul></ul><ul><ul><li>Enter your name: <input type=&quot;text&quot; name=&quot;name&quot; /> </li></ul></ul><ul><ul><li>Enter your age: <input type=&quot;text&quot; name=&quot;age&quot; /> </li></ul></ul><ul><ul><li><input type=&quot;submit&quot; /> </li></ul></ul><ul><ul><li></form> </li></ul></ul><ul><li>Welcome.php </li></ul><ul><ul><li>Welcome <?php echo $_POST[&quot;name&quot;]; ?>.<br /> </li></ul></ul><ul><ul><li>You are <?php echo $_POST[&quot;age&quot;]; ?> years old! </li></ul></ul>
  56. 56. PHP Database
  57. 57. Connecting to a MySQL Database <ul><li>Before you can access and work with data in a database, you must create a connection to the database. </li></ul><ul><li>In PHP, this is done with the mysql_connect() function. </li></ul><ul><li>Syntax </li></ul><ul><ul><li>mysql_connect(servername,username,password); </li></ul></ul><ul><ul><li>servername Optional. Specifies the server to connect to. Default value is &quot;localhost:3306&quot; </li></ul></ul><ul><ul><li>username Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process </li></ul></ul><ul><ul><li>password Optional. Specifies the password to log in with. Default is &quot;&quot; </li></ul></ul>
  58. 58. Example <ul><li>In the following example we store the connection in a variable ($con). The &quot;die&quot; part will be executed if the connection fails: </li></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>$con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;); </li></ul></ul></ul><ul><ul><ul><li>if (!$con) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>die('Could not connect: ' . mysql_error()); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>// some code </li></ul></ul></ul><ul><ul><ul><li>mysql_close($con); </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul>
  59. 59. Create MySQLDatabase and Tables <ul><li>To get PHP to execute a MySQL statement we must use the mysql_query() function. </li></ul><ul><li>This function is used to send a query or command to a MySQL connection. </li></ul>
  60. 60. Example <ul><li>In the following example we create a database called &quot;my_db&quot;: </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>$con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;); </li></ul></ul><ul><ul><li>if (!$con) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>die('Could not connect: ' . mysql_error()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>if (mysql_query(&quot;CREATE DATABASE my_db&quot;,$con)) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>echo &quot;Database created&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>echo &quot;Error creating database: &quot; . mysql_error(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>mysql_close($con); </li></ul></ul><ul><ul><li>?> </li></ul></ul>
  61. 61. Create a Table <ul><li>Example </li></ul><ul><ul><li>$con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;); </li></ul></ul><ul><ul><li>if (!$con) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>die('Could not connect: ' . mysql_error()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// Create table in my_db database </li></ul></ul><ul><ul><li>mysql_select_db(&quot;my_db&quot;, $con); </li></ul></ul><ul><ul><li>$sql = &quot;CREATE TABLE person </li></ul></ul><ul><ul><li>( </li></ul></ul><ul><ul><li>FirstName varchar(15), </li></ul></ul><ul><ul><li>LastName varchar(15), </li></ul></ul><ul><ul><li>Age int </li></ul></ul><ul><ul><li>)&quot;; </li></ul></ul><ul><ul><li>mysql_query($sql,$con); </li></ul></ul><ul><ul><li>mysql_close($con); </li></ul></ul>
  62. 62. Insert new records into a database table <ul><li>Example </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>$con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;); </li></ul></ul><ul><ul><li>if (!$con) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>die('Could not connect: ' . mysql_error()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>mysql_select_db(&quot;my_db&quot;, $con); </li></ul></ul><ul><ul><li>mysql_query(&quot;INSERT INTO person (FirstName, LastName, Age) </li></ul></ul><ul><ul><li>VALUES ('Peter', 'Griffin', '35')&quot;); </li></ul></ul><ul><ul><li>mysql_query(&quot;INSERT INTO person (FirstName, LastName, Age) </li></ul></ul><ul><ul><li>VALUES ('Glenn', 'Quagmire', '33')&quot;); </li></ul></ul><ul><ul><li>mysql_close($con); </li></ul></ul><ul><ul><li>?> </li></ul></ul>
  63. 63. Insert Data From a Form Into a Database <ul><li>Example : insert.html </li></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li><form action=&quot;insert.php&quot; method=&quot;post&quot;> </li></ul></ul><ul><ul><li>Firstname: <input type=&quot;text&quot; name=&quot;firstname&quot; /> </li></ul></ul><ul><ul><li>Lastname: <input type=&quot;text&quot; name=&quot;lastname&quot; /> </li></ul></ul><ul><ul><li>Age: <input type=&quot;text&quot; name=&quot;age&quot; /> </li></ul></ul><ul><ul><li><input type=&quot;submit&quot; /> </li></ul></ul><ul><ul><li></form> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  64. 64. Insert Data From a Form Into a Database <ul><li>Example : insert.php </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>$con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;); </li></ul></ul><ul><ul><li>if (!$con) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>die('Could not connect: ' . mysql_error()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>mysql_select_db(&quot;my_db&quot;, $con); </li></ul></ul><ul><ul><li>$sql=&quot;INSERT INTO person (FirstName, LastName, Age) </li></ul></ul><ul><ul><li>VALUES </li></ul></ul><ul><ul><li>('$_POST[firstname]','$_POST[lastname]','$_POST[age]')&quot;; </li></ul></ul><ul><ul><li>if (!mysql_query($sql,$con)) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>die('Error: ' . mysql_error()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>echo &quot;1 record added&quot;; </li></ul></ul><ul><ul><li>mysql_close($con) </li></ul></ul><ul><ul><li>?> </li></ul></ul>

×