Pages

How to start with PHP?

Saturday, March 10, 2012

What is PHP?

PHP stands for Hypertext Preprocessor and it is a server-side scripting language for web development. Server-side means PHP code execute on the server and the plain HTML result is sent to the browser. Script means it does not need to compile to run. PHP can also be embedded into HTML.

PHP can create the very awsome website and web application. And then you can easily learn PHP if you familiar with programming like C.

What do we need to run PHP in localhost?

1. web server(Apache, IIS, etc.)
2. PHP (http://www.php.net/downloads.php)
3. MySQL (http://www.mysql.com/downloads/)

You can install these 3 programs one by one. You can get a lot of headache to do so. Today everything is easy to use. There are many packages that contain everything we need.

For Windows - WAMP, XAMPP, EasyPHP, AMPPS
For Mac - XAMPP, MAMP
For Linux - XAMPP, LAMP
For Solaris - XAMPP

These packages make your installation a breeze.

XAMPP

As for me, I use XAMPP server so I want to introduce you little about XAMPP. XAMPP can run all major Operating System.

Installation

1. Go to the XAMPP home page from this link.
2. Choose your Operating System. As for me, I choose XAMPP for Windows.
3. Download the Installer.
4. Run your downloaded Installer.

That's all. Now you are ready to write your php website.

Running your Server

Run your XAMPP Control Panel from your Desktop or start menu.

If you haven't run your Apache, click check box and start running like above screenshot.

Open you browser and run this address http://localhost/ and choose your language. You will see like below screenshot.

Congratulations:
You have successfully installed XAMPP on this system!

Writing PHP

Let's say hello to the world.
Open Nodepad. The best way to learn code is to code. So type below code in your Nodepad.

<?php
 echo "Hello World!";
?>

Save this file in your c:xampphtdocs folder as hello.php. You will need to change in "Save as type:" to "All Files" when you save your Nodepad file like below screenshot.


Run your browser and check this address http://localhost/hello.php
You will see "Hello World!" on you browser window.

How does it work?

As you have seen above code, our code started with <?php and ended with ?>. This tells your server to parse the information between them as PHP. It has three forms like below. You can use either of the options.


<?
PHP Code In Here
?>

<?php
PHP Code In Here
php?>

<script language="php">
PHP Code In Here
</script>


The echo function will display the text between double code on the browser. Have you found a semicolon at the end of the statement? Each PHP statement must end with a semicolon.

Embedding withing HTML

As you know, we are creating a website so you need to embed the PHP code withing HTML like below.


<html>
<head>
<title>My first PHP site</title>
</head>

<body>
<?php
 echo "Hello World!";
?>
</body>
</html>

If you encounter any problem, don't hesitate to ask me.

3 comments: