Slide1 l.jpg
Advertisement
This presentation is the property of its rightful owner.
1 / 78

en.wikipedia/wiki/Http PowerPoint PPT Presentation

http://en.wikipedia.org/wiki/Http Hypertext Transfer Protocol ( HTTP ) is a communications protocol for the transfer of information on the Internet . Its use for retrieving inter-linked text documents ( hypertext ) led to the establishment of the World Wide Web .

Download Presentation

en.wikipedia/wiki/Http

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Slide1 l.jpg

http://en.wikipedia.org/wiki/Http

Hypertext Transfer Protocol (HTTP) is a communications protocol

for the transfer of information on the Internet.

Its use for retrieving inter-linked text documents (hypertext)

led to the establishment of the World Wide Web.

HTTP development was coordinated by the World Wide Web Consortium

and the Internet Engineering Task Force (IETF),

culminating in the publication of a series of Request for Comments (RFCs),

most notably RFC 2616 (June 1999),

which defines HTTP/1.1, the version of HTTP in common use.


Slide2 l.jpg

HTTP is a request/response standard between a client and a server.

A client is the end-user, the server is the web site. The client making an HTTP request - using a web browser, spider, or other end-user tool - is referred to as the user agent.

The responding server - which stores or creates resources such as HTML files and images - is called the origin server.


Slide3 l.jpg

Typically, an HTTP client initiates a request.

It establishes a Transmission Control Protocol (TCP) connection to a particular port on a host (port 80 by default; see List of TCP and UDP port numbers).

An HTTP server listening on that port waits for the client to send a request message. Upon receiving the request, the server sends back a status line, such as "HTTP/1.1 200 OK", and a message of its own, the body of which is perhaps the requested file, an error message, or some other information.

Resources to be accessed by HTTP are identified using Uniform Resource Identifiers (URIs) (or, more specifically, Uniform Resource Locators (URLs)) using the http: or httpsURI schemes.


Slide4 l.jpg

http://en.wikipedia.org/wiki/Html


Slide5 l.jpg

HTML, an initialism of HyperText Markup Language,

is the predominant markup language for Web pages.

It provides a means to describe the structure of

text-based information in a document

— by denoting certain text as links, headings, paragraphs, lists, and so on —

and to supplement that text with interactive forms, embedded images,

and other objects.

HTML is written in the form of tags, surrounded by angle brackets.

HTML can also describe, to some degree, the appearance and semantics

of a document, and can include embedded scripting language code

(such as JavaScript) which can affect the behavior of Web browsers

and other HTML processors.

By convention, HTML format data files use a file extension .html or .htm.


Slide6 l.jpg

Following is from HTML tutorial at

http://www.w3schools.com/html/html_intro.asp


Slide7 l.jpg

<html>

<head>

<title>Title of page</title>

</head>

<body> This is my first homepage. <b>This text is bold</b>

</body>

</html>

Save the file as "mypage.htm". Start your Internet browser. Select "Open" (or "Open Page") in the File menu of your browser. A dialog box will appear. Select "Browse" (or "Choose File") and locate the HTML file you just created - "mypage.htm" - select it and click "Open". Now you should see an address in the dialog box, for example "C:\MyDocuments\mypage.htm". Click OK, and the browser will display the page.


Slide8 l.jpg

This is an HTML element:

<b>This text is bold</b>

The HTML element starts with a start tag: <b>The content of the HTML element is: This text is boldThe HTML element ends with an end tag: </b>

The purpose of the <b> tag is to define an HTML element

that should be displayed as bold.

This is also an HTML element:

<body> This is my first homepage.

<b>This text is bold</b>

</body>

This HTML element starts with the start tag <body>,

and ends with the end tag </body>.

The purpose of the <body> tag is to define the HTML

element that contains the body of the HTML document.


Slide9 l.jpg

We have just said that HTML tags are not case sensitive:

<B> means the same as <b>.

If you surf the Web, you will notice that plenty of web sites

use uppercase HTML tags in their source code.

We always use lowercase tags.

Why?

If you want to follow the latest web standards,

you should always use lowercase tags.

The World Wide Web Consortium (W3C) recommends

lowercase tags in their HTML 4 recommendation,

