29 December 2010

Php For Beginners (Lesson 1)

Definition
PHP: Hypertext Preprocessor is a widely used, general-purpose server side scripting language that was 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.

Installation
In order to run php on your own machine you need to install following three things:
  • Apache Server
  • Php
  • MySQL
But to make this lesson easier I will not discuss explicit installation of all the three things, rather I would tell you guys a simpler way of doing this. For this purpose we can simply use WAMP/XAMPP Server which is an all in one package of apache, php & MySQL.

WAMP Server can be downloaded from the link below:
http://sourceforge.net/projects/wampserver/files/WampServer%202/WampServer%202.0/WampServer2.0i.exe/download

Or you can download XAMPP Server for windows from the below mentioned link:
http://sourceforge.net/projects/xampp/files/XAMPP%20Windows/xampp-win32-1.7.3.exe/download

After downloading simply follow the steps to install WAMP/XAMPP server on your PC. Once the installation is complete you can test it by typing http://localhost/ in the explorer. This should open a default page for the WAMP/XAMPP Server. If the default page does not come up then try restarting the WAMP/XAMPP Server.

How to Start
Now that php is successfully installed on your PC, we need to know where you should type the php code. Well you can use Notepad, Dreamweaver or any other editor to write in the php code. I would recommend that you use Dreamweaver because it’s a lot easier to use.

After writing the code, you need to safe the file with a .php extension. For example you can save your file as test.php in the respective server folder.

In case of WAMP Server you need to save your php files in wamp > www directory. The exact path of this directory is:
C:\wamp\www\test.php

Whereas if you are using XAMPP Server then you need to safe your php files in the XAMPP > htdocs directory. The exact path should be:
C:\xampp\htdocs\test.php

Now you can run your php file by typing http://localhost/test.php in your browser.

Syntax
A Php code will always be encapsulated between "<?php " and "?>" each instruction will be terminated by semicolon (;).

Example
<?php
some php code....;
some php code....;
some php code....;
?>
Hello World Example
In php if you want to display something on the browser’s screen, you need to use “echo” function.

Syntax
echo “anything you like to display on the screen”;

Example
<?php 
echo "Hello World!";
?> 
Ok guys that is all for the first lesson now you should practice and play with this code to get used to with the php syntax.

References
http://www.wikipedia.com
http://www.w3schools.com 


Next Lesson next

No comments: