Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

1 2 3 4 5 6 7 8 9 10 11 12

Introduction to PHP Installing and Uninstalling PHP

INDEX

Write a PHP code to check PHP Version Write a PHP Code to demonstrate variables Build a basic PHP Form Write a PHP code to read the contents of a file Write a PHP Code to check List Selection Write PHP Code to demonstrate functions Write a PHP code to demonstrate Arrays Write a PHP code to demonstrate a string manipulation with a Pig Latin Translator Write a PHP code to demonstrate a class Creating a Database Driven Application with PHP

1 INTRODUCTION TO PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document. It also has evolved to include a command-line interface capability and can be used in standalone graphical applications. PHP can be deployed on most web servers and as a standalone interpreter, on almost every operating system and platform free of charge. A competitor to Microsoft's Active Server Pages (ASP) server-side script engine and similar languages, PHP is installed on more than 20 million websites and 1 million web servers. PHP was originally created by Rasmus Lerdorf in 1995. The main implementation of PHP is now produced by The PHP Group and serves as the de facto standard for PHP as there is no formal specification. PHP is free software released under the PHP License which is incompatible with the GNU General Public License (GPL) due to restrictions on the usage of the term PHP. While PHP originally stood for "Personal Home Page", it is now said to stand for "PHP: Hypertext Preprocessor", a recursive acronym. Many high-profile open-source projects ceased to support PHP 4 in new code as of February 5, 2008, because of the GoPHP5 initiative, provided by a consortium of PHP developers promoting the transition from PHP 4 to PHP 5. As of 2011 PHP does not have native support for Unicode or multibyte strings; Unicode support is under development for a future version of PHP and will allow strings as well as class-, method-, and functionnames to contain non-ASCII characters. PHP interpreters are available on both 32-bit and 64-bit operating systems, but on Microsoft Windows the only official distribution is a 32-bit implementation, requiring Windows 32-bit compatibility mode while using Internet Information Services (IIS) on a 64-bit Windows platform. Experimental 64-bit versions of PHP 5.3.0 were briefly available for MS Windows, but have since been removed.

2 INSTALLING AND UNINSTALLING PHP