and XHTML (the next generation HTML) demands

lowercase tags.


Slide10 l.jpg

Experiment with html here:

http://www.w3schools.com/html/tryit.asp?filename=tryhtml_basic


Slide11 l.jpg

Paragraphs are defined with the <p> tag.

<p>This is a paragraph</p>

<p>This is another paragraph</p>

HTML automatically adds an extra blank line before and after a paragraph.

Don't Forget the Closing Tag

You might have noticed that paragraphs can be written without end tags </p>:

<p>This is a paragraph

<p>This is another paragraph

The example above will work in most browsers, but don't rely on it.

Future version of HTML will not allow you to skip ANY end tags.

Closing all HTML elements with an end tag is a

future-proof way of writing HTML.

It also makes the code easier to understand (read and browse)

when you mark both where an element starts and where it ends.


Slide12 l.jpg

Line Breaks

The <br> tag is used when you want to break a line,

but don't want to start a new paragraph.

The <br> tag forces a line break wherever you place it.

<p>This <br> is a para<br>graph with line breaks</p>

Try it yourself

The <br> tag is an empty tag.

It has no end tag like </br>,

since a closing tag doesn't make any sense.


Slide13 l.jpg

<br> or <br />

More and more often you will see the <br> tag written like this:

<br />

Because the <br> tag has no end tag (or closing tag),

it breaks one of the rules for future HTML

(the XML based XHTML),

namely that all elements must be closed.

Writing it like <br /> is a future proof way of closing

(or ending) the tag inside the opening tag,

accepted by both HTML and XML.


Slide14 l.jpg

Comments in HTML

The comment tag is used to insert a comment in the HTML source code.

A comment will be ignored by the browser.

You can use comments to explain your code,

which can help you when you edit the source code at a later date.

<!-- This is a comment -->

Note that you need an exclamation point after the opening bracket,

but not before the closing bracket.


Slide15 l.jpg

Recap on HTML Elements

Each HTML element has an element name (body, h1, p, br)

The start tag is the name surrounded by angle brackets:

<h1>

The end tag is a slash and the name surrounded by

angle brackets </h1>

The element content occurs between the start tag and

the end tag

Some HTML elements have no content

Some HTML elements have no end tag


Slide16 l.jpg

There is quite a bit more in this HTML

Tutorial, but we will not go through that now.


Slide17 l.jpg

http://en.wikipedia.org/wiki/Php

PHP is a computer scripting language.

Originally designed for producing dynamic web pages,

it has evolved to include a command line interface capability

and can be used in standalonegraphical applications.[2]

While PHP was originally created by Rasmus Lerdorf in 1995,

the main implementation of PHP is now produced by The PHP Group

and serves as the de facto standard for PHP as there is no formal specification.[3]

Released under the PHP License, the Free Software Foundation

considers it to be free software.[4]


Slide18 l.jpg

PHP is a widely used general-purpose scripting language

that is especially suited for web development

and can be embedded into HTML.

It generally runs on a web server, taking PHP code as its input

and creating web pages as output.

It can be deployed on most web servers

and on almost every operating system and platform free of charge.[5]


Slide19 l.jpg

PHP primarily acts as a filter,[31]

taking input from a file or stream

containing text and/or PHP instructions

and outputs another stream of data;

most commonly the output will be HTML.


Slide20 l.jpg

Originally designed to create dynamic web pages,

PHP's principal focus is server-side scripting,[35]

and it is similar to other server-side scripting languages

that provide dynamic content from a web server to a client,

such as Microsoft's ASP.NET system,

Sun Microsystems' JavaServer Pages,[36] and mod_perl.


Slide21 l.jpg

PHP has also attracted the development of many frameworks

that provide building blocks and a design structure to promote rapid application development (RAD). Some of these include CakePHP, PRADO, Symfony and Zend Framework, offering features similar to other web application frameworks.

The LAMP architecture has become popular in the web industry as a way of deploying web applications. PHP is commonly used as the P in this bundle alongside Linux, Apache and MySQL, although the P may also refer to Python or Perl.


Slide22 l.jpg

