Further Php
Upcoming SlideShare
Loading in...5
×
 

Further Php

on

  • 1,260 views

php

php

Statistics

Views

Total Views
1,260
Views on SlideShare
1,260
Embed Views
0

Actions

Likes
0
Downloads
58
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Further Php Further Php Presentation Transcript

  • Forms, Functions etc.
    • Objects and Classes
    • Functions
    • Global Arrays
    • Passing Variables
    • An object is a self-contained set of related variables and functions
    • Objects are defined using the class keyword
    • A class is the template for an object
    • Variables contained within a class are called class variables
    • Class variables may have different scopes
      • public : May be accessed directly
      • private : May only be accessed within the class
      • static : Only initialised once
      • protected , global etc.
    • Class variables are referenced from outside the class using the ->
      • $myClass->var = 4
    • Class variables are referenced from inside the class using $this->
      • $this->var = 4
      • $this must be used for private variables
    • You should have a constructor function which is called to create an instance of the class (an object)
      • public function __construct([any arguments]){}
    • <?php
          • class student{
          • public $sID; //Show the id to everyone
          • private $sName; //Limit the name
          • public function __construct ($name, $id)
          • {
          • $this->$sID = $id;
          • $this->sName = $sName;
          • }
          • public function printStudent()
          • { echo “Student ID: $this->sID <br /> Student Name: $this->sName”;}
          • }
          • $ student1 = new student(‘1234’, ‘Peter’) ;
          • $ student1 -> printStudent ();
        • ?>
    • Functions are a self-contained set of expressions and variables
    • They are used to make a set of procedures repeatable
      • function funcName($arg1, $arg2 [=default value])
    • A function can take arguments
    • These arguments can have default values
      • Values which are assumed if no value is provided
    • <?php
    • function repeat($fname,$num=10)
    • {
    • echo &quot;<ol>&quot;;
    • for ($i = 1; $i <= $num; $i++)
    • echo &quot;<li> $fname </li>&quot;;
    • echo &quot;</ol>&quot;;
    • }
    • echo &quot;My name is &quot;;
    • repeat(&quot;Kai Jim&quot;,5);
    • echo &quot;My name is &quot;;
    • repeat(”Jim Kai&quot;);
    • ?>
    • <?php
    • function buildRows($array){
    • $rows = '<tr><td bgcolor=&quot;blue&quot;>’
    • .implode('</td></tr><tr><td bgcolor=&quot;yellow&quot;>', $array)
    • .'</td></tr>';
    • return $rows;
    • }
    • function buildTable($rows){
    • $table = &quot;<table cellpadding='3’ border='2'> $rows </table>&quot;;
    • return $table;
    • }
    • $myarray = array('php tutorial','mysql tutorial','apache tutorial','java tutorial','xml tutorial');
    • $rows = buildRows($myarray);
    • $table = buildTable($rows);
    • echo $table;
    • ?>
    • Implode(glue, $array): Used to join array elements with a string
    • PHP creates 6 global arrays that contain EGPCS (Environment, Get, Post, Cookies and Server) information and File information
    • PHP also creates an array called $_REQUEST[] that contains the unsafe variables.
      • It’s content is dependant on configuration, but normally it contians the $_GET[], $_POST[] and $_COOKIES[] array variables amalgomated into one array.
    • $_SERVER – Information about the server session and the HTTP connection with the client.
      • e.g. $_SERVER[‘ HTTPS ’] : Is a secure connection being used
    • $_POST[] – All variables received as an inline posted data set, normally through using the POST method in an HTML form.
      • e .g. $_POST[‘username’]
    • $_GET[] – The values of any variables sent via the URL
      • e .g. $_GET[‘username’]
    • $ _FILES – References to all files received, most commonly from HTML forms, using the POST method.
    • $_ENV[] – Contains the values of any environment variables, such as the browser version
      • e .g. $_ENV[‘HTTP_USER_AGENT’]
    • $_COOKIES – Contains any cookies submitted as name value pairs
    • $_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.
    • $GLOBALS – Contains all the variables that are of global scope
    • G lobal scope is a variable which is defined outside of any particular function
    • They may also be referenced using the global keyword
    • G lobal variables take precedence over locally defined variables
      • $a = 40;
      • f unction add(){
      • $a = 30;
      • $b = 20;
      • global $a;
      • return $a + $b;
      • }
      • echo “Going to add. Answer to 30+20 = ”.add();
    • $GLOBALS[] and global may both be used to access global variables
    • $GLOBALS[] requires you assign the variable to a local variable
    Same as: $a = $GLOBALS[‘a’]
    • Where does the information which is submitted in a form go?
    • <html>
    • <body>
    • <form action=&quot;welcome.php&quot; method=&quot;post&quot;>
    • Name: <input type=&quot;text&quot; name=&quot;name&quot; />
    • Age: <input type=&quot;text&quot; name=&quot;age&quot; />
    • <input type=&quot;submit&quot; />
    • </form>
    • </body>
    • </html>
    • The information goes to the location specified by action=“”
    • The method used is post, so all variables sent are in the $_POST[] array
    • <html>
    • <body>
    • Welcome <?php echo $_POST[&quot;name&quot;]; ?>.<br />
    • You are <?php echo $_POST[&quot;age&quot;]; ?> years old.
    • </body>
    • </html>
    • The output will be
    • Welcome James .
    • You are 10 years old.
    • 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)
      • This is useful in creating forms
    • This allows one page to handle all the form creation and handling
    • May also make use of the isset() built-in PHP function
      • Checks to see if a variable has been assigned a value
    • <?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;;
    • }
    • ?>
    • <?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;;
    • }
    • ?>
    • <?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;;
    • }
    • ?>
    • <?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;;
    • }
    • ?>
    • <form method=&quot;post&quot; action=&quot;<?php echo $_SERVER['PHP_SELF'];?>&quot;>
    • Select the programming languages you can use<br>
    • <input name=&quot;language[]&quot; type=&quot;checkbox&quot; id=&quot;language[]&quot; value=&quot;C++&quot;> C++ <br>
    • <input name=&quot;language[]&quot; type=&quot;checkbox&quot; id=&quot;language[]&quot; value=&quot;Java&quot;> Java <br>
    • <input name=&quot;language[]&quot; type=&quot;checkbox&quot; id=&quot;language[]&quot; value=&quot;PHP&quot;> PHP <br>
    • <input name=&quot;language[]&quot; type=&quot;checkbox&quot; id=&quot;language[]&quot; value=&quot;ASP&quot;> ASP <br>
    • <input name=&quot;language[]&quot; type=&quot;checkbox&quot; id=&quot;language[]&quot; value=&quot;Delphi&quot;> Delphi <br>
    • <input name=&quot;lang&quot; button type=&quot;submit&quot; value=&quot;Send it!&quot;>
    • </form>
    • <?php
    • if(isset($_POST['lang'])){
    • $language = $_POST['language'];
    • $n = count($language);
    • $i = 0;
    • echo &quot;The languages you selected are &quot; . &quot;<ol>&quot;;
    • while ($i < $n){
    • echo &quot;<li>$language[$i]</li> &quot;;
    • $i++;
    • }
    • echo &quot;</ol>&quot;;
    • }
    • ?>
    • Steven M. Schafer (2005), HTML, CSS, JavaScript, Perl, and PHP Programmer's Reference , Hungry Minds Inc,U.S.
    • Christopher Schmitt (2003), Designing CSS Web Pages, New Riders
    • Larry Ullman (2005), PHP and Mysql for Dynamic Web Sites, Peachpitt Press
    • The main site for all PHP information:
      • http://php.net