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() function……………………………………………………………………11 5. PHP echo ………………………………………………………….………………………..12 6. Conditional statements……………………………………………………………….…….12 6.1 Numeric arrays…………………………………………………………………………13 6.2 Associative arrays………………………………………………………………………13 6.3 Multidimensionol arrays…………………………………………………………….....14 7. Arrays in PHP………………………………………………………………………….……14 7.1 The if statement………………………………………………………………………..14 7.2 The if……else statement……………………………………………………………....15 7.3 The if……else if…….else statement………………………………………………….16 7.4 Switch statement……………………………………………………………………… .16

.23 10.……………………………………………………………….2 The $_ POST function………………………………………………………………….8.25 10.….26 10.. PHP Functions ……………..1 Create a PHP function…………………………………………………………………17 8. Looping in PHP ………………………………………………………………………..27 11..1 The $_GET function……………………………………………………………………21 9. Difference.17 8.28 12.. Benefits of PHP ………………………………………………………………………….NET and PHP……………………………………………………….3 Return values in PHP function……………………………………………………….2 Adding parameters in PHP function…………………………………………………....1 The while loop………………………………………………………………………. PHP $_GET and $_POST functions…………………………………………………………20 9. .18 8.ASP .2 The do….22 10.24 10..3 The for loops………………………………………………………………………...19 9.31 ..4 The for each loop……………………………………………………………………..29 CONCLUSION………………………………………………………………………….while loop………………………………………………………………….……30 BIBLIOGRAPHY…………………………………………………………………………….

ROHIT DUGGAL Computer Science & Engg. little remains for which I can take full credit. Bikaner).Acknowledgement I express my sincere thanks to my guide Mr. support for literature. 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. CET . Manish Shandilya (Web page developer in PHP. with all this assistance. 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. Finally. I sincerely acknowledge him for extending his valuable guidance. for guiding me right from the inception till the successful completion. VII semester.

Open up thousands of possibilities for online tools. etc.) PHP is FREE to download from the official PHP resource: http://www. It then sees which parts it needs to show to visitors (content and pictures) and hides the other stuff (file operations.) 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. 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. behind the scenes scripting language that your visitors won't see! When someone visits your PHP webpage.php. PHP will allow you to: • • • • Reduce the time to create large websites. Linux. Sybase.INTRODUCTION "PHP is an HTML-embedded scripting language. Check out PHP . etc. UNIX. However. Much of its syntax is borrowed from C. After the translation into HTML. Create a customized user experience for visitors based on information that you have gathered from them. Generic ODBC. it does contain a lot of terms you may not be used to. Solid. IIS." PHP is a server-side scripting language. Another way to think of PHP is a powerful. Java and Perl with a couple of unique PHP-specific features thrown in. Oracle. etc. This is generally a good definition of PHP. your web server processes the PHP code.Hot Scripts for examples of the great things that are possible with PHP. etc. Why PHP? • • • • PHP runs on different platforms (Windows.) PHP is compatible with almost all servers used today (Apache. Informix.net/ PHP is easy to learn and runs efficiently on the server side . Allow creation of shopping carts for e-commerce websites.) then translates your PHP into HTML. The goal of the language is to allow web developers to write dynamically generated pages quickly. math calculations. PostgreSQL. like ASP PHP scripts are executed on the server PHP supports many databases (MySQL.

and some PHP scripting code. A PHP scripting block can be placed anywhere in the document. just like an HTML file. There are two basic statements to output text with PHP: echo and print. we recommend that you use the standard form (<?php) rather than the shorthand form. we have an example of a simple PHP script which sends the text "Hello World" to the browser: <html> <body> <?php echo "Hello World". Basic PHP Syntax A PHP scripting block always starts with <?php and ends with ?>. . the PHP code will not be executed.php extension. and the plain HTML result is sent to the browser. For maximum compatibility. PHP Syntax PHP code is executed on the server. In the example above we have used the echo statement to output the text "Hello World".html extension. Note: The file must have a . The semicolon is a separator and is used to distinguish one set of instructions from another. <?php ?> A PHP file normally contains HTML tags. On servers with shorthand support enabled you can start a scripting block with <? and end with ?>.1. Below. ?> </body> </html> Each code line in PHP must end with a semicolon. If the file has a .

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