As of April 2007, over 20 million Internet domains were hosted on servers with PHP installed, and PHP was recorded as the most popular Apache module.[37] Significant websites are written in PHP including the user-facing portion of Facebook,[38][39] and Wikipedia (Mediawiki).[40]


Slide23 l.jpg

Following is from PHP tutorial:

http://www.htmlgoodies.com/beyond/php/article.php/3472431


Slide24 l.jpg

Start up Apache and PHP

http://localhost

Select phpinfo()

Find “document_root”


Slide25 l.jpg

<html><head><title>PHP Test</title></head><body><?php phpinfo(); ?></body></html>

Save as first.htm at document root.

http://localhost/first.htm

Save as first.php at document root.

http://localhost/first.php


Slide26 l.jpg

…each statement in your PHP code is ended with a semicolon. 

Leaving this semicolon off is a common syntax error

and when something isn't working the way you expect

it to should be one of the first things you check.

Whitespace (spaces, tabs and newlines) are ignored in the syntax of PHP. 

These three examples are completely equivalent:

<?php phpinfo(); ?>

<?php     phpinfo();     ?>

<?php phpinfo();?>


Slide27 l.jpg

As in all forms of programming, adding comments to you code is

a very useful idea. 

Make your code self-documenting and you will love yourself later

when you come back to if after having forgotten

why you did things a certain way!


Slide28 l.jpg

Here are some examples:

<?phpphpinfo();/*  This is a      multi-line comment following     the C language style of comments.*/?>

<?php  // this is a C++ style comment on the one code linephpinfo();?>

<?php  # this is a shell script style comment on the one code linephpinfo();?>


Slide29 l.jpg

We continue now by creating a simple order form.  The processing of forms like this is one of the more common things to be done with server side languages including PHP.  For our example we have chosen the component order form from the Acme Widget Company (of course!)  Here's the code for the entire form:


Slide30 l.jpg

<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Acme Widget Company</title></head><body style="font-family: Arial"><h1><br>Acme Widget Company</h1><p>Component Order Form</p>


Slide31 l.jpg

<FORM ACTION="processorder.php" method=post><table border=0><tr><td width=150>Item<br>&nbsp;</td><td width=15>Quantity<br>&nbsp;</td></tr><tr><td>Bases</td><td align="center"><input type="text" name="qtybases" size="3" maxlength="3"></td></tr><tr><td>Stems</td>


Slide32 l.jpg

<td align="center"><input type="text" name="qtystems" size="3" maxlength="3"></td></tr><tr><td>Tops</td><td align="center"><input type="text" name="qtytops" size="3" maxlength="3"></td></tr><tr><td colspan="2" align="center"><br><input type="submit" value="Enter Order"></td></tr></table></form></body></html>


Slide33 l.jpg

You can click here to see what this form looks like in your browser.

http://www.htmlgoodies.com/legacy/beyond/php/orderform.html

Now let's break it down.  As with our last example, most of the code you see is ordinary HTML. 


Slide34 l.jpg

Notice, however, that the ACTION= on the FORM statement points to a PHP file called processorder.php (the capitals we've used here are only to highlight to code we're talking about - it doesn't make any difference whether you use capitals or lowercase letters, except that the filename may be case sensitive if your site is hosted on a Unix/Linux system.).

When the user hits the "enter order" button, the file whose name (URL) appears in the ACTION statement will be loaded and the data the user typed into the form elements will be passed to it. 

The data will be identified to that file by the field names used on the form, here "qtybases", "qtystems"  and "qtytops".  For this reason it's a good idea to use easily recognized and meaningful names, such as ours are.


Slide35 l.jpg

There's really no PHP code in this form (which is why it's in a file named with a .html extension) so we'll move right along to the file that will process our order.


Slide36 l.jpg

Our order form sent its data to a file called processorder.php via the instruction:

ACTION="processorder.php" method=post

This is the content of that file:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Acme Widget Company</title></head><body style="font-family: Arial"><h1><br>Acme Widget Company</h1><p>&nbsp;</p>


Slide37 l.jpg

<?php

$qtybases = $_POST['qtybases'];$qtystems = $_POST['qtystems'];$qtytops = $_POST['qtytops'];?><p>Thank you!</p><p>Your order for:</p><?php

