Php 3 1

1,634 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,634
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
21
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Php 3 1

  1. 1. overview <ul><li>Installing WAMP or MAMP </li></ul><ul><ul><li>WampServer is a Windows (or Mac) web development environment </li></ul></ul><ul><li>Adding files and previewing in local host </li></ul><ul><li>Creating a testing server </li></ul><ul><li>Creating a new php file </li></ul><ul><ul><li>PHP is a powerful server-side scripting language for creating dynamic and interactive websites </li></ul></ul><ul><ul><li>http://www.w3schools.com/php/default.asp </li></ul></ul><ul><li>Viewing a php file </li></ul>
  2. 2. Installing wamp/mamp <ul><li>Install MAMP for MAC </li></ul><ul><ul><li>http:// www.mamp.info/en/index.php </li></ul></ul><ul><li>Install WAMP for pc </li></ul><ul><ul><li>http:// www.wampserver.com /en/ </li></ul></ul>
  3. 3. Set your www directory <ul><li>Install your WWW directory in a directory that you are going to use for creating websites. </li></ul><ul><li>This will allow the server to run virtually on your local machine </li></ul><ul><li>To start/stop server manually: </li></ul><ul><ul><li>Goto “Control Panel” – “Administrative Tools” – “Services” – “wampapache” start/stop </li></ul></ul>
  4. 4. Set up Dreamweaver <ul><li>Set up Dreamweaver Site with these settings for Testing Server </li></ul>
  5. 5. Some PHP syntax <ul><li>A PHP scripting block always starts with <?php and ends with ?> </li></ul><ul><ul><li>A PHP scripting block can be placed anywhere in the document. A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. </li></ul></ul><ul><li>Each code line in PHP must end with a semicolon ; </li></ul><ul><li>Use // to make a single-line comment or /* and */ to make a large comment block </li></ul><ul><li>All variables in PHP start with a $ sign symbol </li></ul>
  6. 6. Creating a php file <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>My First PHP Page</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><?php </li></ul><ul><li>$txt = &quot;Hello World!&quot;; </li></ul><ul><li>echo &quot;$txt, <p>How Are You Today?</p>&quot;; </li></ul><ul><li>?> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  7. 7. Arrays (mistakes on purpose!) <ul><li><?php $numbers = array(1, 2, 3, 4, 5, 6); $age = array(&quot;mom&quot; => 45, &quot;pop&quot; => 50, &quot;bro&quot; => 5); $mixed = array(&quot;hello&quot; => &quot;World&quot;, 2 => &quot;It's two&quot;; echo &quot;numbers[4] = {$numbers[4]} <br>&quot;; echo &quot;My mom's age is {$age['mom']} <br>&quot;; echo &quot;mixed['hello'] = {$mixed['hello']} <br>&quot;; echo &quot;mixed[2] = {$mixed[2'}&quot;; </li></ul><ul><li>?> </li></ul>
  8. 8. Strings <ul><li><?php </li></ul><ul><li>$quote1 = &quot;Never insult Dumbledore &quot; . &quot;in front of me!&quot;; </li></ul><ul><li>$quote2 = &quot;Nami, you are my nakama!&quot;; </li></ul><ul><li>echo $quote1 . &quot;<br>&quot;; </li></ul><ul><li>echo $quote2; </li></ul><ul><li>?> </li></ul>
  9. 9. Control structures <ul><li><?php </li></ul><ul><li>$number = 1; </li></ul><ul><li>while ($number < 10) </li></ul><ul><li>{ </li></ul><ul><li>echo $number . '<br>'; </li></ul><ul><li>$number += 1; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  10. 10. Control structures <ul><li><table width=&quot;200&quot; border=&quot;0&quot; cellspacing=&quot;1&quot; cellpadding=&quot;2&quot;> </li></ul><ul><li><tr> </li></ul><ul><li><td bgcolor=&quot;#CCCCFF&quot;>Alternating row colors</td> </li></ul><ul><li></tr> </li></ul><ul><li><?php </li></ul><ul><li>for ($i = 1; $i <= 10; $i++) { </li></ul><ul><li>if ($i % 2) { </li></ul><ul><li>$color = '#FFFFCC'; </li></ul><ul><li>} else { </li></ul><ul><li>$color = '#CCCCCC'; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li><tr> </li></ul><ul><li><td bgcolor=&quot;<?php echo $color; ?>&quot;><?php echo $i; ?></td> </li></ul><ul><li></tr> </li></ul><ul><li><?php </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li></table> </li></ul>
  11. 11. functions <ul><li><?php </li></ul><ul><li>function repeat($fname,$num=10) </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;<ol>&quot;; </li></ul><ul><li>for ($i = 1; $i <= $num; $i++) </li></ul><ul><li>echo &quot;<li> $fname </li>&quot;; </li></ul><ul><li>echo &quot;</ol>&quot;; </li></ul><ul><li>} </li></ul><ul><li>echo &quot;My name is &quot;; </li></ul><ul><li>repeat(&quot;Kai Jim&quot;,5); </li></ul><ul><li>echo &quot;My name is &quot;; </li></ul><ul><li>repeat(&quot;Kai Jim&quot;); </li></ul><ul><li>?> </li></ul>
  12. 12. functions <ul><li><?php </li></ul><ul><li>function buildRows($array){ </li></ul><ul><li>$rows = '<tr><td bgcolor=&quot;blue&quot;>' . implode('</td></tr><tr><td bgcolor=&quot;yellow&quot;>', $array) . '</td></tr>'; </li></ul><ul><li>return $rows; </li></ul><ul><li>} </li></ul><ul><li>function buildTable($rows){ </li></ul><ul><li>$table = &quot;<table cellpadding='3' cellspacing='1'  border='2'> $rows </table>&quot;; </li></ul><ul><li>return $table; </li></ul><ul><li>} </li></ul><ul><li>$myarray = array('php tutorial','mysql tutorial','apache tutorial','java tutorial','xml tutorial'); </li></ul><ul><li>$rows = buildRows($myarray); </li></ul><ul><li>$table = buildTable($rows); </li></ul><ul><li>echo $table; </li></ul><ul><li>?> </li></ul>
  13. 13. forms <ul><li>HTML file </li></ul><ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><form action=&quot;welcome.php&quot; method=&quot;post&quot;> </li></ul><ul><li>Name: <input type=&quot;text&quot; name=&quot;name&quot; /> </li></ul><ul><li>Age: <input type=&quot;text&quot; name=&quot;age&quot; /> </li></ul><ul><li><input type=&quot;submit&quot; /> </li></ul><ul><li></form> </li></ul><ul><li></body> </li></ul><ul><li></html </li></ul><ul><li>above form sent to PHP file “welcome.php” </li></ul><ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li>Welcome <?php echo $_POST[&quot;name&quot;]; ?>.<br /> </li></ul><ul><li>You are <?php echo $_POST[&quot;age&quot;]; ?> years old. </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li>sample output… </li></ul><ul><li>Welcome John. </li></ul><ul><li>You are 28 years old. </li></ul>
  14. 14. forms <ul><li><form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;> </li></ul><ul><li>Name : <input name=&quot;username&quot; type=&quot;text&quot;><br> </li></ul><ul><li>Password : <input name=&quot;password&quot; type=&quot;password&quot;><br> </li></ul><ul><li><input name=&quot;send&quot; type=&quot;submit&quot; value=&quot;Send Me!&quot;> </li></ul><ul><li></form> </li></ul>
  15. 15. forms <ul><li><?php </li></ul><ul><li>if(isset($_POST[&quot;send&quot;])){ </li></ul><ul><li>echo &quot;Accessing username using POST :&quot; . $_POST['username'] .&quot; <br>&quot;; </li></ul><ul><li>echo &quot;Accessing username using REQUEST : &quot;. $_REQUEST['username'] . &quot;<br>&quot;; </li></ul><ul><li>//$password = $_POST['password']; </li></ul><ul><li>$password = $_REQUEST['password']; </li></ul><ul><li>echo &quot;Password is $password&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  16. 16. forms <ul><li><form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;> </li></ul><ul><li>Select the programming languages you can use<br> </li></ul><ul><li><input name=&quot;language[]&quot; type=&quot;checkbox&quot; id=&quot;language[]&quot; value=&quot;C++&quot;> C++ <br> </li></ul><ul><li><input name=&quot;language[]&quot; type=&quot;checkbox&quot; id=&quot;language[]&quot; value=&quot;Java&quot;> Java <br> </li></ul><ul><li><input name=&quot;language[]&quot; type=&quot;checkbox&quot; id=&quot;language[]&quot; value=&quot;PHP&quot;> PHP <br> </li></ul><ul><li><input name=&quot;language[]&quot; type=&quot;checkbox&quot; id=&quot;language[]&quot; value=&quot;ASP&quot;> ASP <br> </li></ul><ul><li><input name=&quot;language[]&quot; type=&quot;checkbox&quot; id=&quot;language[]&quot; value=&quot;Delphi&quot;> Delphi <br> </li></ul><ul><li><input name=&quot;lang&quot; button type=&quot;submit&quot; value=&quot;Send it!&quot;> </li></ul><ul><li></form> </li></ul><ul><li><?php </li></ul><ul><li>if(isset($_POST['lang'])){ </li></ul><ul><li>$language = $_POST['language']; </li></ul><ul><li>$n = count($language); </li></ul><ul><li>$i = 0; </li></ul><ul><li>echo &quot;The languages you selected are &quot; . &quot;<ol>&quot;; </li></ul><ul><li>while ($i < $n){ </li></ul><ul><li>echo &quot;<li>$language[$i]</li> &quot;; </li></ul><ul><li>$i++; </li></ul><ul><li>} </li></ul><ul><li>echo &quot;</ol>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  17. 17. <?php if (! $_POST[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ ?> <form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;> <p>Your surname: <input type=&quot;text&quot; name=&quot;surname&quot;></p> <p>Your address: <input type=&quot;text&quot; name=&quot;address&quot;></p> <input button type=&quot;submit&quot; value= &quot;Please send me the brochure.&quot;> </form> <?php } else{ $sn = $_REQUEST['surname']; echo &quot;<p>Thank you, $sn.</p>&quot;; $addr = $_REQUEST['address']; echo &quot;<p> We will write to you at $addr .</p>&quot;; } ?> forms

×