Anda di halaman 1dari 24

ABSTRACT

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.

CONTENTS

ACKNOWLEDGEMENT.5 INTRODUCTION.6 1. PHP Syntax.7 1.1 Basic PHP Syntax.7 2. Comments in PHP....8 3. PHP variable..9 3.1 Variables In PHP...9 3.2 PHP is a loosely typed language...9 3.3 Naming rules for variable.9 4. PHP string ....10 4.1 String variable in PHP...10 4.2 The concatenation operatar...10 4.3 The strlen() function..11 4.4 the strpos() function11 5. PHP echo ...12 6. Conditional statements..12 6.1 Numeric arrays13 6.2 Associative arrays13 6.3 Multidimensionol arrays.....14 7. Arrays in PHP.14 7.1 The if statement..14 7.2 The ifelse statement....15 7.3 The ifelse if.else statement.16 7.4 Switch statement .16

8. PHP Functions ...17 8.1 Create a PHP function17 8.2 Adding parameters in PHP function..18 8.3 Return values in PHP function...19 9. PHP $_GET and $_POST functions20 9.1 The $_GET function21 9.2 The $_ POST function.22 10. Looping in PHP ....23 10.1 The while loop..24 10.2 The do.while loop.25 10.3 The for loops....26 10.4 The for each loop.27 11. Benefits of PHP . .28 12. Difference- ASP .NET and PHP.....29 CONCLUSION.30 BIBLIOGRAPHY.31

Acknowledgement
I express my sincere thanks to my guide Mr. Manish Shandilya (Web page developer in PHP, Bikaner), for guiding me right from the inception till the successful completion. I sincerely acknowledge him for extending his 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. ROHIT DUGGAL Computer Science & Engg. VII semester, CET

INTRODUCTION

"PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly." PHP is a server-side scripting language. This is generally a good definition of PHP. However, it does contain a lot of terms you may not be used to. Another way to think of PHP is a powerful, behind the scenes scripting language that your visitors won't see! When someone visits your PHP webpage, your web server processes the PHP code. It then sees which parts it needs to show to visitors (content and pictures) and hides the other stuff (file operations, math calculations, etc.) then translates your PHP into HTML. After the translation into HTML, it sends the webpage to your visitor's web browser. So mainly PHP is:

PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software PHP is free to download and use

It is also helpful to think of PHP in terms of what it can do for you. PHP will allow you to:

Reduce the time to create large websites. Create a customized user experience for visitors based on information that you have gathered from them. Open up thousands of possibilities for online tools. Check out PHP - Hot Scripts for examples of the great things that are possible with PHP. Allow creation of shopping carts for e-commerce websites.

Why PHP?

PHP runs on different platforms (Windows, Linux, UNIX, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource: http://www.php.net/ PHP is easy to learn and runs efficiently on the server side

1. PHP Syntax
PHP code is executed on the server, and the plain HTML result is sent to the browser.

Basic PHP Syntax


A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document. On servers with shorthand support enabled you can start a scripting block with <? and end with ?>. For maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.

<?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.

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>

Multiple line comment : In this we use /* and */ to make a large comment block.

<html> <body> <?php /* This is a multiline Comment block */ ?> </body> </html>

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 case-sensitive, 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; ?>

3.2 PHP is a Loosely Typed Language In PHP, a variable does not need to be declared before adding a value to it. In the example above, you see that you do not have to tell PHP which data type the variable is.PHP automatically converts the variable to the correct data type, depending on its value. In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it. In PHP, the variable is declared automatically when you use it.

3.3 Naming Rules for Variables


A variable name must start with a letter or an underscore "_" A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ) A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($my_String)

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; ?>


The output of the code above will be:

Hello World

4.2 The Concatenation Operator 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; ?>

The output of the code above will be:

Hello World! What a nice day!

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.

4.3 The strlen() function The strlen() function is used to return the length of a string. Let's find the length of a string:
<?php echo strlen("Hello world!"); ?>
The output of the code above will be:

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).

4.4 The strpos() function The strpos() function is used to search for character within a string. If a match is found, this function will return the position of the first match. If no match is found, it will return FALSE. Let's see if we can find the string "world" in our string:
<?php echo strpos("Hello world!","world"); ?>
The output of the code above will be:

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.

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!

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

6.1 Numeric Arrays A numeric array stores each array element with a numeric index. In the following example you access the variable values by referring to the array name and index:
<?php $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0] . " and " . $cars[1] . " are Swedish cars."; ?>
The code above will output:

Saab and Volvo are Swedish cars.

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;

6.2 Associative Arrays An associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them. In an associative array a key is associated with a value. If you wanted to store the salaries of your employees in an array.

In this example we use an array to assign ages to the different persons:


<?php $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; echo "Peter is " . $ages['Peter'] . " years old."; ?>
The code above will output:

Peter is 32 years old.

6.3 Multidimensional Arrays In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

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>

7.2 The if...else Statement The if....else statement is used to execute some code if a condition is true and another code if a condition is false. Syntax for it is:

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>

7.3 The if...else if....else Statement The if....else if...else statement is used to select one of several blocks of code to be executed. Syntax used for it is:
if (condition) code to be executed if condition is true; 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>

7.4 Switch Statement Conditional statements are used to perform different actions based on different conditions. Switch statement is used to select one of many blocks of code to be executed. The syntax used for switch statements is:
Syntax
switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; default: code to be executed if n is different from both label1 and label2; }

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>

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

8.2 Adding parameters in PHP Functions To add more functionality to a function, we can add parameters. A parameter is just like a variable. Parameters are specified after the function name, inside the parentheses.
Example <html> <body> <?php function writeName($fname) { echo $fname . " Refsnes.<br />"; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?> </body> </html>
Output:

My name is Kai Jim Refsnes. My sister's name is Hege Refsnes. My brother's name is Stale Refsnes.

8.3 Return values in PHP Functions To let a function return a value, use the return statement. Example
<html> <body> <?php function add($x,$y) { $total=$x+$y; return $total; } echo "1 + 16 = " . add(1,16); ?> </body> </html>
Output:

1 + 16 = 17

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.

12. Difference between PHP and ASP .net


Some of the differences between these are mentioned in the table below: PHP 1. It is a language. 2. PHP does not follow the OOP paradigm entirely. ASP .NET 1. It is not a language, but a FRAMEWORK. 2. ASP.NET framework is built entirely on an OOP paradigm

3. PHP is Open source. 4. Compilation speed is much faster. 5. PHP allows to use MySQL (Free) and provides functions to make it easy. 6. PHP has its own error-handling and library of functions. 7. PHP has several built-in functions

3. ASP .NET is not an open source. 4. Comparatively less. 5. ASP.NET had pretty much guarantees more cost because MS-SQL isn't free. 6. In error-handling, need an actual language like C# for ASP.NET 7. ASP.NET doesn't have a rich list of functions like that.

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.

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

Anda mungkin juga menyukai