echo $qtybases.' bases<br>';echo $qtystems.' stems<br>';echo $qtytops.' tops<br>';?><p>was processed.</p></body></html>


Slide38 l.jpg

In here there's plenty of interesting PHP code to take a look at!  We'll start with that first piece of PHP code:$qtybases = $_POST['qtybases'];$qtystems = $_POST['qtystems'];$qtytops = $_POST['qtytops'];(To make life easier, I've left off the opening and closing tags when looking at the code itself & I'll continue like that throughout this series.)

Each of these statements is an "assignment" instruction. 

The equal sign in the middle designates it as such and tells PHP to "make the value of this Equal to the value of that." 

In this case we're making the value of one variable equal to the value of another variable (a "variable" is a data element whose value can change, as opposed to a "constant" which is a data element whose value can not change.)


Slide39 l.jpg

The variables on the left, $qtybases for example, are variable fields that are being dynamically defined by these statements. 

This means that no other statement was needed to create the variable $qtybases; it's being defined right here. 

Those variables are being set to the values of some "POST" variables. 

"POST" variables are those that are passed down from the preceding form by means of the POST method (remember the statement <FORM ACTION="processorder.php" method=post> in the form.) 

We'll get into more detail about these variables later; for now suffice it to say that this format, using the names that were used in the calling form, will pick up the variables being passed down.


Slide40 l.jpg

Then there's that group of echo statements. "Echo" works like a "print here" command. 

Remember that the PHP processor is going to interpret the PHP code in this page and then send the page down to the client browser. 

Whatever the echo prints out becomes part of the resulting page. 

Each of these echo statements prints out the value of a variable and a text string enclosed in quotes. 


Slide41 l.jpg

Look closely and you'll see a period between the two 

This tells PHP to concatenate the two strings (the value of the variable and the quoted string) together. 

Thus, if $qtybases has a value of 5 then the first echo statement will return this:

5 bases<br>

which, on the resulting web page, will show up as "5 bases" followed by a line break.

Try  it out on our example.

http://www.htmlgoodies.com/legacy/beyond/php/orderform.html


Slide42 l.jpg

Variables hold one or another "type" of information.  PHP is "loosely typed," meaning that the type of information to be held in a particular variable can change on the fly. 

Changing on the fly means that if you start with a variable holding numerical data and use it in calculations, for example, and then write an instruction to put text into it, PHP will then consider it to be a text variable.

Here are the main data types used in PHP:

Integer whole numbers

Double real numbers

String strings of characters (text, etc.)

Boolean true/false

Array a set or group of variables of the same type

Object class instances (more later!)


Slide43 l.jpg

An array needs a few words at this point. 

A simple example of an array would be a string array called $month. 

There could be twelve in the set and they could have the values January, February etc. 

To reference the first of the set we use an index value thus:

$month(1)

In our example this would refer to array element with a value of:

January


Slide44 l.jpg

The index could also be a variable, thus:

$monthnum = 2;

echo $month($monthnum);

Assuming the array had been defined with the values descried in our example, this would echo:

February


Slide45 l.jpg

You've probably heard of type casting, but maybe not the kind I'm thinking of here!  This has nothing to do with thespian arts - type casting in PHP means telling PHP to treat a variable of one type as if it was another (without actually changing its type.) 

It doesn't really matter right here why you'd want to do that - it'll become clearer later. 

For now, just take a look at this example:

$quantity = 0;$amount = (double)$quantity;

$quantity is set up as an integer with a value of zero. 

The next line treats $quantity as a double type and assigns its value to $amount. 

$amount is now a double with a value of zero and $quantity is still an integer with a value of zero.


Slide46 l.jpg

In PHP you can even use a variable as the name of a variable.  Take a look at this:

$varname = 'quantity';

$$varname = 5;

The second line uses the $varname variable in the name of the variable being assigned the value of 5. 

Consequently the variable $quantity is assigned the value 5. 

It does exactly the same thing as:

$quantity = 5;


Slide47 l.jpg

In PHP there are two types of expression for us to look at. One is something called a "regular expression", which is used in the manipulation of text.

We'll talk more about them later in the series.

The other is more mathematical in nature, expressing operations to be performed on values and variables.

