Você está na página 1de 17

ISBN 978-0-557-04160-2


  


     

  
    

  !  "


  #$%&  '
 ()#&%%$% % *%!  +
 $&$ !  ,
" $-!.

  


 /  


 &- 0)1#(#% $ 
  $& % 
 0$% %& 
"  -$(  "
' (  )%) '
  $%1$()#&
+ 1)$( +
, ( # %)#0(1%) .
. )) 

 


 

 /  


 (%( %  
 &0%  

 



   ,


 # %$%1& ,
 )2 #%$&$ %)(( # %()#&% 
 # %$0)1#) &$  

   

"   "


"  ) "



  

Chapter

GETTING STARTED
WITH HTML
1 Getting Started With HTML

1.1 Basic Q and A’s:


What is HTML?
HTML (Hyper Text Markup Language) is a computer language devised to
allow website creation.
How does it Work?
HTML consists of a series of short codes (known as tags) typed into a
text-file by the site author. The text file is then saved as .html file, and
viewed through a browser like Internet Explorer. The browser reads the
text and translates it into visible form. You can use anything from notepad
to powerful graphical editor to create web pages.

"

  

What does these tags do?


These tags separate normal text from HTML code. In short, tags are
words between <> for example, <b> is a tag. These simple tags apply
formatting to the text, look at the code below:

<b>This is bold text</b> and this is not

When the above code is saved as .html file, and opened in your browser
then it will display:

This is bold text and this is not


Tags are not case sensitive, for example use of <B> and <b> will not
make any difference.
Is there anything HTML can’t do?
Yes, HTML can only make static pages (it can provide information to the
surfer but can not take information from the visitor). For example, if you
want the visitor to write comments about your web page, then you need to
have a server sided language to process this request.

1.2 Learning HTML


This part of the chapter will provide enough information about HTML
language to you to be able to write PHP programs.

An HTML web page consists of two parts, the HEAD part and the BODY
part. HEAD part comes before BODY part.
In the HEAD part, you define how the content in the body part of the web
page will be displayed. HEAD part contains the title of the web page, and
formatting of the text, which will be displayed on the body part of the web
page.
All HTML pages begin with <HTML> and end with </HTML>
HEAD part begins with <HEAD> and ends with </HEAD>

'

  

BODY part begins with <BODY> and ends with </BODY>


Making a basic Web page:
<HTML>
<HEAD> </HEAD>
<BODY> </BODY>
</HTML>

Adding title of web page:

<HTML>
<HEAD>
<TITLE>This is my First Web page</TITLE>
</HEAD>
<BODY>
</BODY>
</HTML>

Above code will display a blank web page with the title
“This is my First Web page”
Now, lets come to the BODY part. The body part as discussed above
contains the content of the web page.
To display some text in a web page, just add the text in between the body
tag.
<HTML>
<HEAD>
<TITLE>This is my First Web page</TITLE>
</HEAD>
<BODY> My first Web page
</BODY>
</HTML>



  

The above code will not only display the title “This is my First Web page”
but will also display the text “My first Web page” on the webpage.

1.3 Formatting the text:


Now we will do some formatting to the webpage:
To make the text look bold, add the text in between <b></b> tags.
For Example:
<HTML>
<HEAD>
<TITLE>This is my First Web page</TITLE>
</HEAD>
<BODY> <b>My first Web page</b>
</BODY>
</HTML>

Similarly, to make the text look italic, use <i></i> instead of <b></b>
You can also display the text both bold and italic by using <b><i></i></b>
similarly to underline the text use tag <u></u>
For Example:
<HTML>
<HEAD>
<TITLE>This is my First Web page</TITLE>
</HEAD>
<BODY> <b><i>My first Web page</i></b>
</BODY>
</HTML>

To make the text size larger or smaller, use the <font> tag.
<font size=”4” color=”#FFCC00”>Some text</font>

+

  

Chapter

PHP Basics
2 PHP BASICS
In this chapter you will be able to get know how of PHP language, how it
works and how you can make php scripts easily!

2.1 Basic Q & A’s


What is PHP?

PHP, which stands for "Hypertext Preprocessor", is a server-side, HTML


embedded scripting language used to create dynamic Web pages. Much
of its syntax is borrowed from C, Java and Perl with some unique features
thrown in. The goal of the language is to allow Web developers to write
dynamically generated pages quickly.



  

How does PHP Work?

In an HTML page, PHP code is enclosed within special PHP tags. When
a visitor opens the page, the server processes the PHP code and then
sends the output (not the PHP code itself) to the visitor's browser. It
means that, unlike JavaScript, you don't have to worry that someone can
steal your PHP script.

What are the advantages of PHP over other Server Sided


Languages?

PHP offers excellent connectivity to many databases including MySQL,


Informix, Oracle, Sybase, Solid, PostgreSQL, and Generic ODBC. The
popular PHP-MySQL combination (both are open-source products) is
available on almost every UNIX host. Being web-oriented, PHP also
contains all the functions to do things on the Internet - connecting to
remote servers, checking email via POP3 or IMAP, url encoding, setting
cookies, redirecting, etc.

What do PHP Code look like?

PHP is a rather simple language. Much of its syntax is borrowed from C


except for dealing with the types of variables. You don't need to think of
the types of variables at all - you just work with their values, not their
types. And you don't have to declare variables before you use them.

Things to remember:



  

1. An HTML web page containing PHP code should be save as .php


not as .html
2. A php code starts with <?php and ends with ?>
3. Each instruction in PHP code must end with semicolon (;)

2.2 Make your first PHP Web page


Here is a sample:
<HTML>
<HEAD>
<TITLE>My first PHP Script</TITLE>
</HEAD>
<BODY>
<?php
echo “The text is written with PHP”;
?>
</BODY>
</HTML>

Save the above code in blank file as webpage.php and run this file in your
browser, you will see the following output:
This text is written with PHP
As you might have noticed that the php code starts with <?php and ends
with ?>
To output we use echo just like in C language.

2.3 Sending Mail with PHP


PHP can be used to send emails, for this purpose, PHP mail() function is
used. Many web servers have this feature enabled.
To send email you must have a subject, a message, recipient and sender.
Let us provide the value of each of the variables:



  

Unlike javascript, the time displayed is the time on servers PC clock.

2.5 Checking File size


You can check the size of any file within the directory or in any
subdirectory.
Lets say we want to get the size of the file, which exists in the same
directory, lets say its name is index.html, then:
<?php
$filesize=filesize("index.html");
echo “$filesize”;
?>

The above function filesize() displays the size of file in bytes, you can
convert it into Kilobytes by dividing the output by 1024.
<?php
$filesize=filesize("index.html");
$filesizeinkb=$filesize/1024;
echo “$filesizeinkb”;
?>

To round off the value to two decimal places just use the round()
function.
<?php
$filesize=filesize("index.html");
$filesizeinkb=$filesize/1024;
$rounded_value=round($filesizeinkb,2);
echo “$rounded_value”;
?>

In the round function, there are two parameters:

"

  

round (Parameter1,Parameter2);
Parameter 1 is the numeric value that we want to round off and
Parameter 2 is the number of decimal places up to which the number will
be rounded off.

2.6 PHP if and else conditions


PHP has support for conditional sentences. For example, if you would like
to run different output upon different conditions then you can use if and
else condition.
In the following code we will display “Pass” if the value of a variable is
greater than or equal to 60, and “Fail” if the output is less than 60.

<?php
//Give the value of variable
$value=”75”;
if($value>=”60”){
echo “Pass”;
}else{
echo “Fail”;
}
?>

You can use elseif condition if there are more than two possibilities.
For Example in the code below, we will display “Positive” when variable is
greater than 60 and “Neutral” when variable is equal to 60 and “Negative”
when variable is less than 60, so,
<?php
//Give the value of variable
$value=”60”;
if($value>”60”){
echo “Positive”;
}elseif($value==”60”){

'

  

echo “Neutral”;
}else{
echo “Negative”;
}
?>

2.7 Sending data using Forms


In the above examples we have declared the values within the PHP code,
what if user wants to input the value? We use forms to let user input the
data.
A form code is an HTML tag which begins with <form> and ends with
</form>
First we create a form, which asks the user a value, and then we output
the user whether it is positive, neutral or negative:
<HTML>
<HEAD>
<TITLE>PHP FORM</TITLE>
</HEAD>
<BODY>
<?php
if($action==”validate”){
if($variab >”60”){
echo “Positive”;
}elseif($variab ==”60”){
echo “Neutral”;
}else{
echo “Negative”;
}
}
?>
<form method=post action=”?action=validate”>
Please input the value: <input type=text name=variab> <input type=submit
value=”Check”>



  

2.10 Loop
Loops are very common in any program. Loop is a repetitive task
assigned to script. Two most common types of loop are “for” and “while”.

For Loop
Here is a sample of a “for” loop:
<?php
for($i=1;$i<=5;$i++){
echo “This is $i line<BR>”;
}
?>

Output of above code will be:

This is 1 line
This is 2 line
This is 3 line
This is 4 line
This is 5 line

Same text is print five times only the line number changes each time.
For loop consists of three parameters each separated by “;” semicolon.
Parameter 1 $i=1 is executed only once.
Parameter 2 $i<=5 is the condition which must be meet to go inside the
loop.
In the above code $i=1, $i is a variable to which I have assigned a value
of 1. The script then checks whether $i is less than or equal to 5, if the
condition is true, then script goes inside the loop. The code inside the
loop is:



  

echo “This is $i line<BR>”;

Since the value of $i is 1, so the script prints:


This is 1 line.
Now, the script goes to the third parameter of for loop, $i++
It means, whatever the value of $i is, add one more to it. So now, the
value of $i is 2. It the goes to parameter number 2, since the value of $i is
still less than five, it again goes inside the loop and prints the line. It
continues until value of $i becomes greater than 5 and the loop ends.

While Loop
While loop is similar to for loop, but it only has one parameter.
Look at the code below:
<?php
$i=1;
while($i<=5){
echo “This is line $i<BR>”;
$i++;
}
?>

You can see that initial value of $i is defined outside the loop. Then while
loops checks for the value of $i, if it is less than or equal to 5 then it goes
inside the loop. Inside the loop, the value of $i is increased by 1, so the
loop continues until the value of $i becomes greater than 5.

If you do not increase the value of $i inside the loop, then loop will
continue endlessly, and can cause your system to crash.



  

Chapter

Using Database
3 USING DATABASE

3.1 Basic Q & A’s


What is a Database?

A database is simply a method by which you can store information. Lets


say you want to store information of your employees name and address,
you can use database for this purpose, and stored values can be viewed
using PHP.
What is MySQL?
MySQL is an open source Relational Database Management System that
uses Structured Query Language. Information is stored in "Tables" which



  

can be thought of as the equivalent of Excel spreadsheets. A single


MySQL database can contain many tables at once and store thousands
of individual records. It's fast, reliable and flexible.
What is a Flat File Database?
A Flat File database is a simple text file used to store information, and is
most often used when you do not have access to a MySQL database, as
they require no server addons other than PHP. They are best for smaller
amounts of data than a MySQL database is capable of, and have fewer
handling functions, but are very handy for things like counters and guest
books.

3.2 Flat File Databases


Lets start by creating a simple raw visit counter which will count the number of
times a page has been opened.
<?php
//Read the file which contains raw hits information
$counter_file = file(“stats.txt”);
$current_stats = $counter_file[0];
//New ststistics is Old Statistics + 1
$new_stats = $current_stats+1;
//Now write this information to the database file
//Open the file and make it blank
$file_open=fopen(“stats.txt”,”w”);
//Write latest statistics to file
fwrite($file_open,$new_stats);
//Close the file
fclose($file_open);
//Output the last number of visits
echo “Number of Visitors: $new_stats”;
?>

In the line
$counter_file = file(“stats.txt”);



  

We assign stored value of stats.txt to a variable $counter_file


Then we read the value stored in the file, since the value is on the first
line of the file stats.txt so it is counted as zero line in PHP!
We make a new variable $current_stats and store the value which is on
the first line of stats.txt in it.
New statistics is of course 1 value additional to the old value, e.g. if it was
4 then now it should be 5.
Then we remove everything from the database file using the code:
$file_open=fopen(“stats.txt”,”w”);
and write the latest visit statistics
fwrite($file_open,$new_stats);
then we close the database file using:
fclose($file_open);
Then we output the latest visit information using:
echo “Number of Visitors: $new_stats”;
In the above example the line
$file_open=fopen(“stats.txt”,”w”);
opens a text file stats.txt and removes everything from it, if the file does
not exist then it first creates a file stats.txt
NOTE: TO RUN THE ABOVE CODE YOU MUST CREATE A TEXT FILE stats.txt
IN THE SAME DIRECTORY IN WHICH YOU PUT THE FILE WITH THE ABOVE
COUNTER CODE AND SET PERMISSION OF stats.txt TO CHMOD 777
CHMOD refers to setting access privileges to a file. It is required if your web
hosting is on Linux Server. CHMOD 777 means that a file is readable, write able
and executable.
To set the permission-using FTP just right click the file and select CHMOD.
Then select all the check boxes.

"

Você também pode gostar