In the example above. In a strongly typed programming language. such as text string "Hello World!" or the integer value 4.3. The variables $a_number and $A_number are different variables in PHP's eyes.1 Variables in PHP All variables in PHP start with a $ sign symbol. 0-9. a variable does not need to be declared before adding a value to it.2 PHP is a Loosely Typed Language In PHP. variable names are case-sensitive. ?> 3. and _ ) A variable name should not contain spaces. the variable is declared automatically when you use it. $x=16. and a variable containing a number: <?php $txt="Hello World!". PHP Variables A variable is used to store information. so use the exact same capitalization when using a variable. A variable can then be reused throughout your code.PHP automatically converts the variable to the correct data type. depending on its value. A-Z. Also. In PHP. A variable is a means of storing a value. If a variable name is more than one word. you have to declare (define) the type and name of the variable before using it. or with capitalization ($my_String) . you see that you do not have to tell PHP which data type the variable is. Let's try creating a variable containing a string. 3. it should be separated with an underscore ($my_string). In that case it will not work. New PHP programmers often forget the $ sign at the beginning of the variable. The correct way of declaring a variable in PHP: $var_name = value. instead of having to type out the actual value over and over again. 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.

" " .1 String Variables in PHP String variables are used for values that contain characters. echo $txt. The concatenation operator (. PHP String A string variable is used to store and manipulate text. use the concatenation operator: <?php $txt1="Hello World!". ?> The output of the code above will be: Hello World 4. To concatenate two string variables together. ?> The output of the code above will be: . After we create a string we can manipulate it. Below. the PHP script assigns the text "Hello World" to a string variable called $txt: <?php $txt="Hello World".2 The Concatenation Operator There is only one string operator in PHP. $txt2. echo $txt1 . 4. A string can be used directly in a function or it can be stored in a variable. In this chapter we are going to look at the most common functions and operators used to manipulate strings in PHP.4. $txt2="What a nice day!".) is used to put two string values together.

to separate the two strings. 5. is that the first position in the string is 0. If a match is found. PHP. 4. in a loop.Hello World! What a nice day! If we look at the code above you see that we used the concatenation operator two times. this function will return the position of the first match.4 The strpos() function The strpos() function is used to search for character within a string. and not 1. Throughout your PHP career you will be using the echo command more than any other. ?> The output of the code above will be: 12 The length of a string is often used in loops or other functions. Let's find the length of a string: <?php echo strlen("Hello world!").e."world"). (i. The reason that it is 6 (and not 7). This is because we had to insert a third string (a space character). it will return FALSE. Let's see if we can find the string "world" in our string: <?php echo strpos("Hello world!".3 The strlen() function The strlen() function is used to return the length of a string. when it is important to know when the string ends. . If no match is found. we would want to stop the loop after the last character in the string). 4.Echo The PHP command echo is a means of outputting text to the web browser. ?> The output of the code above will be: 6 The position of the string "world" in our string is position 6.

Just because you're using PHP to make web pages does not mean you can forget about HTML syntax! 6. like we do below. The problem is. a variable will hold only one value. like we have done in previous lessons. Arrays in PHP An array stores multiple values in one single variable.To output a string. In PHP. echo $txt. <?php $txt="Hello World!". 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. An array is a special variable. which can store multiple values in one single variable. to create a string that the echo function will output. 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. use PHP echo. To do this we simply put the <h5> at the beginning of the string and closed it at the end of the string. You can place either a string variable or you can use quotes. there are three kind of arrays: .

• • • Numeric array . each ID key is associated with a value. In the following example you access the variable values by referring to the array name and index: <?php $cars[0]="Saab". $cars[1]="Volvo".". " and " . 6. $cars[2]="BMW".2 Associative Arrays An associative array.An array where each ID key is associated with a value Multidimensional array . . " are Swedish cars. If you wanted to store the salaries of your employees in an array. Each index of an array represents a value that we can manipulate and reference. $cars[3]="Toyota". a numerical array is not always the best way to do it. The index was the numbers we specified in the array and the values were the names of the cars.1 Numeric Arrays A numeric array stores each array element with a numeric index. In the above example we made use of the index / value structure of an array. When storing data about specific named values.An array with a numeric index Associative array .An array containing one or more arrays 6. With associative arrays we can use the values as keys and assign values to them. $cars[1] . ?> The code above will output: Saab and Volvo are Swedish cars. In an associative array a key is associated with a value. The general form for setting the index of an array equal to a value is: • $array[index] = value. echo $cars[0] .

$ages['Joe'] = "34". Conditional Statements Very often when you write code..In this example we use an array to assign ages to the different persons: <?php $ages['Peter'] = "32".use this statement to select one of several blocks of code to be executed . And each element in the sub-array can be an array.else if. ?> The code above will output: Peter is 32 years old. $ages['Peter'] . 6...else statement .else statement . each element in the main array can also be an array. and so on. 7.use this statement to execute some code only if a specified condition is true if.3 Multidimensional Arrays In a multidimensional array.. In PHP we have the following conditional statements: • • • if statement . " years old. you want to perform different actions for different decisions. $ages['Quagmire'] = "30"..use this statement to execute some code if a condition is true and another code if the condition is false if. You can use conditional statements in your code to do this.". echo "Peter is " ...