Here's a simple example: c = a + b

Assuming a and b are numbers (remember data types?  -- the result would be different if they were strings -- more about that later) this expression would add a and b and put the sum into c (remember again, that the equals sign here makes this an assignment instruction.) 


Slide48 l.jpg

Arithmetic Operators:


Slide49 l.jpg

Comparison Operators:


Slide50 l.jpg

Logical Operators:


Slide51 l.jpg

Increment and Decrement Operators:


Slide52 l.jpg

The Concatenate Operator

I get in trouble all the time because I ask how this can be considered an "operator", but trust me, the world thinks it is!  The concatenate "operator" is a period (full stop to the English English!)  It simply says "join this to this", so that

$parta = "This is part one “

$partb = "of a sentence!“

print $parta.$partb

prints out "This is part one of a sentence!”


Slide53 l.jpg

Combined Operators:

There are some combination operators that you may see used (or may use!)  There are those who argue that these combination operators provide an advantage because they reduce typing and reduce the final size of the code.  In my humble opinion, however, they don't represent the way that most of us think of things, and are therefore somewhat confusing.  See what you think.

You remember from earlier examples that = is an assignment operator:

$a = 5

assigns the value 5 to $a.   This assignment can be combined with some other operators like this:

$a += 5

If $a already had a value of 5, then after this instruction it would be 10, as if the full instruction was:

$a = $a + 5

Valid combinations include:  +=  -=  *=  /=


Slide54 l.jpg

Precedence and Associativity.

Think about this operation:

$a = 7 - 4 + 1

If the minus happens before the plus, the result would be 4.  If the plus happens first, the result is 2.  Since predictability is essential, an order of precedence was established, along with something called associativity. 

The order of precedence establishes a "rank" for operators, where those with the highest rank will be evaluated first.  When there are several operators of the same rank, they will be processed from left to right if they are left associative, and right to left if they are right associative.  Some operators, such as the comparison operators, have neither left nor right associativity.


Slide55 l.jpg

The increment and decrement operators  ( ++ and -- ) are right associative, while the others I have discussed here are left associative.  The order of precedence, from highest to lowest (with those of equal precedence on the same line) is as follows:

++  --*  /  %+  -|&&| |andxoror


Slide56 l.jpg

You can override the order of precedence but wrapping part of an expression in parentheses.  The innermost parentheses are processed first, so that:

(7 - 4) + 1 will give a result of 47 - (4 + 1) will give a result of 2

2 + 3 * 3 will give a result of 112 + (3 * 3) will also give 11(2 + 3) * 3 will give a result of 15

1 + 3 + 3 * 2 will give a result of 101 + ((3 + 3) * 2) will give a result of 13(1 + (3 + 3)) * 2 will give a result of 14


Slide57 l.jpg

The If Statement

The basic decision making statement, the "if" statement says "if this is true, do this, else do that".  Its format is like this:

if ( condition ){ do these instructions }else{ do these instructions }

and here's an example:

if ($age < 21){ print "Minor"; }else{ print "Adult"; }


Slide58 l.jpg

It is also possible to add a condition to the "else" side by using "elseif" like this:

if ($temp > $idealtemp){ print "Add cold water" }elseif ($temp < $idealtemp){ print "Add hot water" }else{ print "Add more water" }


Slide59 l.jpg

The For loop

You can create a loop by using a "for" loop construct (as it is known.)  The "for" statement says "for a value of x starting here, until the value of x meets this condition, and changing the value of x by this amount each time you process the loop, process this code".  This could use a little explanation!  Basically, x is going to serve as a counter. 

We give x a start value, for example 1. 

Then we specify a condition to test before we decide to process the block of instructions in the loop, for example x < 11. 

Finally, we specify an amount by which to change x at the end of processing the block of instructions, for example, increment by 1. 

In this example, the code would be run ten times, with x having a value of 1 the first time through, 2 the second, etc. The last time through it would have a value of 10. 


Slide60 l.jpg

Here's its format:

for ( counter's initial value,  counter's condition test, counter's increment ){ do these instructions }

and here's an example

for ($count = 1; $count < 11; $count++ ){   $tot = 2 * $count;   print "2 times $count equals $tot ";}

This example prints out the two times table from two times one to two times ten.  It's worth taking special note of the fact that the test comes at the beginning of the loop -- that is, before the code block is run.  Hence the need for a test for "less than 11" in order to get the tenth iteration to run.


Slide61 l.jpg

The "While" loop

Another loop construct is the "While" loop construct.  The for loop uses a counter to control the number of iterations of its code block, but the while loop can use any kind of condition.   There is an inherent danger here!  What happens if the condition always remains true? 

The code never stops running -- that's what! 

This is known as an infinite loop and the a sure sign of a programmer error. 

PHP allows a system administrator to defend their system against this kind of abuse of processor time by providing a time-out mechanism.  PHP scripts are typically limited to 30 seconds of execution time, and will quit with an error at that point.  It is an important part of the debugging process to ensure that you don't wind up with any infinite loops!  That having been said.....


Slide62 l.jpg

A "while" statement says "while this condition is true, process this code" and its format is:

while ( condition ){ do these instructions }

Here's our two times table as a while loop:

$count = 1while ( $count < 11 ){   $tot = 2 * $count;   print "2 times $count equals $tot ";   $count = $count + 1;}


Slide63 l.jpg

There is a couple of things to take particular notice of in this example.  Firstly, we had to include a separate statement to initialize the count data element because it is not part of the construct as it would be in a for statement. 

Remember that the condition in a while statement can be any condition -- I have used a counter type of condition only to show the comparison of this construct to the for construct. 

Secondly, and for the same reason, there has to be a statement in the code block to increment $count (I could also have used   $count++   or   $count += 1  -- I like the format I used the best because it seems the clearest to me.)


Slide64 l.jpg

There is also a variant of the while loop that puts the test for the condition at the end, that is, after the code block has been processed. 

This, of course, means that the code block will always run at least once.  

This variant is the "do... while" loop and it says "do this code block, and while this condition is true, go back and do it again".  Its format is:

do{ these instructions}while ( condition );


Slide65 l.jpg

Here's our two times table as a do while loop:

$count = 1do{   $tot = 2 * $count;   print "2 times $count equals $tot ";   $count = $count + 1;}while ( $count < 11 )


Slide66 l.jpg

These options show you how you can create program execution loops in you PHP code.  Which one is the best to use depends on the particular type of execution requirement. 

You have to think a it about the flow of logic as you see it, and use the construct that most closely resembles what you need. 

Another thing to remember is that you can nest these constructs, by which I mean that the code block to be run by the construct could include another loop construct.  Any type of loop can be contained within any other type of loop.

When you include code within a loop like this it is a valuable thing to do to indent the code a couple of spaces like I have done in the examples above. 


Slide67 l.jpg

What is a Function?

In the last part of this tutorial series we looked at controlling logic flow in PHP. 

This included logic flowing in a linear fashion, but with loops of logic at various points and with decision making along the way. 

The overall flow was a straight line - the loops were processed where they occurred, and the logic continued along its line from the beginning of the program routine to its end. 

There are times, however, when a series of instructions has to be repeated from a variety of different points within the main routine, or where a series of instructions needs to be followed that doesn't really fit within the main routine. 


Slide68 l.jpg

Variable Scope and Lifetime

It's important to note that if you define a variable within a function, that variable is only available within that function; it cannot be referenced in another function or in the main body of your program code.  This is known as a variable's scope. 

The scope of a variable defined within a function is confined to that function -- in other words, it's local to that function.

If a function needs to use a variable that is defined in the main body of the program, it must reference it using the "global keyword, like this:

function AddingNumbers ( ){   global $sum = 2 + 2 }

$sum = 0Addnumbers ( )print "2 + 2 = ".$sum


Slide69 l.jpg

While the scope of a variable defined in a function is local to that function, a variable defined in the main body of code has a global scope. 

The "global" keyword tells PHP to look for a variable that has been defined outside the function.

There is also the idea of a lifetime for a variable.  A variable defined within a PHP program script exists only while that script is running. 

When the script ends, the variable ceases to exist.  This seems pretty obvious! 

Now apply it to a function:  a variable defined within a function exists only while that function is being processed; when the function ends, the variable ceases to exist.    Ooooh!  Aaaah- yes!!


Slide70 l.jpg

The usefulness of functions is greatly enhanced by the ability to pass information from the main body of code to the function and in the opposite direction, or between one function and another it is calling  Values that are sent to a function are called arguments.

Passing Argument to a Function

To define arguments that will be passed to a function, you provide a list of names for them between the parentheses in the function statement, like this:

function Myfunction ($arg1, $arg2)

and to pass values to the function as arguments by putting them between the parentheses following the function's name in the statement that calls it, like this:

$answer = Myfunction (3, 5)


Slide71 l.jpg

A function returns a result to the calling code by using the "return: keyword, like this:

return $result;

When the return statement is processed, the function terminates and the value of the variable specified is returned to the calling code. 

The return statement only returns one variable -- if you need to return more than one value, you'd have to define and return an array (we'll be getting into arrays a little later in this series.)

