PHP Tutorials

1,867 views
1,798 views

Published on

PHP Tutorials - The main data on PHP

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,867
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
214
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

PHP Tutorials

  1. 1. PHP Basic Basic PHP Syntax PHP Arrays PHP If...Else Statements PHP For Loops PHP Forms and User Input PHP Functions PHP introduction PHP Operators PHP - Variables PHP While Loops
  2. 2. Basic PHP Syntax <ul><ul><li>PHP code is executed on the server , and the plain HTML result is sent to the browser. A PHP scripting block can be placed anywhere in the document. It always starts with <?php and ends with ?>. </li></ul></ul><ul><ul><li>The example of PHP code below sends the text “Conkurent LLC” to the browser: </li></ul></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li>  <body> </li></ul></ul><ul><ul><li>  <?php   echo &quot; Conkurent LLC&quot;;   ?>   </li></ul></ul><ul><ul><li></body>   </li></ul></ul><ul><ul><li></html>   </li></ul></ul><ul><ul><li>To make a single-line comment, use //. To make a large comment block, use /* and */. See the example below:   </li></ul></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li>  <body> </li></ul></ul><ul><ul><li>  <?php   //This is a comment   /*   This is   a comment   block   */   ?> </li></ul></ul><ul><ul><li>  </body>   </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  3. 3. PHP Arrays <ul><ul><li>An array is a special variable that stores one or more values in a single variable. </li></ul></ul><ul><ul><li>If you have a list of items, storing them in single variables could look like this: </li></ul></ul><ul><ul><li>$item1=&quot;item-name1&quot;; </li></ul></ul><ul><ul><li>$item2=&quot; item-name2&quot;; </li></ul></ul><ul><ul><li>$item3=&quot; item-name3&quot;; </li></ul></ul><ul><ul><li>An array can hold all your variable values under a single name. And you can access the values by referring to the array name. Each element in the array has its own index so that it can be easily accessed. </li></ul></ul><ul><ul><li>In PHP, there are three kind of arrays: </li></ul></ul><ul><ul><ul><li>Numeric array - An array with a numeric index </li></ul></ul></ul><ul><ul><ul><li>Associative array - An array where each ID key is associated with a value </li></ul></ul></ul><ul><ul><ul><li>Multidimensional array - An array containing one or more arrays </li></ul></ul></ul>
  4. 4. PHP If…Else Statements <ul><ul><li>Conditional statements are used to perform different actions based on different conditions. Use the if....else statement to execute some code if a condition is true and another code if a condition is false . </li></ul></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>if (condition) code </li></ul></ul><ul><ul><li>to be executed if condition is true ; </li></ul></ul><ul><ul><li>else code to be executed if condition is false ; </li></ul></ul>
  5. 5. <ul><ul><li>The following example will output &quot;Get ready for the week!&quot; if the current day is Monday, otherwise it will output &quot;Have a good day!&quot;: </li></ul></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li><?php $d=date(&quot;D&quot;); </li></ul></ul><ul><ul><li>if ($d==&quot;Mon&quot;) echo &quot; Get ready for the week!&quot;; </li></ul></ul><ul><ul><li>else echo &quot;Have a good day!&quot;; ?> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  6. 6. <ul><ul><li>Use the if....elseif...else statement to select one of several blocks of code to be executed. </li></ul></ul><ul><ul><li>Syntax </li></ul></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>
  7. 7. PHP For Loops <ul><ul><li>The for loop is simply a while loop with a bit more code added to it. The common tasks that are covered by a for loop are: </li></ul></ul><ul><ul><ul><li>Set a counter variable to some initial value. </li></ul></ul></ul><ul><ul><ul><li>Check to see if the conditional statement is true. </li></ul></ul></ul><ul><ul><ul><li>Execute the code within the loop. </li></ul></ul></ul><ul><ul><ul><li>Increment a counter at the end of each iteration through the loop. </li></ul></ul></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>for (init; condition; increment) </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>Parameters: </li></ul></ul><ul><ul><ul><li>init : Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop) </li></ul></ul></ul><ul><ul><ul><li>condition : Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. </li></ul></ul></ul><ul><ul><ul><li>increment : Mostly used to increment a counter (but can be any code to be executed at the end of the loop) </li></ul></ul></ul>
  8. 8. PHP Forms and User Input <ul><ul><li>The example below contains an HTML form with two input fields and a submit button: </li></ul></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;fname&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>
  9. 9. <ul><ul><li>When a user fills out the form above and clicks on the submit button, the form data is sent to a PHP file, called &quot;welcome.php&quot;, &quot;welcome.php&quot; looks like this: </li></ul></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li>Welcome <?php echo $_POST[&quot;fname&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><ul><ul><li>Output could be something like this: Welcome John! </li></ul></ul><ul><ul><li>You are 28 years old. </li></ul></ul><ul><ul><li>Notice that any form element in an HTML page will automatically be available to your PHP scripts. </li></ul></ul>
  10. 10. PHP Functions <ul><ul><li>In PHP, there are more than 700 built-in functions . </li></ul></ul><ul><ul><li>This chapter shows how to create your own functions . A function will be executed by a call to the function. You may call a function from anywhere within a page. </li></ul></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>function functionName() </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>
  11. 11. <ul><ul><li>To add more functionality to a function, you can add parameters. A parameter is just like a variable. </li></ul></ul><ul><ul><li>Parameters are specified after the function name, inside the parentheses. </li></ul></ul><ul><ul><li>To let a function return a value, use the return statement (see the example below): </li></ul></ul><ul><ul><li>  <html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li><? </li></ul></ul><ul><ul><li>php function add($x,$y) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>$total=$x+$y; return $total; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>echo &quot;6 + 9 = &quot; . add(6,9); </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul><ul><ul><li>Output: 6 + 9 = 15 </li></ul></ul>
  12. 12. <ul><ul><li>Arithmetic Operators </li></ul></ul><ul><ul><li>OperatorDescriptionExampleResult +Additionx=2 x+24-Subtractionx=2 5-x3*Multiplicationx=4 x*520/Division15/5 5/23 2.5%Modulus (division remainder)5%2 10%8 10%21 2 0++Incrementx=5 x++x=6--Decrementx=5 x--x=4 </li></ul></ul>

×