23
Forms, Functions etc.

Further Php

Embed Size (px)

DESCRIPTION

php

Citation preview

Page 1: Further Php

Forms, Functions etc.

Page 2: Further Php

Objects and Classes Functions Global Arrays Passing Variables

Page 3: Further Php

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.

Page 4: Further Php

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

Page 5: Further Php

You should have a constructor function which is called to create an instance of the class (an object)•public function __construct([any arguments]){}

Page 6: Further Php

<?phpclass student{

public $sID; //Show the id to everyoneprivate $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();

?>

Page 7: Further Php

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

Page 8: Further Php

<?php

function repeat($fname,$num=10) { echo "<ol>"; for ($i = 1; $i <= $num; $i++) echo "<li> $fname </li>"; echo "</ol>"; } echo "My name is ";

repeat("Kai Jim",5);

echo "My name is ";repeat(”Jim Kai");

?>

Page 9: Further Php

<?php

function buildRows($array){

$rows = '<tr><td bgcolor="blue">’.implode('</td></tr><tr><td bgcolor="yellow">', $array) .'</td></tr>';

return $rows;}

function buildTable($rows){

$table = "<table cellpadding='3’ border='2'> $rows </table>";

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

Page 10: Further Php

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.

Page 11: Further Php

$_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’]

Page 12: Further Php

$_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.

Page 13: Further Php

$GLOBALS – Contains all the variables that are of global scope

Global scope is a variable which is defined outside of any particular function

They may also be referenced using the global keyword

Global variables take precedence over locally defined variables

Page 14: Further Php

$a = 40;

function 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’]

Page 15: Further Php

Where does the information which is submitted in a form go?

<html> <body>

<form action="welcome.php" method="post"> Name: <input type="text"

name="name" /> Age: <input type="text" name="age" /> <input type="submit" />

</form></body></html>

The information goes to the location specified by action=“”

Page 16: Further Php

The method used is post, so all variables sent are in the $_POST[] array

<html> <body>

Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old.

</body> </html>

The output will be

Welcome James.You are 10 years old.

Page 17: Further Php

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

Page 18: Further Php

<?phpif (!$_POST["surname"] or !$_POST["address"]){?> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

<p>Your surname: <input type="text" name="surname"></p><p>Your address: <input type="text" name="address"></p><input button type="submit" value= "Please send me the brochure.">

</form><?php } else{

$sn = $_REQUEST['surname'];echo "<p>Thank you, $sn.</p>";$addr = $_REQUEST['address'];

echo "<p> We will write to you at $addr .</p>";} ?>

Page 19: Further Php

<?phpif (!$_POST["surname"] or !$_POST["address"]){?> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

<p>Your surname: <input type="text" name="surname"></p><p>Your address: <input type="text" name="address"></p><input button type="submit" value= "Please send me the brochure.">

</form><?php } else{

$sn = $_REQUEST['surname'];echo "<p>Thank you, $sn.</p>";$addr = $_REQUEST['address'];

echo "<p> We will write to you at $addr .</p>";} ?>

Page 20: Further Php

<?phpif (!$_POST["surname"] or !$_POST["address"]){?> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

<p>Your surname: <input type="text" name="surname"></p><p>Your address: <input type="text" name="address"></p><input button type="submit" value= "Please send me the brochure.">

</form><?php } else{

$sn = $_REQUEST['surname'];echo "<p>Thank you, $sn.</p>";$addr = $_REQUEST['address'];

echo "<p> We will write to you at $addr .</p>";} ?>

Page 21: Further Php

<?phpif (!$_POST["surname"] or !$_POST["address"]){?> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

<p>Your surname: <input type="text" name="surname"></p><p>Your address: <input type="text" name="address"></p><input button type="submit" value= "Please send me the brochure.">

</form><?php } else{

$sn = $_REQUEST['surname'];echo "<p>Thank you, $sn.</p>";$addr = $_REQUEST['address'];

echo "<p> We will write to you at $addr .</p>";} ?>

Page 22: Further Php

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> Select the programming languages you can use<br> <input name="language[]" type="checkbox" id="language[]" value="C++"> C++

<br> <input name="language[]" type="checkbox" id="language[]" value="Java"> Java <br> <input name="language[]" type="checkbox" id="language[]" value="PHP"> PHP <br> <input name="language[]" type="checkbox" id="language[]" value="ASP"> ASP <br> <input name="language[]" type="checkbox" id="language[]" value="Delphi"> Delphi

<br> <input name="lang" button type="submit" value="Send it!"></form>

<?phpif(isset($_POST['lang'])){

$language = $_POST['language'];$n = count($language);$i = 0;echo "The languages you selected are " . "<ol>";while ($i < $n){

echo "<li>$language[$i]</li> ";$i++;

}echo "</ol>";

}?>

Page 23: Further Php

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