You could have more than one return statement in your function if, for example, you had conditional logic (such as "if" statements) within your function, but only one of them will ever be processed (since processing it terminates the function!)


Slide72 l.jpg

An Example

Here's an example of a couple of variables being passed to a function as arguments with a result returned:

<html>

<head>

<title>Function Arguments Example</title>

</head>

<body>

<h1>Showing a Volume</h1>

<br />

<?php

function CalcVol ($x, $y, $z)

{

$vol = $x * $y * $z;

return $vol;

}


Slide73 l.jpg

$long = 3;

$wide = 4;

$high = 5;

print  "The volume of this object is " . CalcVol($long, $wide, $high) . " volumetric units.";

?>

</body>

</html>

In this example you can see that a function called CalcVol is being defined, and that it receives three parameters, $x, $y & $z.  These are being used to specify the lengths of the three axes of a solid object, which the function multiplies together to determine the volume of the object.  The function then returns the volume to the calling code.

The function is being called from the middle of a print statement, which passes the three values contained in the variables $long, $wide & $high.  Thus the print statement will display the result of the calculation.


Slide74 l.jpg

Lots more of this tutorial continuing at:

http://www.htmlgoodies.com/beyond/php/article.php/3472511


Slide75 l.jpg

http://www.w3.org/TR/html401/interact/forms.html

http://www.w3.org/TR/html401/interact/forms.html#h-17.1


Slide76 l.jpg

17.13.3 Processing form data

When the user submits a form (e.g., by activating a submit button), the user agent processes it as follows.

Step one: Identify the successful controls

Step two: Build a form data set 

A form data set is a sequence of control-name/current-value pairs constructed from successful controls

Step three: Encode the form data set 

The form data set is then encoded according to the content type specified by the enctype attribute of the FORM element.


Slide77 l.jpg

Step four: Submit the encoded form data set 

Finally, the encoded data is sent to the processing agent designated by the action attribute using the protocol specified by the method attribute.

This specification does not specify all valid submission methods or content types that may be used with forms. However, HTML 4 user agents must support the established conventions in the following cases:

If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type. The user agent then traverses the link to this URI. In this scenario, form data are restricted to ASCII codes.

If the method is "post" and the action is an HTTP URI, the user agent conducts an HTTP "post" transaction using the value of the action attribute and a message created according to the content type specified by the enctype attribute.

For any other value of action or method, behavior is unspecified.

User agents should render the response from the HTTP "get" and "post" transactions.


Slide78 l.jpg

17.13 Form submission

The following sections explain how user agents

submit form data to form processing agents.

17.13.1 Form submission method

The method attribute of the FORM element specifies the

HTTP method used to send the form to the processing agent.

This attribute may take two values:

get: With the HTTP "get" method, the form data set is

appended to the URI specified by the action attribute

(with a question-mark ("?")

as separator) and this new URI is sent to the processing agent.

post: With the HTTP "post" method, the form data set is included

in the body of the form and sent to the processing agent.

The "get" method should be used when the form is idempotent

(i.e., causes no side-effects). Many database searches have no visible side-effects and make ideal applications for the "get" method.

If the service associated with the processing of a form causes side effects (for example, if the form modifies a database or subscription to a service), the "post" method should be used.


  • Login