Installation
The Windows PHP installer for later versions of PHP is built using MSI technology using the Wix Toolkit ( http://wix.sourceforge.net/). It will install and configure PHP and all the built-in and PECL extensions, as well as configure many of the popular web servers such as IIS, Apache, and Xitami. First, install your selected HTTP (web) server on your system, and make sure that it works. Then proceed with one of the following install types. Normal Install Run the MSI installer and follow the instructions provided by the installation wizard. You will be prompted to select the Web Server you wish to configure first, along with any configuration details needed. You will then be prompted to select which features and extensions you wish to install and enable. By selecting "Will be installed on local hard drive" in the drop-down menu for each item you can trigger whether to install the feature or not. By selecting "Entire feature will be installed on local hard drive", you will be able to install all sub-features of the included feature (for example by selecting this option for the feature "PDO" you will install all PDO Drivers). The installer then sets up PHP to be used in Windows and the php.ini file, and configures certain web servers to use PHP. The installer will currently configure IIS, Apache, Xitami, and Sambar Server; if you are using a different web server you'll need to configure it manually. Silent Install The installer also supports a silent mode, which is helpful for Systems Administrators to deploy PHP easily. To use silent mode: msiexec.exe /i php-VERSION-win32-install.msi /q You can control the install directory by passing it as a parameter to the install. For example, to install to e:\php: msiexec.exe /i php-VERSION-win32-install.msi /q INSTALLDIR=e:\php You can also use the same syntax to specify the Apache Configuration Directory (APACHEDIR), the Sambar Server directory (SAMBARDIR), and the Xitami Server directory (XITAMIDIR). You can also specify what features to install. For example, to install the mysqli extension and the CGI executable:

msiexec.exe /i php-VERSION-win32-install.msi /q ADDLOCAL=cgi,ext_php_mysql

Uninstallation
Navigate to the drive where the php folder is located.Select the folder. Delete the PHP folder from the drive Next, navigate to C:\WINDOWS and search for a file named php.ini and select the file. Delete this file from C:\WINDOWS PHP has been completely removed from the system. Its as simple as this.

3 WRITE A PHP CODE TO CHECK PHP VERSION


<html> <head> <title>Hello in PHP</title> </head> <body> <h1>Hello in PHP</h1> <? print "Hello, world!"; phpInfo(); ?> </body> </html>

4 WRITE A PHP CODE TO DEMONSTRATE VARIABLES


<html> <head> <title>Variables</title> </head> <body> <? $StringVariable="Hello World"; $IntegerVariable=2*2; $FloatVariable=3.145; print $StringVariable; print "<br/>"; print $IntegerVariable; print "<br/>"; print $FloatVariable; print "<br/>"; ?> </body> </html>

5 BUILD A BASIC PHP FORM


<html> <head> <title>What's your name?</title> </head> <body> <h1>What's your name?</h1> <h3>Writing a form for user input (POST Method)</h3> <form method = "post" action = "index.php"> Please type your name: <input type = "text" name = "userName" value = ""> <br> <input type = "submit"> </form> <h1>What's your Age?</h1> <h3>Writing a form for user input (GET Method)</h3> <form method = "get" action = "index.php"> Please type your Age: <input type = "text" name = "userAge" value = ""> <br> <input type = "submit"> </form> <? if (!empty ($_POST["userName"])) { print "<h3>Hi there, {$_POST["userName"]} !</h3>"; } if (!empty ($_GET["userAge"])) { print "<h3>Your age, {$_GET["userAge"]} !</h3>"; } ?> </body> </html>

6 WRITE A PHP CODE TO READ THE CONTENTS OF A FILE


<html> <head> <title>Tip of the day</title> </head> <body> <center> <h1>Tip of the day</h1> <div style = "border-color:green; border-style:groove; border-width:2px"> <? readfile("tip.txt"); ?> </div> </center> </body> </html>

7 WRITE A PHP CODE TO CHECK LIST SELECTION


<html> <head> <title>What's your name?</title> </head> <body> <h1>What's your name?</h1> <h3>Writing a form for user input (POST Method)</h3> <form method = "post" action = "index.php"> <select name = "action"> <option value = "fast asleep">fast asleep</option> <option value = "drinking cappuccino">drinking cappuccino</option> <option value = "wandering around aimlessly">wandering around aimlessly</opti <option value = "doing nothing in particular">doing nothing in particular</op </select> <input type = "submit"> </form> <? if (!empty($_POST["action"])) { print "<h3>Selected Option is {$_POST["action"]} !</h3>"; } ?> </body> </html>

8 WRITE PHP CODE TO DEMONSTRATE FUNCTIONS


<html> <head> <title>Param Old Man</title> </head> <body> <h1>Param Old Man </h1> <h3>Demonstrates use of function parameters</h3> <? print verse(1); print chorus(); print verse(2); print chorus(); print verse(3); print chorus(); print verse(4); print chorus(); function verse($stanza) { switch ($stanza) { case 1: $place = "thumb"; break; case 2: $place = "shoe"; break; case 3: $place = "knee"; break; case 4: $place = "door"; break; default: $place = "I don't know where"; } // end switch $output = <<<HERE This old man, he played $stanza<br> He played knick-knack on my $place<br><br> HERE; return $output; } // end verse function chorus() { $output = <<<HERE ...with a knick-knack<br> paddy-whack<br> give a dog a bone<br> this old man came rolling home<br> <br><br>

HERE; }

return $output;

// end chorus ?> </body> </html>

9 WRITE A PHP CODE TO DEMONSTRATE ARRAYS


<html> <head> <title>Distance calculator</title> </head> <body> <? $city = array( "Indianapolis", "New York", "Tokyo", "London" ); $distance = array( array(0, 648, 6476, 4000), array(648, 0, 6760, 3470), array(6476, 6760, 0, 5956), array(4000, 3470, 5956, 0) ); $cityA=0; $cityB=3; $result = $distance[$cityA][$cityB]; print "<h3>The distance between "; print "$city[$cityA] and $city[$cityB]"; print " is $result miles.</h3>"; ?> </body> </html>

10 WRITE A PHP CODE TO DEMONSTRATE A STRING MANIPULATION WITH A PIG LATIN TRANSLATOR
<html> <head> <title>Pig Latin Generator</title> </head> <body> <h1>Pig Latin Generator</h1> <? if (empty($_POST["inputString"])) { print <<<HERE <form action = "index.php" method = "post"> <textarea name = "inputString" rows = 20 cols = 40></textarea> <input type = "submit" value = "pigify"> </form> HERE; } else { $inputString = $_POST["inputString"]; //there is a value, so we'll deal with it //break phrase into array $words = split(" ", $inputString); foreach ($words as $theWord) { $theWord = rtrim($theWord); $firstLetter = substr($theWord, 0, 1); $restOfWord = substr($theWord, 1, strlen($theWord)); print "$firstLetter) $restOfWord <br> \n"; if (strstr("aeiouAEIOU", $firstLetter)) { //it's a vowel $newWord = $theWord . "way"; } else { //it's a consonant $newWord = $restOfWord . $firstLetter . "ay"; } // end if $newPhrase = $newPhrase . $newWord . " "; } // end foreach print $newPhrase; } // end if ?> </body> </html>

11 WRITE A PHP CODE TO DEMONSTRATE A CLASS


<html> <head> <title>Class Example</title> </head> <body> <?php class MyClass { const constant = 'constant value'; function showConstant() { echo self::constant . "\n"; } } echo MyClass::constant . "\n"; $classname = "MyClass"; echo $classname::constant . "\n"; // As of PHP 5.3.0 $class = new MyClass(); $class->showConstant(); echo $class::constant . "\n"; // As of PHP 5.3.0 ?> </body> </html>

12 CREATING A DATABASE DRIVEN APPLICATION WITH PHP


<html> <head> <title>Class Example</title> </head> <body> <?php $con = mysqli_connect("localhost", "root", ""); if (!$con) { exit('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } mysqli_select_db($con, "userdatabase"); $LastName = mysqli_real_escape_string($con, "Kapoor"); $query = mysqli_query($con, "SELECT * FROM usertable WHERE LastName='" . $LastName . "'"); if (mysqli_num_rows($query) < 1) { exit("The person is not found. Please check the spelling and try again"); } //$rows = mysqli_fetch_row($query); while ($row = mysqli_fetch_array($query)) { echo $row['FirstName'] . " " . $row['LastName']." ".$row['Email']; echo "<br />"; } mysqli_close($con); ?> </body> </html>

You might also like