Embed
Email

php tutorial.pdf

Document Sample
php tutorial.pdf
Description

tips and trick

Shared by: san toso
Categories
Stats
views:
16
posted:
2/1/2012
language:
pages:
24
PHP Basics

● We will look at the language more formally later.

● For now

–become familiar with the programming model

–get familiar with server-side programming

–get used to handling form submission









PHP Basics 1

PHP: Hypertext Preprocessor



Some References:





www.php.net



www.w3schools.com



http://www.oreillynet.com/pub/ct/29





PHP Basics 2

A PHP Program

● contained in an HTML document.

● All code is found inside:

tags.

● conditional HTML

–you can have PHP control what HTML actually

makes it to the output document.



foo is true!



foo is false!

PHP Basics 3

Web Server and PHP

● A client sends a request to a server, perhaps:

GET /myprog.php HTTP/1.1

● The server must be configured to recognize that

the request should be handled by PHP.

● PHP reads the document myprog.php and

produces some output (which is typically

HTML).

– all normal HTML tags are output without

modification.

PHP Basics 4

Our first PHP program?





I am a PHP program





PHP can handle HTML













PHP Basics 5

A better example – try this





I am a PHP program



















PHP Basics

Server-side programming

● This is not like JavaScript

– the browser does not understand PHP.

● You have to use a web server that supports

php.

– php preprocesses a file that contains HTML and

code, and generates pure HTML (could actually be

anything that the browser can understand).

– If you “view source” in the browser, you can't tell the

page was produced by php.



PHP Basics 7

The Language in one slide

● Similar syntax to C/C++/Javascript

– statements end with ';'

– if, while, for, ... all just like we are used to.

– assignment statements are the same.

● Variables are untyped (like Javascript)

– can create a new variable by assigning a value.

– variable names start with '$'

● arrays are different (associative arrays), but

easy to get used to.

PHP Basics 8

Generating HTML

● You can use functions echo , print and/or

printf to generate HTML:

Dynamic H3 tag");

print("a paragraph produced with the print

function");

printf("printf %s, works as well\n",

"(from C)");

?>





PHP Basics 9

Jump right in!

Here are some lines

";

for ($i=0;$i";

}

echo ""; string concatenation operator!



?>





PHP Basics 10

First Exercise

i 2i

----------------------------------

● Create a PHP script that produces 1 2

an HTML table with the first 10 2 4

3 8

powers of two. 4 16

● You need to use the pow() 5 32

6 64

function: 7 128

8 256

– pow(x,y) = xy 9 512

10 1024









PHP Basics 11

Non-obvious solution?





i

2i

















PHP Basics 12

PHP style

● some PHP programmers think:

– "I have an HTML document and I can add little bits

of PHP code in special places"

● some others think:

– "I have a PHP document in which I can add a few

HTML tags".



● Use whatever style seems most comfortable to

you.

PHP Basics 13

PHP and forms

● Recall that an HTML form submission looks

something like this (GET method):

GET /somefile?x=32&name=Joe+Smith

● Typically a PHP script wants to get at the

values the user typed into the form.

– often the form itself came from a php script.









PHP Basics 14

Form example





First Name:

Last Name:















PHP Basics 15

Receiving the submission

● You can put the form in a file named "form.php",

and set the action to also be "form.php".

– The php file will receive the submission itself

– You could also leave off the action attribute



● Unless we add some php code, all that will

happen when the form is submitted is that we

get a new copy of the same form.





PHP Basics 16

Form Fields and PHP

● PHP takes care of extracting the individual form

field names and values from the query.

– also does urldecoding for us.

● A global variable named $_REQUEST holds all

the form field names and values.

– this variable is an associative array – the keys

(indicies) are the form field names.









PHP Basics 17

Getting the values

● To get the value the user submitted for the field

named "first":

$_REQUEST['first']





● To get the value the user submitted for the field

named "last":

$_REQUEST['last']







PHP Basics 18

Adding some PHP to the form

● We could simply print out the values entered

(as HTML):

First name is ";

echo $_REQUEST['first'] . "";





echo "Last name is ";

echo $_REQUEST['last'] . "";

?>



PHP Basics 19

Or do it like this

First name is







Last name is











PHP Basics 20

Make a php form handler



First Name:

Last Name:





First name is ";

echo $_REQUEST['first'] . "";



echo "Last name is ";

echo $_REQUEST['last'] . "";

?>



PHP Basics 21

Looking for Joe Smith

● We can easily turn this into a primitive login

system.

– we only allow Joe Smith to login

– If the name is not Joe Smith, we send back the form

along with a rude message.

● A real login system would not have the valid

login names (passwords) hard-coded in the

program

– probably coming from a database.



PHP Basics 22

Login handling form



Welcome back joe;

} else {

?>

You are not the correct person.

Try again



First Name:

Last Name:



PHP Basics 23



Exercise

● Create a php script with a form where the user

enters a number between 1 and 10.

● If they guess correctly, tell them!

● If they guess wrong – send the form back.



● Play with your php program directly (skipping

the form) by constructing URLs manually.





PHP Basics 24



Related docs
Other docs by san toso
7 secreet facebook.pdf
Views: 9  |  Downloads: 0
Internet problem.pdf
Views: 12  |  Downloads: 0
how to easy money.pdf
Views: 9  |  Downloads: 0
DNS.pdf
Views: 2  |  Downloads: 0
dns malware.pdf
Views: 11  |  Downloads: 0
php tutorial.pdf
Views: 16  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!