Escolar Documentos
Profissional Documentos
Cultura Documentos
PHP was developed by Rasmus Lerdorf . It is a freeware . It is also a weakly typed, free form
language. PHP has since evolved into a powerful server-side markup language with syntax that
resembles a mix between Perl and C.
PHP is a server-side scripting language designed specifically for the Web. Within an HTML
page, we can embed PHP code that will be executed each time the page is visited.HTML
generates the web page with the static text and images. However the need evolved for dynamic
web based application, mostly involving database usage. These dynamic usages are facilated by
PHP. Other tasks that PHP is especially good at are database access, disk access, networking and
text manipulation. PHP is an excellent alternative to such similar programming solutions as
Microsoft's proprietary scripting engine ASP and Allaire's rather expensive ColdFusion. As
mentioned before, PHP is a cross-platform language.
Finally, PHP is easy. If you know C or Perl, learning PHP is a cinch. The language is a mix
between the two, taking the best features from both. Plus PHP adds features to solve common
problems that programmers often encounter when programming for the Web.
1-
CONTENTS
ACKNOWLEDGEMENT.3
INTRODUCTION.4
1. PHP Syntax...5
2. Comments in PHP.....6
3. PHP variable..7
4. PHP string ....8
5. PHP echo .....10
6. Conditional statements.....11
7. Arrays in PHP...13
8. PHP Functions ........17
9. PHP $_GET and $_POST functions..20
10. Looping in PHP .....22
11. Benefits of PHP ....27
12. Screenshots of the project build ..................................................................28
CONCLUSION.32
BIBLIOGRAPHY.33
2-
Acknowledgement
I express my sincere thanks to my guide Ms. NEHA (Web page developer in PHP,SLR), for
guiding me right from the inception till the successful completion. I sincerely acknowledge her
for extending her valuable guidance, support for literature, critical reviews on PHP (PHP:
Hypertext Preprocessor) and the report and above all the moral support he had provided me with
all stages of this topic. Thanks also too many people who provided detailed reviews on the topic
PHP (PHP: Hypertext Preprocessor) and thanks also to the people who are responsible for the
publication of the report.
Finally, with all this assistance, little remains for which I can take full credit.
3-
INTRODUCTION
4-
1. PHP Syntax
PHP code is executed on the server, and the plain HTML result is sent to the browser.
<?php
?>
A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.
Below, we have an example of a simple PHP script which sends the text "Hello World" to the browser:
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to
distinguish one set of instructions from another.
There are two basic statements to output text with PHP: echo and print. In the example above we
have used the echo statement to output the text "Hello World".
Note: The file must have a .php extension. If the file has a .html extension, the PHP code will not be
executed.
5-
2. Comments in PHP
In PHP, there are two types of comments used here. These types are as follows:
Single line comment :
In this we use use // to make a single-line comment
<html>
<body>
<?php
//This is a single line comment
?>
</body>
</html>
<html>
<body>
<?php
/*
This is a multiline
Comment block
*/
?>
</body>
</html>
6-
3. PHP Variables
A variable is used to store information. A variable is a means of storing a value, such as text
string "Hello World!" or the integer value 4. A variable can then be reused throughout your code,
instead of having to type out the actual value over and over again. Also, variable names are casesensitive, so use the exact same capitalization when using a variable. The variables $a_number and
$A_number are different variables in PHP's eyes.
3.1 Variables in PHP
All variables in PHP start with a $ sign symbol. The correct way of declaring a variable in
PHP:
$var_name = value;
New PHP programmers often forget the $ sign at the beginning of the variable. In that case
it will not work. Let's try creating a variable containing a string, and a variable containing a
number:
<?php
$txt="Hello World!";
$x=16;
?>
7-
4. PHP String
A string variable is used to store and manipulate text.
4.1 String Variables in PHP
String variables are used for values that contain characters. In this chapter we are going to look at
the most common functions and operators used to manipulate strings in PHP. After we create a
string we can manipulate it. A string can be used directly in a function or it can be stored in a
variable. Below, the PHP script assigns the text "Hello World" to a string variable called $txt:
<?php
$txt="Hello World";
echo $txt;
?>
Hello World
There is only one string operator in PHP. The concatenation operator (.) is used to put two string
values together. To concatenate two string variables together, use the concatenation operator:
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
8-
If we look at the code above you see that we used the concatenation operator two times. This is
because we had to insert a third string (a space character), to separate the two strings.
12
The length of a string is often used in loops or other functions, when it is important to know
when the string ends. (i.e. in a loop, we would want to stop the loop after the last character in the
string).
The position of the string "world" in our string is position 6. The reason that it is 6 (and not 7), is
that the first position in the string is 0, and not 1.
9-
5. PHP- Echo
The PHP command echo is a means of outputting text to the web browser. Throughout your
PHP career you will be using the echo command more than any other.
To output a string, like we have done in previous lessons, use PHP echo. You can place
either a string variable or you can use quotes, like we do below, to create a string that the echo
function will output.
<?php
$txt="Hello World!";
echo $txt; echo <h5>Using ECHO</h5>;
?>
The output for above program is:
Hello World!
Using ECHO
In the above example we output "Hello World!" without a hitch. The text we are outputting
is being sent to the user in the form of a web page, so it is important that we use proper HTML
syntax!
In our second echo statement we use echo to write a valid Header 5 HTML statement. To do
this we simply put the <h5> at the beginning of the string and closed it at the end of the string.
Just because you're using PHP to make web pages does not mean you can forget about HTML
syntax!
10 -
6. Arrays in PHP
An array stores multiple values in one single variable. The problem is, a variable will hold only
one value. An array is a special variable, which can store multiple values in one single variable.
In PHP, there are three kind of arrays:
Numeric array - An array with a numeric index
Associative array - An array where each ID key is associated with a value
Multidimensional array - An array containing one or more arrays
In the above example we made use of the index / value structure of an array. The index was the
numbers we specified in the array and the values were the names of the cars. Each index of an
array represents a value that we can manipulate and reference. The general form for setting the
index of an array equal to a value is:
$array[index] = value;
11 -
12 -
7. Conditional Statements
Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
if statement - use this statement to execute some code only if a specified condition is true
if...else statement - use this statement to execute some code if a condition is true and
another code if the condition is false
if...else if....else statement - use this statement to select one of several blocks of code to
be executed
switch statement - use this statement to select one of many blocks of code to be executed
Now, let us discuss these one after other:
7.1 The if Statement
The if statement is used to execute some code only if a specified condition is true. Syntax for if
statement is given below.
Syntax
if (condition) code to be executed if condition is true;
EXAMPLE
The following example will output "Have a nice weekend!" if the current day is Friday:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>
</body>
</html>
13 -
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Example
The following example will output "Have a nice weekend!" if the current day is Friday,
otherwise it will output "Have a nice day!":
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
14 -
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Example
The following example will output "Have a nice weekend!" if the current day is Friday, and
"Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
15 -
This is how it works: First we have a single expression n (most often a variable), that is
evaluated once. The value of the expression is then compared with the values for each case in the
structure. If there is a match, the block of code associated with that case is executed. Use break
to prevent the code from running into the next case automatically. The default statement is used
if no match is found.
Example
<html>
<body>
<?php
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
16 -
8. PHP Functions
In PHP, there are more than 700 built-in functions. To keep the browser from executing a script
when the page loads, you can put your script into a function. A function will be executed by a
call to the function. You may call a function from anywhere within a page.
8.1 Create a PHP Function
A function will be executed by a call to the function.
Syntax
function functionName()
{
code to be executed;
}
Php function guidelines:
Give the function a name that reflects what the function does
The function name can start with a letter or underscore (not a number)
Example
<html>
<body>
<?php
function writeName()
{
echo "Kai Jim Refsnes";
}
echo "My name is ";
writeName();
?>
</body>
</html>
Output:
My name is Kai Jim Refsnes
17 -
Output:
18 -
Output:
1 + 16 = 17
19 -
When the user clicks the "Submit" button, the URL sent to the server could look something like
this:
The "welcome.php" file can now use the $_GET function to collect form data (the names of the
form fields will automatically be the keys in the $_GET array):
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
20 -
When the user clicks the "Submit" button, the URL will look like this:
http://www.w3schools.com/welcome.php
The "welcome.php" file can now use the $_POST function to collect form data (the names of the
form fields will automatically be the keys in the $_POST array):
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
Because the variables are not displayed in the URL, it is not possible to bookmark the page.
But it is possible in GET function, as the variables are appeared in URL.
21 -
EXAMPLE
The example below defines a loop that starts with i=1. The loop will continue to run as long as i
is less than, or equal to 5. i will increase by 1 each time the loop runs:
<html>
<body>
<?php
$i=1;
while($i<=5)
22 -
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>
Output:
The
The
The
The
The
number
number
number
number
number
is
is
is
is
is
1
2
3
4
5
EXAMPLE
The example below defines a loop that starts with i=1. It will then increment i with 1, and write
some output. Then the condition is checked, and the loop will continue to run as long as i is less
than, or equal to 5:
<html>
<body>
<?php
23 -
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<=5);
?>
</body>
</html>
Output:
The
The
The
The
The
number
number
number
number
number
is
is
is
is
is
2
3
4
5
6
Parameters:
init: Mostly used to set a counter (but can be any code to be executed once at the
beginning of the loop)
Condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues.
If it evaluates to FALSE, the loop ends.
increment: Mostly used to increment a counter (but can be any code to be executed at the
end of the loop)
The common tasks that are covered by a for loop are:
1. Set a counter variable to some initial value.
2. Check to see if the conditional statement is true.
24 -
Output:
The
The
The
The
The
number
number
number
number
number
is
is
is
is
is
1
2
3
4
5
25 -
The for each loop is used to loop through arrays. The syntax used for it is as follows:
Syntax
foreach ($array as $value)
{
code to be executed;
}
For every loop iteration, the value of the current array element is assigned to $value (and the
array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next
array value.
Example
The following example demonstrates a loop that will print the values of the given array:
<html>
<body>
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
</body>
</html>
Output:
one
two
three
26 -
Cross Platform
Runs on almost any Web server on several operating systems.
One of the strongest features is the wide range of supported databases
Web Servers: Apache, Microsoft IIS, Caudium, Netscape Enterprise Server
Operating Systems: UNIX, Mac OSX, Windows NT/98/2000/XP/2003
Supported Databases: Adabas D, dBase,Empress, FilePro (read-only), Hyperwave,IBM
DB2, Informix, Ingres, InterBase, FrontBase, mSQL, Direct MS-SQL, MySQL, ODBC, Oracle
(OCI7 and OCI8), Ovrimos, PostgreSQL, SQLite, Solid, Sybase, Velocis,Unix dbm
Cost Benefits
PHP is free. Open source code means that the entire PHP community will contribute
towards bug fixes. There are several add-on technologies (libraries) for PHP that are also
free.
27 -
FEW SNAPSHOTS
of projects handled
28 -
29 -
30 -
31 -
CONCLUSION
PHP is a great tool for writing dynamic web pages.
Few things we must remember:
i.
PHP is a server-side technology, and does not work in a browser.
ii.
The filename must have .php extension.
iii.
PHP enhanced pages can contain a mixture of HTML and PHP code.
iv.
PHP code must be enclosed in a <?php ?> tag.
32 -
BIBILIOGRAPHY
REFERENCE BOOK AND WEBSITES WHICH HELPED ME IN COMPLETING MY
PROJECT
BOOKS:
Beginning PHP 5
By: Dave W. Mercer, Allan Kent
WEBSITES:
www.wkiipedia.com
ww.tzag.com
www.w3schools.com
www.slideshare.com
www.php.net
33 -
34 -