Forms, Functions etc.
<ul><li>Objects and Classes </li></ul><ul><li>Functions </li></ul><ul><li>Global Arrays </li></ul><ul><li>Passing Variable...
<ul><li>An object is a self-contained set of related variables and functions </li></ul><ul><li>Objects are defined using t...
<ul><li>Class variables are referenced from outside the class using the  ->  </li></ul><ul><ul><li>$myClass->var = 4 </li>...
<ul><li>You should have a constructor function which is called to create an instance of the class (an object) </li></ul><u...
<ul><li><?php </li></ul><ul><ul><ul><ul><li>class  student{ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>public $sID; //Sh...
<ul><li>Functions are a self-contained set of expressions and variables </li></ul><ul><li>They are used to make a set of p...
<ul><li><?php </li></ul><ul><li>function repeat($fname,$num=10) </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;<ol>&quot...
<ul><li><?php </li></ul><ul><li>function buildRows($array){ </li></ul><ul><li>$rows = '<tr><td bgcolor=&quot;blue&quot;>’ ...
<ul><li>PHP creates 6 global arrays that contain EGPCS (Environment, Get, Post, Cookies and Server) information and File i...
<ul><li>$_SERVER  –  Information about the server session and the HTTP connection with the client. </li></ul><ul><ul><li>e...
<ul><li>$ _FILES  –  References to all files received, most commonly from HTML forms, using the POST method. </li></ul><ul...
<ul><li>$GLOBALS  –  Contains all the variables that are of global scope </li></ul><ul><li>G lobal scope is a variable whi...
<ul><ul><li>$a = 40; </li></ul></ul><ul><ul><li>f unction add(){ </li></ul></ul><ul><ul><li>$a = 30; </li></ul></ul><ul><u...
<ul><li>Where does the information which is submitted in a form go? </li></ul><ul><li><html>  </li></ul><ul><li><body> </l...
<ul><li>The method used is post, so all variables sent are in the $_POST[] array </li></ul><ul><li><html>  </li></ul><ul><...
<ul><li>PHP creates a variable called within the SERVER global array called  $_SERVER[‘PHP_SELF’]  that contains the name ...
<ul><li><?php </li></ul><ul><li>if (!$_POST[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ </li></ul><ul><li>?>  <...
<ul><li><?php </li></ul><ul><li>if (!$_POST[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ </li></ul><ul><li>?>  <...
<ul><li><?php </li></ul><ul><li>if (!$_POST[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ </li></ul><ul><li>?>  <...
<ul><li><?php </li></ul><ul><li>if (!$_POST[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ </li></ul><ul><li>?>  <...
<ul><li><form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;> </li></ul><ul><li>Select the ...
<ul><li>Steven M. Schafer (2005),  HTML, CSS, JavaScript, Perl, and PHP Programmer's Reference , Hungry Minds Inc,U.S.  </...
Upcoming SlideShare
Loading in...5
×

Further Php

1,084

Published on

php

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

  • Be the first to like this

No Downloads
Views
Total Views
1,084
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
62
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Further Php"

  1. 1. Forms, Functions etc.
  2. 2. <ul><li>Objects and Classes </li></ul><ul><li>Functions </li></ul><ul><li>Global Arrays </li></ul><ul><li>Passing Variables </li></ul>
  3. 3. <ul><li>An object is a self-contained set of related variables and functions </li></ul><ul><li>Objects are defined using the class keyword </li></ul><ul><li>A class is the template for an object </li></ul><ul><li>Variables contained within a class are called class variables </li></ul><ul><li>Class variables may have different scopes </li></ul><ul><ul><li>public : May be accessed directly </li></ul></ul><ul><ul><li>private : May only be accessed within the class </li></ul></ul><ul><ul><li>static : Only initialised once </li></ul></ul><ul><ul><li>protected , global etc. </li></ul></ul>
  4. 4. <ul><li>Class variables are referenced from outside the class using the -> </li></ul><ul><ul><li>$myClass->var = 4 </li></ul></ul><ul><li>Class variables are referenced from inside the class using $this-> </li></ul><ul><ul><li>$this->var = 4 </li></ul></ul><ul><ul><li>$this must be used for private variables </li></ul></ul>
  5. 5. <ul><li>You should have a constructor function which is called to create an instance of the class (an object) </li></ul><ul><ul><li>public function __construct([any arguments]){} </li></ul></ul>
  6. 6. <ul><li><?php </li></ul><ul><ul><ul><ul><li>class student{ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>public $sID; //Show the id to everyone </li></ul></ul></ul></ul><ul><ul><ul><ul><li>private $sName; //Limit the name </li></ul></ul></ul></ul><ul><ul><ul><ul><li>public function __construct ($name, $id) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>{ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>$this->$sID = $id; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>$this->sName = $sName; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>public function printStudent() </li></ul></ul></ul></ul><ul><ul><ul><ul><li>{ echo “Student ID: $this->sID <br /> Student Name: $this->sName”;} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>$ student1 = new student(‘1234’, ‘Peter’) ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>$ student1 -> printStudent (); </li></ul></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul>
  7. 7. <ul><li>Functions are a self-contained set of expressions and variables </li></ul><ul><li>They are used to make a set of procedures repeatable </li></ul><ul><ul><li>function funcName($arg1, $arg2 [=default value]) </li></ul></ul><ul><li>A function can take arguments </li></ul><ul><li>These arguments can have default values </li></ul><ul><ul><li>Values which are assumed if no value is provided </li></ul></ul>
  8. 8. <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(”Jim Kai&quot;); </li></ul><ul><li>?> </li></ul>
  9. 9. <ul><li><?php </li></ul><ul><li>function buildRows($array){ </li></ul><ul><li>$rows = '<tr><td bgcolor=&quot;blue&quot;>’ </li></ul><ul><li>.implode('</td></tr><tr><td bgcolor=&quot;yellow&quot;>', $array) </li></ul><ul><li>.'</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’ 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><ul><li>Implode(glue, $array): Used to join array elements with a string </li></ul>
  10. 10. <ul><li>PHP creates 6 global arrays that contain EGPCS (Environment, Get, Post, Cookies and Server) information and File information </li></ul><ul><li>PHP also creates an array called $_REQUEST[] that contains the unsafe variables. </li></ul><ul><ul><li>It’s content is dependant on configuration, but normally it contians the $_GET[], $_POST[] and $_COOKIES[] array variables amalgomated into one array. </li></ul></ul>
  11. 11. <ul><li>$_SERVER – Information about the server session and the HTTP connection with the client. </li></ul><ul><ul><li>e.g. $_SERVER[‘ HTTPS ’] : Is a secure connection being used </li></ul></ul><ul><li>$_POST[] – All variables received as an inline posted data set, normally through using the POST method in an HTML form. </li></ul><ul><ul><li>e .g. $_POST[‘username’] </li></ul></ul><ul><li>$_GET[] – The values of any variables sent via the URL </li></ul><ul><ul><li>e .g. $_GET[‘username’] </li></ul></ul>
  12. 12. <ul><li>$ _FILES – References to all files received, most commonly from HTML forms, using the POST method. </li></ul><ul><li>$_ENV[] – Contains the values of any environment variables, such as the browser version </li></ul><ul><ul><li>e .g. $_ENV[‘HTTP_USER_AGENT’] </li></ul></ul><ul><li>$_COOKIES – Contains any cookies submitted as name value pairs </li></ul><ul><li>$_SESSION – If PHP is being used for session management, this array is to store any session variables that need to be stored on the server between calls from the client. </li></ul>
  13. 13. <ul><li>$GLOBALS – Contains all the variables that are of global scope </li></ul><ul><li>G lobal scope is a variable which is defined outside of any particular function </li></ul><ul><li>They may also be referenced using the global keyword </li></ul><ul><li>G lobal variables take precedence over locally defined variables </li></ul>
  14. 14. <ul><ul><li>$a = 40; </li></ul></ul><ul><ul><li>f unction add(){ </li></ul></ul><ul><ul><li>$a = 30; </li></ul></ul><ul><ul><li>$b = 20; </li></ul></ul><ul><ul><li>global $a; </li></ul></ul><ul><ul><li>return $a + $b; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>echo “Going to add. Answer to 30+20 = ”.add(); </li></ul></ul><ul><li>$GLOBALS[] and global may both be used to access global variables </li></ul><ul><li>$GLOBALS[] requires you assign the variable to a local variable </li></ul>Same as: $a = $GLOBALS[‘a’]
  15. 15. <ul><li>Where does the information which is submitted in a form go? </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>The information goes to the location specified by action=“” </li></ul>
  16. 16. <ul><li>The method used is post, so all variables sent are in the $_POST[] array </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>The output will be </li></ul><ul><li>Welcome James . </li></ul><ul><li>You are 10 years old. </li></ul>
  17. 17. <ul><li>PHP creates a variable called within the SERVER global array called $_SERVER[‘PHP_SELF’] that contains the name of the current script/page (relative to the doc root) </li></ul><ul><ul><li>This is useful in creating forms </li></ul></ul><ul><li>This allows one page to handle all the form creation and handling </li></ul><ul><li>May also make use of the isset() built-in PHP function </li></ul><ul><ul><li>Checks to see if a variable has been assigned a value </li></ul></ul>
  18. 18. <ul><li><?php </li></ul><ul><li>if (!$_POST[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ </li></ul><ul><li>?> </li></ul><ul><li><form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;> </li></ul><ul><li><p>Your surname: <input type=&quot;text&quot; name=&quot;surname&quot;></p> </li></ul><ul><li><p>Your address: <input type=&quot;text&quot; name=&quot;address&quot;></p> </li></ul><ul><li><input button type=&quot;submit&quot; value= &quot;Please send me the brochure.&quot;> </li></ul><ul><li></form> </li></ul><ul><li><?php </li></ul><ul><li>} </li></ul><ul><li>else{ </li></ul><ul><li>$sn = $_REQUEST['surname']; </li></ul><ul><li>echo &quot;<p>Thank you, $sn.</p>&quot;; </li></ul><ul><li>$addr = $_REQUEST['address']; </li></ul><ul><li>echo &quot;<p> We will write to you at $addr .</p>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  19. 19. <ul><li><?php </li></ul><ul><li>if (!$_POST[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ </li></ul><ul><li>?> </li></ul><ul><li><form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;> </li></ul><ul><li><p>Your surname: <input type=&quot;text&quot; name=&quot;surname&quot;></p> </li></ul><ul><li><p>Your address: <input type=&quot;text&quot; name=&quot;address&quot;></p> </li></ul><ul><li><input button type=&quot;submit&quot; value= &quot;Please send me the brochure.&quot;> </li></ul><ul><li></form> </li></ul><ul><li><?php </li></ul><ul><li>} </li></ul><ul><li>else{ </li></ul><ul><li>$sn = $_REQUEST['surname']; </li></ul><ul><li>echo &quot;<p>Thank you, $sn.</p>&quot;; </li></ul><ul><li>$addr = $_REQUEST['address']; </li></ul><ul><li>echo &quot;<p> We will write to you at $addr .</p>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  20. 20. <ul><li><?php </li></ul><ul><li>if (!$_POST[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ </li></ul><ul><li>?> </li></ul><ul><li><form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;> </li></ul><ul><li><p>Your surname: <input type=&quot;text&quot; name=&quot;surname&quot;></p> </li></ul><ul><li><p>Your address: <input type=&quot;text&quot; name=&quot;address&quot;></p> </li></ul><ul><li><input button type=&quot;submit&quot; value= &quot;Please send me the brochure.&quot;> </li></ul><ul><li></form> </li></ul><ul><li><?php </li></ul><ul><li>} </li></ul><ul><li>else{ </li></ul><ul><li>$sn = $_REQUEST['surname']; </li></ul><ul><li>echo &quot;<p>Thank you, $sn.</p>&quot;; </li></ul><ul><li>$addr = $_REQUEST['address']; </li></ul><ul><li>echo &quot;<p> We will write to you at $addr .</p>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  21. 21. <ul><li><?php </li></ul><ul><li>if (!$_POST[&quot;surname&quot;] or !$_POST[&quot;address&quot;]){ </li></ul><ul><li>?> </li></ul><ul><li><form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;> </li></ul><ul><li><p>Your surname: <input type=&quot;text&quot; name=&quot;surname&quot;></p> </li></ul><ul><li><p>Your address: <input type=&quot;text&quot; name=&quot;address&quot;></p> </li></ul><ul><li><input button type=&quot;submit&quot; value= &quot;Please send me the brochure.&quot;> </li></ul><ul><li></form> </li></ul><ul><li><?php </li></ul><ul><li>} </li></ul><ul><li>else{ </li></ul><ul><li>$sn = $_REQUEST['surname']; </li></ul><ul><li>echo &quot;<p>Thank you, $sn.</p>&quot;; </li></ul><ul><li>$addr = $_REQUEST['address']; </li></ul><ul><li>echo &quot;<p> We will write to you at $addr .</p>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  22. 22. <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>
  23. 23. <ul><li>Steven M. Schafer (2005), HTML, CSS, JavaScript, Perl, and PHP Programmer's Reference , Hungry Minds Inc,U.S. </li></ul><ul><li>Christopher Schmitt (2003), Designing CSS Web Pages, New Riders </li></ul><ul><li>Larry Ullman (2005), PHP and Mysql for Dynamic Web Sites, Peachpitt Press </li></ul><ul><li>The main site for all PHP information: </li></ul><ul><ul><li>http://php.net </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×