Вы находитесь на странице: 1из 10

C

o
n
L
e
n
L
s


1

Beginners PHP

> PHP Section One - An Introduction to PHP

1. What is PHP and Why do I need it?
2. What you need to get started
3. Installing and testing Easy PHP
4. Troubleshooting

> PHP Two - Getting Started With Variables

1. What is a Variable?
2. Putting text into variables
3. Variables - some practice
4. More variable practice
5. Joining direct text and variable data
6. Adding up in PHP
7. Subtraction
8. Multiplication
9. Division
10. Floating point numbers

> PHP Three - Conditional Logic

1. II Statements
2. Using II Statements
3. iI ... else
4. iI ... else iI
5. Comparison Operators
6. NOT Equal To
7. Less Than and Greater Than
8. What these mean: , ~
9. The Switch Statement
10. Logical Operators
11. Boolean Values
12. Operator Precedence - a List

> PHP Four - Working with HTML Forms

1. The HTML Form
2. The Method Attribute
3. The Post Attribute
4. The Action Attribute
5. The Submit button
6. Getting values Irom a Text Box
7. Checking iI the Submit button was clicked
8. More on the ACTION attribute
9. Keep data the user entered
10. PHP and Radio Buttons
11. PHP and Checkboxes



C
o
n
L
e
n
L
s


2

> PHP Five - Programming Loops

1. For Loops
2. A Times Table Programme
3. The Code Ior the PHP Times Table
4. While Loops
5. Do ... While loops
6. The break statement

> PHP Six - Arrays in PHP

1. What is an Array?
2. Setting up an Array in PHP
3. Getting at the values stored in Arrays
4. Arrays - Using Text as Keys
5. Arrays and For Each
6. Sorting Array values
7. Random Keys Irom an Array
8. The count Iunction
9. Some Array Scripts

> PHP Seven - String Manipulation

1. Changing Case
2. Trimming White Space
3. ShuIIle characters
4. Finding String Positions with strpos
5. Splitting a line oI text
6. Joining text into a single line
7. PHP and Escaping
8. String Iunction list

> PHP Eight - Create your own Functions

1. An Introduction to Functions
2. Variable scope and Iunctions
3. Functions and Arguments
4. A Function to check Ior blank text boxes
5. Getting values out oI Iunctions
6. By ReI, By Val
7. PHP Server Variables
8. HTTP Header() Function
9. The INCLUDE( ) Function

> PHP Nine - Security Issues

1. Security Issues And Form Elements
2. htmlspecialchars( )
3. striptags( )




C
o
n
L
e
n
L
s


3

> PHP Ten - Working With Files In PHP
1. An Introduction to Working With Files In PHP
2. Opening a Iile with readIile( )
3. Opening a Iile with Iopen( )
4. Options Ior Iopen( )
5. Writing to Iiles
6. Working with CSV Iiles
7. Reading a text Iile into an array
8. File Locations

> PHP Eleven - Date and Time Functions in PHP

1. The date( ) Iunction
2. Using the date( ) Iunction
3. The getdate( ) Function

> PHP Twelve - PHP and MySQL

1. What you need Ior these Tutorials
2. Creating a database using phpMyAdmin
3. Setting up Fields in database tables
4. Adding records to a MySQL Table

> PHP Thirteen - Manipulate a MySQL Database

1. Access a MySQL database with PHP code
2. Reading records Irom a MySQL database
3. Adding records to a MySQL database
4. Magic Quotes
5. SQL injection
6. Create a Table using SQL
7. Updating a record in a table
8. Deleting a record in a table
9. Using WHERE to limit the data returned

> Walkthrough One - PHP User Authentication

1. PHP User Authentication - Introduction
2. The login page
3. Checking iI the user is logged on
4. Log Out
5. Register a new user
6. Other considerations

> Walkthrough Two - Build your own PHP Survey/Poll Application

1. Build your own Survey Application - Intro
2.The Database Ior the Survey
3. Setting a question the code
4. The Code Ior the Survey
5. Add the Vote to the Database
6. Viewing the Results oI the Survey


C
o
n
L
e
n
L
s


4


> Walkthrough Three - How to Build an Online Forum: the database

1. Introduction to the Forum
2. Setting up a Database Ior a bigger project
3. The Forum Sections Table
4. The Members Table
5. The Post Tables
6. The Reply Tables

> Walkthrough Four - The Code for the Forum

1. Code Strategies and Objectives
2. The IorumTest.php page
3. The pageThread.php page
4. 2D Arrays and PHP
5. How many replies are there Ior each post
6. Find out which member posted
7. Print the table out
8. The pageReply.php Code
9. The Reply Form
10. The results.php page
11. Posting a Topic on the Forum
12. The resultsP.php page
13. Final Project

Source:

LLp//wwwomeandlearncouk/pp/ppLml



