4 Installing WAMP and PHP Basics

Hiren Joshi

epgp books

 

 

 

What is PHP?

 

PHP is a server side scripting language. PHP is one of the most popular server side scripting language. PHP code can embedded within HTML tag.

 

PHP stands for Hypertext PreProcessor. PHP was also known as Personal Home Page. PHP was originally designed by Rasmus Lerdrof. Then other people also contributed for PHP development. The first version of PHP is resealed in 1994. Then various versions of PHP are released. The latest version as on today is PHP 5.5. As of May 2015, PHP is installed on more than 244 million websites world-wide. You can check the current number of PHP installation world wide on PHP official web page http://www.php.net/usage.php

 

PHP is an open source project. Open source means you can access the source code of PHP. Open Source philosophy allows you to use, modify and redistribute it without any charge.

 

Strengths of PHP

  • cost: PHP is freely available. You can download it free of cost from PHP’s official web site http://www.php.net
  • Easy to learn: PHP is easy to learn. Its syntax is very similar to C language.
  • Performance: PHP is very fast. Using a single inexpensive server, PHP can serve millions of hits per day. The benchmark published by Zend Technologies (http://www.zend.com) proves it.
  • Portability: PHP can work on major operating system inclusing Linux, Unix, Windows.
  • Scalability: PHP has shared nothing architecture. That means PHP can effectively and cheaply implement horizontal scaling with large numbers of commodity servers.
  • Database Integration: PHP can connect with many database systems including MySQL, Oracle, PostgreSQL, DB2, Informix, Sybase, Hyperwave, Interbase and FilePro. PHP has support SQLLite.Using ODBC (Open Database Connectivity) PHP can connect to database which provides ODBC driver. Using ODBC PHP can connect with MS SQL server also.
  • Built in Libraries: PHP comes with many built-in libraries for providing many functionalities including web service connectivity, sending email, genereate PDF document etc.
  • Object Oriented Support: From PHP 5 onwards, PHP support Object Oriented concepts including inheritance, abstract classes and methods, interface.
  • Availability of support: Zend Technologies promote PHP by providing support and documentation. There are many communities available to get help for PHP.

 

Install WAMP on your PC

 

Install WAMP on your PV is very easy. WAMP stands for Windows, Apache, MySQL and PHP. Windows is an operating system. Apache is a web server. MySQL is a relational database management system and PHP is a server side scripting language.

 

To install WAMP,

 

1. Go to www.wamp.com and download the appropriate server. I have used WAMP Server (64 Bits & PHP 5.5) 2.5 as I have Windows 8 64 bit operating system.

 

 

 

2. Click on the downloaded .exe file, which shows first screen with version for Apache, MySQL and PHP.

 

 

 

3.  Click next button and the screen ask you about license agreement. Select I accept the agreement and click the next button.

 

 

4. Then provide the path where you want to install WAMP. By Default it will take C:\Wamp. Click the next button.

 

 

5. Check for create desktop icon and Quick launch checkbox to create desktopicon and quick launch checkbox and click the next button.

 

 

7.  Click install button to install wamp.

8. The next screen confirm about default browser do you want to use. In Windows OS by default it is internet explorer. In this case, I prefer mozila firefox. If you have already installed firefox, you can find it at C:\Program Files (x86)\Mozilla Firefox\firefox.exe.

 

9. Next screen will prompts for to set parameters for PHP mail. In case you are not sure about it, just click the next button.

 

    10.  Wamp successfully installation will shows following screen. This screen describes that the PC has which versions of Apache, MySQL and PHP are installed.

 

 

 

Once WAMP successful installed, you can start by just double click on the short cut icon on the desktop. It may ask you to make sure that are you sure to run this program. Click yes button. Once wamp is running it shows green symbol at the status bar like . To test WAMP is working, open browser and type http://localhost/ on browser’s address bar. The following screen will be appearing. You can click on phpinfo() in Tools category to view information about PHP.

 

 

Editors for PHP

 

The PHP code can be written in any editor notepad. Even you can use IDE (Integrated Development Environment) tools like netbeans, adobe dreamviewers etc. Netbeans is freely avialble while dreamviewer is required to get license to use it.

 

 

Notepad++ is a free of cost available editor to write code for many programming languages. Though it does not provide many good features like IDE, it is better than notepad. Notepad++ is provide color code while writing codes. Hence it makes the task easier for developer to remember syntax. You can freely download Notepad++ from notepad-plus-plus.org. You can easily install notepad++ by simply double click on the downloaded file. Once it is installed you can use it. The version of notepad++ is used here is 6.6.9

 

 

 

Type the following code in notepad++

 

<?php

echo “Welcome to PHP”;

?>

 

Now click on save icon in notepad++ or from file menu click save menu item. It will open save as dialog box. You must save the file in www directory of wamp folder. Here we install wamp in c:. So the path is C:/wamp. Inside wamp create a new folder named PHP1. Save the file at C:/Wamp/www/PHP1/prog1.php

 

 

To run the program open browser (now onwards we will use mozila firefox as our browser.) Type localhost. It will shows all the folder for our PHP file. As we have created folder PHP1, it shows as PHP1 under Your Projects. To get the file of PHP1 folder, type localhost/php1 at browser’s address bar.

 

 

 

Note: 1. Before Install WAMP make sure your PC has Microsoft Visual C++ 2012 Redistributable (X64) installed for Windows 8/8.1 64-bit OS. In case it is not installed, WAMP will not install successfully. If it is not installed, then

  • first uninstall WAMP,
  • restart your PC,
  • Install Microsoft Visual C++ 2012 Redistributable (X64)
  • Install Wamp.

   Basics of PHP

  • PHP tag start with <?php and end with ?>. The PHP engine will run the code inside the PHP tags. This tag style is known as XML tag style. There are also other styles available, but we will stick with this style. For your reference the other tag styles are:
  • o  Short style: <? Echo “Welcome” ?>
  • Script Style <script language=’php’>echo “Welcome” </script> o ASP Style: <% echo “Welcome” %>      Whitespace includes spaces, tabs and new line (carriage return). It is advisable to write ode with appropriate white space to make the code more readable. Extra Whitespaces are ignored by PHP similar to HTML.
  • PHP also support comments. Comments are not interpreted by PHP. Single line comments are written by // (two consecutive forward slash) and multiline comments are written as /* and */. Multiline comment is also known as block line comment. For example,

 

echo “welcome” // this will print welcome on browser.

/* This program is written by

Dr. Hiren Joshi for explaining

Multiline Comments on PHP */

  • PHP statement ends with semicolon
  • PHP syntax is similar to C language.

    Variable in PHP

  • Variable stores a value which can be changed during program execution.
  • In PHP variables are declared as $followed by variable name ($VariableName).
  • Variable names are case sensitive in PHP. That means $name, $Name are treated as two different variables.
  • Varibale name can contain letters, numbers and underscore.
  • Special Character is not allowed for variable  name.
  • Variable name cannot start with digit or two underscore.
  • Varibale name cannot used the PHP reserved key words. For example, $this is a reserved keyword in  PHP which is used for objects. So, you cannot define a variable name as $this.

     A value can be assigned to variable by using assignment operator (=). The general syntax is $VariableName = value. So this value is assigned to this variable. For example, $name = “Hiren”;

 

Assign the value Hiren to the variable name.

 

PHP is dynamic type language. Dynamic type is also known as weakly typed language. Dynamic type means the data type for a variable does not require to declare. The data type determine by the variable as the value assign to the variable. If the value assign to the variable is string, then the data type of a variable is string. If a value assign to the variable is integer, then the variable data type is integer.

 

Data types in PHP

 

PHP has following six data types. Each data type is used to store a different type of data.

 

 

How to get data from user.

 

Let us write a program which enter his name and PHP welcomes him.

For that let us first create a HTML file.

<HTML>

<BODY>

<form action=”disp.php” method=”get”>

Enter your name:

<input type=”text” name=”user”>

<br>

<input type=”submit” name=”submit” value=”Submit”>

<input type=”reset” name=”reset” value=”Reset”>

</form>

</BODY>

</HTML>

     Save this file in C:/Wamp/WWW/PHP1 folder with input.html.

Also create a PHP file named disp.php and save it on C:/Wamp/WWW/PHP1.

   <HTML>

<BODY>

<h1> Welcome </h1>

<?php

$user  = $_GET[‘user’];

echo $user

?>

 

When you run it typing localhost/php1/input.html, it will display empty textbox. Enter name in it, I have enter my name Hiren in it.

 

 

$_GET is a superglobal variable/array. The superglobal variable is always available to PHP code for a page.

 

PHP has many superglobal variables. Following is the list of superglobal variables.

  • $GLOBALS
  • $_SERVER
  • $_GET
  • $_POST
  • $_COOKIE
  • $_FILES
  • $_ENV
  • $_REQUEST
  • $_SESSION

 

In the above example, we are using $_GET supervariable to assign the value entered by user. We use $_GET superglobal variable because the method defined in form is get. In case the method is post, then the code have to use $_POST superglobal variable.

 

When get method is used for a form, then .php page can be appended in the URL. The passed parameters are shown in the address bar of webpage. The parameters are shown as name=value pair. These parameters are appended after page name followed by ?name=value. Multiple parameters can be passed in the address bar. These name=value pair is separated by &. You can observe it in the previous program when it is running. In HTML file, we have defined name of textbox as user and name of submit button is submit. The username entered as “Hiren” and clicking on submit button, the address bar shows.

 

http://localhost/PHP1/prog1.php?user=Hiren&submit=Submit.

Post method of form does not append the value in the address bar.

 

Let us see by example.

 

<HTML>

<BODY>

<form method=”post” action=”postdemo.php”>

First Name:

<input type=”text” name=”fname”>

Last Name:

<input type=”text” name=”lname”>

<br><br>

<input type=”submit” name=”submit” value=”Submit”>

<input type=”reset” name=”reset” value=”Reset”>

</form>

</BODY>

</HTML>

 

In the above HTML form , the method is post.

 

<?php

$first = $_POST[‘fname’];

$last = $_POST[‘lname’];

echo “First Name: $first” ;

echo “<br>”;

echo “Last Name: $last”;;

?>

 

In the PHP code, the $_POST superglobal variable is used. So when the program runs, it will not display data the address bar of the browser as shown in the following figure.

 

 

If the html form does not specify method, it is always by default get.

 

Generally, when the data is sent to the server by using post method and when the data are get from server get method is used.

 

$_REQUEST superglobal variable can work for both methods (get and post) .However, due to limited functionalities compare to $_GET and $_POST, $_REQUIRE is less used.

you can view video on Installing WAMP and PHP Basics

References:

 

  • Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,
  • W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress.
  • Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.glass: Beginning PHP5, Apache, and MySQL Web Development, Wrox,
  • Robin Nixon: Learning PHP, MySQL, and JavaScript, O’Reilly Media.
  • Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP, Wrox.
  • Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible.
  • Joel Murach, Ray Harris: Murach’s PHP and MySQL, Shroff/Murach.
  • Ivan Bayross, Web Enabled Commercial Application Development Using HTML/Javascript/DHTML/PHP , BPB Publications.
  • Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams.
  • Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education.
  • http://www.php.net.
  • http://www.w3schools.com/
  • http://www.tutorialspoint.com/