otherwise it will output "Have a nice day!": .. EXAMPLE The following example will output "Have a nice weekend!" if the current day is Friday: <html> <body> <?php $d=date("D").use this statement to select one of many blocks of code to be executed Now. ?> </body> </html> 7. Syntax if (condition) code to be executed if condition is true.• switch statement .else statement is used to execute some code if a condition is true and another code if a condition is false.. if ($d=="Fri") echo "Have a nice weekend!"..2 The if. Syntax for it is: 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..1 The if Statement The if statement is used to execute some code only if a specified condition is true.else Statement The if. else code to be executed if condition is false.. let us discuss these one after other: 7. Syntax for if statement is given below.

Otherwise it will output "Have a nice day!": <html> <body> <?php $d=date("D").. 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.else statement is used to select one of several blocks of code to be executed....3 The if. else code to be executed if condition is false.<html> <body> <?php $d=date("D"). else echo "Have a nice day!".. if ($d=="Fri") echo "Have a nice weekend!".else Statement The if..else if. elseif (condition) code to be executed if condition is true. if ($d=="Fri") echo "Have a nice weekend!". ?> </body> </html> 7.else if. Syntax used for it is: if (condition) code to be executed if condition is true. .

If there is a match. } This is how it works: First we have a single expression n (most often a variable). break.4 Switch Statement Conditional statements are used to perform different actions based on different conditions.elseif ($d=="Sun") echo "Have a nice Sunday!". The syntax used for switch statements is: Syntax switch (n) { case label1: code to be executed if n=label1. else echo "Have a nice day!". that is evaluated once. the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. break. default: code to be executed if n is different from both label1 and label2. Switch statement is used to select one of many blocks of code to be executed. The default statement is used if no match is found. ?> </body> </html> 7. Example <html> <body> <?php switch ($x) { . case label2: code to be executed if n=label2. The value of the expression is then compared with the values for each case in the structure.

PHP Functions In PHP. break.case 1: echo "Number 1". 8. You may call a function from anywhere within a page. To keep the browser from executing a script when the page loads.1 Create a PHP Function A function will be executed by a call to the function. case 3: echo "Number 3". } ?> </body> </html> 8. . break. there are more than 700 built-in functions. default: echo "No number between 1 and 3". A function will be executed by a call to the function. you can put your script into a function. case 2: echo "Number 2". break.

?> </body> </html> Output: My name is Kai Jim Refsnes . } 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 ".Syntax function functionName() { code to be executed. writeName().

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

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

• Cross Platform Runs on almost any Web server on several operating systems. mSQL. Velocis. dBase. Hyperwave. Informix. Difference between PHP and ASP . 12. There are several add-on technologies (libraries) for PHP that are also free. It is a language. Microsoft IIS. PostgreSQL. Solid.IBM DB2.net Some of the differences between these are mentioned in the table below: PHP 1. but a FRAMEWORK. Direct MS-SQL. InterBase. MySQL. Windows NT/98/2000/XP/2003 Supported Databases: Adabas D.Unix dbm • Cost Benefits PHP is free. Caudium. FilePro (read-only).NET 1. Mac OSX. SQLite. Ovrimos. ASP. It is not a language. Ingres. Open source code means that the entire PHP community will contribute towards bug fixes. One of the strongest features is the wide range of supported databases Web Servers: Apache. ASP . ODBC. Sybase. PHP does not follow the OOP paradigm entirely.Empress. FrontBase. Netscape Enterprise Server Operating Systems: UNIX. 2. Oracle (OCI7 and OCI8). 2.NET framework is built entirely on an OOP paradigm .

NET is not an open source. ASP. PHP is Open source. ASP. 4. Comparatively less. 5. 5. CONCLUSION  PHP is a great tool for writing dynamic web pages. PHP has several built-in functions 3. Compilation speed is much faster. 7.3. . need an actual language like C# for ASP. PHP has its own error-handling and library of functions.NET had pretty much guarantees more cost because MS-SQL isn't free. ASP . PHP allows to use MySQL (Free) and provides functions to make it easy. In error-handling.NET doesn't have a rich list of functions like that.NET 7. 6. 4. 6.

iv. PHP is a server-side technology. PHP enhanced pages can contain a mixture of HTML and PHP code. ii. The filename must have . iii.php extension. Few things we must remember: i. BIBILIOGRAPHY REFERENCE BOOK AND WEBSITES WHICH HELPED ME IN COMPLETING MY PROJECT . and does not work in a browser. PHP code must be enclosed in a <?php ?> tag.

com www.net . Mercer.com • ww. Allan Kent WEBSITES: • www.w3schools.com • www.wkiipedia.tzag.com • • www.BOOKS: • Beginning PHP 5 By: Dave W.php.slideshare.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.