C

a
p
L
e
r

2


1

PHP Tutorials - What is a Variable?
A variable is just a storage area. You put things into your storage areas (variables) so that you
can use and manipulate them in your programmes. Things you'll want to store are numbers
and text.
II you're ok with the idea oI variables, then you can move on. II not, think oI them like this.
Suppose you want to catalogue your clothing collection. You enlist two people to help you, a
man and a woman. These two people are going to be your storage areas. They are going to
hold things Ior you, while you tally up what you own. The man and the woman, then, are
variables.
You count how many coats you have, and then give these to the man. You count how many
shoes you have, and give these to the woman. UnIortunately, you have a bad memory. The
question is, which one oI your people (variables) holds the coats and which one holds the
shoes? To help you remember, you can give your people names! You could call them
something like this:
mr_coats
mrs_shoes
But it's entirely up to you what names you give your people (variables). II you like, they
could be called this:
man_coats
woman_shoes
Or
HimCoats
HerShoes
But because your memory is bad, it's best to give them names that help you remember what it
is they are holding Ior you. (There are some things your people balk at being called. You
can't begin their names with an underscore (), or a number. But most other characters are
Iine.)
OK, so your people (variables) now have name. But it's no good just giving them a name.
They are going to be doing some work Ior you, so you need to tell them what they will be
doing. The man is going to be holding the coats. But we can speciIy how many coats he will
be holding. II you have ten coats to give him, then you do the "telling" like this:
mr_coats 10
So, the variable name comes Iirst, then an equals sign. AIter the equals sign, you tell your
variable what it will be doing. Holding the number 10, in our case. (The equals sign, by the
way, is not really an equals sign. It's called an assignment operator. But don't worry about it,
at this stage. Just remember that you need the equals sign to store things in your variables.)


C

a
p
L
e
r

2


2

However, you're learning PHP, so there's something missing. Two things, actually. First, your
people (variables) need a dollar sign at the beginning (people are like that). So it would be
this:
mr_coats 10
II you miss the dollar sign out, then your people will reIuse to work! But the other thing
missing is something really picky and Iussy - a semi-colon. Lines oI code in PHP need a
semi-colon at the end:
mr_coats 10;
II you get any parse errors when you try to run your code, the Iirst thing to check is iI you've
missed the semi-colon oII the end. It's very easy to do, and can be Irustrating. The next thing
to check is iI you've missed out a dollar sign. But back to our people (variables).
So the man is holding ten coats. We can do the same thing with the other person (variable):
mrs_shoes 25;
So, $mrsshoes is holding a value oI 25. II we then wanted to add up how many items oI
clothes we have so Iar, we could set up a new variable (Note the dollar sign at the begining oI
the new variable):
total_clothes
We can then add up the coats and the shoes. You add up in PHP like this:
total_clothes mr_coats + mrs_shoes;
Remember, $mrcoats is holding a value oI 10, and $mrsshoes is holding a value oI 25. II
you use a plus sign, PHP thinks you want to add up. So it will work out the total Ior you. The
answer will then get stored in our new variable, the one we've called $totalclothes. You can
also add up like this:
total_clothes 10 + 35;
Again, PHP will see the plus sign and add the two together Ior you. OI course, you can add
up more than two items:
total_clothes 10 + 35 + 7 + 38 + 1250;
But the idea is the same - PHP will see plus signs and then add things up. The answer is then
stored in your variable name, the one to the leIt oI the equals sign.
In the next part, we'll take a look at how to put text into variables.



C

a
p
L
e
r

2


3

PHP Tutorials - Putting Text into Variables
In the previous section, you saw how to put numbers into variables. But you can also put text
into your variables. Suppose you want to know something about the coats you own. Are they
Winter coats? Jackets? Summer coats? You decide to catalogue this, as well. You can put
direct text into your variables. You do it in a similar way to storing numbers:
coats1 "Winter Coats";
Again, our variable name starts with a dollar sign ($). We've then given it the name coats1.
The equals sign Iollows the variable name. AIter the equals sign, however, we have direct
text - Winter Coats. But notice the double quotation marks around our text. II you don't
surround your direct text with quotation marks, then you'll get errors. You can, however, use
single quotes instead oI double quotes. So you can do this:
coats1 'Winter Coats';
But you can't do this:
coats1 'Winter Coats";
In the above line, we've started with a single quote and ended with a double quote. This will
get you an error.
We can store other text in the same way:
coats2 "1ackets";
coats3 "Summer Coats";
The direct text will then get stored in the variable to the leIt oI the equals sign.
So, to recap, variables are storage areas. You use these storage areas to manipulate things like
text and numbers. You'll be using variables a lot, and on the next Iew pages you'll see how
they work in practice.
PHP Tutorials: Variables - Some Practice
In the previous section, you saw what variables are: storage areas to hold things like numbers
and text. You tell PHP to remember these values because you want to do something with
them. In this section, you'll get some practice using variables. OII we go.

Testing variables witb PHP
First, we'll take a look at how to display what's in your variables. We're going to be viewing
our results on a web page. So see iI you can get this script working Iirst, because it's the one
we'll be building on. Using a text editor like Notepad, or your PHP soItware, type the
Iollowing. (You can copy and paste it, iI you preIer. But you learn more by typing it out
yourselI - it doesn't really sink in unless you're making mistakes!)


C

a
p
L
e
r

2


4

html>
head>
title>Variables - Some Practice/title>
/head>
body>
?php print("It Worked!"); ?>
/body>
/html>
When you've Iinished typing it all, save the page as variables.php. Then Run the script.
Remember: when you're saving your work, save it to the WWW Iolder, as explained here. To
run the page, start your browser up and type this in the address bar:
http://localhost/variables.php
II you've created a Iolder inside the www Iolder, then the address to type in your browser
would be something like:
http://localhost/FolderName/variables.php
II you were successIul, you should have seen the text "It worked!" displayed in your browser.
II so, Congratulations! You have a working server up and running! (II you're using
Wampserver, you should see an icon in the bottom right oI your screen. Click the icon and
select Start All Services Irom the menu.)
@e P scrlpL ls only one llne long
?php print("It Worked!"); ?>
The rest oI the script is just plain HTML code. Let's examine the PHP in more detail.
We've put the PHP in the BODY section oI an HTML page. Scripts can also, and oIten do, go
between the HEAD section oI an HTML page. You can also write your script without any
HTML. But beIore a browser can recognise your script, it needs some help. You have to tell
it what kind oI script it is. Browsers recognise PHP by looking Ior this punctuation (called
syntax):
?php ?>
So you need a leIt angle bracket ( ) then a question mark ( ? ). AIter the question mark, type
PHP (in upper or lowercase). AIter your script has Iinished, type another question mark.
Finally, you need a right angle bracket ( ~ ). You can put as much space as you like between
the opening and closing syntax.
To display things on the page, we've used print( ). What you want the browser to print goes
between the round brackets. II you're printing direct text, then you need the quotation marks
(single or double quotes). To print what's inside oI a variable, just type the variable name
(including the dollar). Finally, the line oI code ends as normal - with a semi-colon (;).
Another way to display things on the page is to use an alternative to print() echo( ).


C

a
p
L
e
r

2


3

Now let's adapt the basic page so that we can set up some variables. We'll try some text Iirst.
Keep the HTML as it is, but change your PHP Irom this:
?php print("It Worked!"); ?>
To this:
?php
print("It Worked!");
?>
OK, it's not much oI a change! But spreading your code out over more than one line makes it
easier to see what you're doing. Now, it's clear that there's only one line oI code - Print. So
add this second line to your code (the one in red):

?php
test_String "It Worked!";
print("It Worked!");
?>
We've set up a variable called test_String. AIter the equals sign, the text "It Worked!" has
been added. The line is then ended with a semi-colon. Don't run your script yet. Change the
Print line to this:
print(test_String);
Then add some comments ...

?php
//--------------TESTING VARIABLES------------
test_String "It Worked!";
print(test_String);
?>
Comments in PHP are Ior your beneIit. They help you remember what the code is supposed
to do. A comment can be added by typing two slashes. This tells PHP to ignore the rest oI the
line. AIter the two slashes, you can type anything you like. Another way to add a comment, is
like this:
?php
/` --------------TESTING VARIABLES------------
Use this type of comment if you want to spill over to more than one line.
Notice how the comment begin and end.
`/


C

a
p
L
e
r

2


6

test_String "It Worked!";
print(test_String);
?>
Whichever method you choose, make sure you add comment to your code: they really do
help. Especially iI you have to send your code to someone else!
But you can now run the script above, and test it out.
How did you get on? You should have seen that exactly the same text got printed to the page.
And you might be thinking - what's the big deal? Well, what you just did was to pass some
text to a variable, and then have PHP print the contents oI the variable. It's a big step: your
coding career has now begun!
Exercise
Change the text "It Worked!" to anything you like. Then run the script again. Try typing
some numbers in between your double quotes, instead oI text.
Exercise
Change the double quotes to single quotes. Did it have any eIIect? Put a single quote at the
beginning oI your text, and a double quote at the end. What happens when you run the code?
Exercise
Delete the dollar sign Irom the variable name. Then run your code. What error did you get?
Put the dollar sign back, but now delete the semi-colon. Run your code again? What error did
you get, this time? It's well worth remembering these errors - you'll see them a lot when
you're starting out! II you see them in Iuture, you'll be better able to correct your errors.

Now that you're up and running, we'll do some more variable work in the next section.

Вам также может понравиться