Pages

Showing posts with label PHP Basic. Show all posts
Showing posts with label PHP Basic. Show all posts

Basic PHP Crash Course (part 7)

Saturday, May 12, 2012
In this post, we discuss how to retrieve or select the data from MySQL database to view our pizza order.
To do so we should create admin folder. So that we can prevent the customer from viewing our admin data.

Create a folder named admin in our pizza_shop folder.

Open the new document in your favourite editor or Notepad and type below code.


<html>
<head>
 <title>Amin Pannel</title>
</head>
<body>
<?php
 $con = mysqli_connect('localhost','root','','pizza') or die(mysqli_error());
 $sql="SELECT * FROM `order`";
 $result = mysqli_query($con,$sql) or die(mysqli_error());
 echo "<table width='100%'>";
  echo "<tr bgcolor='#FC0'>".
    "<td width='40%'>Customer Name</td>".
    "<td width='40%'>Address</td>".
    "<td width='20%' align='right'>Quantity</td>".
    "</tr>";
 while($row=mysqli_fetch_array($result)){
  echo "<tr valign='top'>".
    "<td width='40%'>".$row['name']."</td>".
    "<td width='40%'>".$row['address']."</td>".
    "<td width='20%' align='right'>".$row['quantity']."</td>".
    "</tr>";
 }#end of while loop
 echo "</table>";
?>
</body>
</html>

Save above file as index.php in our admin folder. Now if run this address http://localhost/pizza_shop/admin/ from your browser you will see like below.

Explanation

Our sql query is
"SELECT * FROM `order`"
* means all. This query will retrieve all data from our order table of pizza database.


If you familiar with programming, you can follow the code showed above picture. In this code I use while loop to show the order data.

Looping

In every programming, looping is an essential thing. In PHP, there are four kinds of looping. They are while, dowhile, for and foreach. You can use for loop if you know the looping time exactly. You can use dowhile if you want to execute at least once. If you don't know the looping time exactly, you can use while loop. The foreach loop is specially designed for use with arrays.

While Loop

This loop is simplest loop in PHP. It look like if statement. As long as the condition is true, it will execute the block repeatedly.

For our code as long as our order rows exist in our order table of the pizza database, the while loop will show the order rows.
mysqli_fetch_array($result) function will fetch the order table row one by one as an array. If there is no more rows, it will return NULL. When NULL return, while loop will stop.
Read more ...

Basic PHP Crash Course (part 6)

Thursday, April 19, 2012
In the early posts, we do not save the order data. To deliver the user order, we have to save the order data permanently. To do so we will need suitable database server. I will use MySQL.

What is MySQL?

MySQL is a Relational Database Management System. It can store, search, sort and retrieve data efficiently. You can use it freely under the GPL license. You can check more about MySQL in its official website www.mysql.com

What do we need?

In this series, we will learn about MySQL by using phpMyAdmin. It will make you more easy to create a database by using phpMyAdmin than command line because it is a GUI(Graphical User Interfaces) system. To use phpMyAdmin you don't need to install anything if you have installed xampp or wamp as mentioned in this post.
Type this address "http://localhost/phpmyadmin/" in your browser address bar. You will see like below screenshot.




Creating our Database

Click Databases from the menu.


Type in the text box as pizza for the database name and click "Create" button.


You will see your database name in the left menu like below screenshot.


Click your database name. You will see like below screenshot.



Type order in the Name field and type 4 in the Number of columns field like below screenshot. And then click Go button to crate a database table.


Now you need to fill the field name, type etc. like below screenshot.



A database table should have a primary key. So for our order table id field should be the primary key. Select in the Index select box as PRIMARY and set the A_I(AUTO_INCREMENT) check box that will increase automatically our id field number.


Then click Save button.


Now you have created a database table to save your customer order.

PHP and MySQL

It is time to connect the MySQL database by using PHP.
Firstly I want to introduce you to the PHP built-in functions that can connect to the MySQL.

mysqli_connect("hostname","username","password","databasename")

Above function will help you to connect the MySQL database. So you need to know the parameter values.
hostname - Hostname running database server. Default is localhsot. For our xampp or wamp is also localhost.
username - Our xampp or wamp default username is root.
password - Our xampp or wamp default password is blank(no password).
databasename - Our database name is pizza.

You need to add below code to our process.php.

<?php
$con = mysqli_connect('localhost','root','','pizza') or die(mysqli_error());
$sql="INSERT INTO `order` (`name`, `address`, `quantity`) VALUES ('$cus_name', '$address', '$quantity')";
mysqli_query($con,$sql) or die(mysqli_error());
?>


First line will connect to our MySQL sever and it will also return a object which represents the connection to a MySQL Server or FALSE if the connection failed. We need to use this return object in other php function.
In second line, I create a variable of SQL statement that will use in the query function. We can write this SQL statement in the query function directly but we do so for the simplicity. This SQL statement will insert the post data from user order to the order table of our pizza database.
The third line will run our SQL Query.
Our process.php file will be like below.

<?php
 $title = "Pizza Shop: Order Process";
 include('header.php');
 include('sidebar.php');
?>
<div id="content">
<?php
 if(isset($_POST['submit']))
 {
  if( $_POST['cus_name'] != '' && $_POST['quantity'] != '' && $_POST['address'] !='' )
  {
   $cus_name = $_POST['cus_name'];
   $quantity = $_POST['quantity'];
   $address = $_POST['address'];

   $con = mysqli_connect('localhost','root','','pizza') or die(mysqli_error());
   $sql="INSERT INTO `order` (`name`, `address`, `quantity`) VALUES ('$cus_name', '$address', '$quantity')";
   mysqli_query($con,$sql) or die(mysqli_error());
?>
 <p>Thank <?php echo $cus_name; ?> !</p>
 <p>You order <?php echo $quantity; ?> pizza.</p>
 <?php
  $total = $quantity * 10;
 ?>
 <p>It will cost you <?php echo $total; ?> $.</p>
 <p>We will send withing 2 hours to <?php echo $address; ?>.</p>
 <?php
 }
 else
 {
  $_SESSION['error'] = "You need to fill all data";
  header("location: index.php");
 }
 ?>
<?php } ?>
</div><!--end of content -->
<?php
 include('footer.php');
?>
Read more ...

Basic PHP Crash Course (part 5)

Monday, April 9, 2012
This post is part 5 of the Basic PHP Crash Course. If you have never red before this Crash Course, you should read part 1, part 2, part 3 and part 4 first. In this post, we will discuss about our site design or template. For a website it will has header, sidebar, content and footer. So we need to add some html code to our index.php. Following code is our index.php.
<?php session_start(); ?>
<html>
<head>
 <link href="styles.css" rel="stylesheet" type="text/css">
 <title>Pizza Shop: Home</title>
</head>
<body>
 <div id="wrapper">
 <div id="header">
  <h1>Pizza Shop</h1>
 </div><!--end of header -->

 <div id="sidebar">
  <h2>Sidebar</h2>
 </div><!--end of sidebar -->

 <div id="content">
 <?php 
  if($_SESSION['error'] != '')
  {
   echo $_SESSION['error']; 
   $_SESSION['error']='';
  }
 ?>
 <h3>Pizza Shop Order Form</h3>
 <form class="order" action="process.php" method="post">
  <p>
  <label for="cus_name">Customer Name:</label>
  <input type="text" name="cus_name" />
  </p>
  <p>
  <label for="address">Shipping Address:</label>
  <input type="text" name="address" />
  </p>
  <p>
  <label for="quantity">Pizza Quantity:</label>
  <input type="text" name="quantity" />
  </p>
  <p>
  <input type="submit" name="submit" value="Submit Order" />
  </p>
 </form>
 </div><!--end of content -->

 <div id="footer">
  Pizza Shop &copy; 2011
 </div><!--end of footer -->

 </div><!--end of wrapper -->
</body>
</html>
I also create a css file named styles.css like following.
body{
 margin:0px;
 padding:0px;
}
#wrapper{
 width:960px;
 margin:0 auto;
 background-color:#FFC;
}
#header{
 height:90px;
 background-color: #FC0;
}
#header h1{
 color:#FFF;
 padding:10px 0px 0px 10px;
}
#content{
 padding:20px;
 float:left;
}
.order label{
 width:150px;
 float:left;
}
#sidebar{
 width:250px;
 height:300px;
 background-color: #C00;
 float:left;
}
#footer{
 clear:both;
 height:40px;
 background-color:#FC0;
 text-align:center;
}
Now if you run our site, you will see like below.
We have a problem. For our process.php we also need to add the html code that added to our index.php. You can add the needed code to that page. But if you have many files, you need to add every page and if you want to change some code, you will change many files. PHP has a very useful statement include() to solve this problem. It will include and evaluate the specific file. Before we use the include() statement, we will separate the duplicated coed as the separated file. Type the following code and save as header.php.
<?php session_start(); ?>
<html>
<head>
 <link href="styles.css" rel="stylesheet" type="text/css">
 <title>Pizza Shop: Home</title>
</head>
<body>
 <div id="wrapper">

 <div id="header">
  <h1>Pizza Shop</h1>
 </div><!--end of header -->
Type the following code and save as sidebar.php.
<div id="sidebar">
 <h2>Sidebar</h2>
</div><!--end of sidebar -->
Type the following code and save as footer.php.
<div id="footer">
 Pizza Shop &copy; 2011
</div><!--end of footer -->

</div><!--end of wrapper -->
</body>
</html>
Now our index.php will be like below by using include() statement.
<?php
 include('header.php');
 include('sidebar.php');
?>
<div id="content">
<?php 
 if($_SESSION['error'] != '')
 {
  echo $_SESSION['error']; 
  $_SESSION['error']='';
 }
?>
<h3>Pizza Shop Order Form</h3>
<form class="order" action="process.php" method="post">
 <p>
 <label for="cus_name">Customer Name:</label>
 <input type="text" name="cus_name" />
 </p>
 <p>
 <label for="address">Shipping Address:</label>
 <input type="text" name="address" />
 </p>
 <p>
 <label for="quantity">Pizza Quantity:</label>
 <input type="text" name="quantity" />
 </p>
 <p>
 <input type="submit" name="submit" value="Submit Order" />
 </p>
</form>
</div><!--end of content -->
<?php
 include('footer.php');
?>
Our process.php file will be like below.
<?php
 include('header.php');
 include('sidebar.php');
?>
<div id="content">
<?php
 if(isset($_POST['submit']))
 {
  if( $_POST['cus_name'] != '' && $_POST['quantity'] != '' && $_POST['address'] !='' )
  {
   $cus_name = $_POST['cus_name'];
   $quantity = $_POST['quantity'];
   $address = $_POST['address'];
?>
<p>Thank <?php echo $cus_name; ?> !</p>
<p>You order <?php echo $quantity; ?> pizza.</p>
<?php
$total = $quantity * 10;
?>
<p>It will cost you <?php echo $total; ?> $.</p>
<p>We will send withing 2 hours to <?php echo $address; ?>.</p>
<?php
  }
  else
  {
   $_SESSION['error'] = "You need to fill all data";
   header("location: index.php");
  }
?>
<?php } ?>
</div><!--end of content -->
<?php
 include('footer.php');
?>
Our template system is almost finished. Let's think about our page title. It is not flexible. I want to change my title when the page change. To do so we need to create a variable and set our title to this variable before loading the header.php. And then we can use this variable in our header.php. Below is our index.php.
<?php
 $title = "Pizza Shop: Home";
 include('header.php');
 include('sidebar.php');
?>
...
Our process.php file will be like below.
<?php
 $title = "Pizza Shop: Order Process";
 include('header.php');
 include('sidebar.php');
?>
...
Our header.php will be like below.
<?php session_start(); ?>
<html>
<head>
 <link href="styles.css" rel="stylesheet" type="text/css">
 <title><?php echo $title; ?></title>
</head>
<body>
 <div id="wrapper">

 <div id="header">
  <h1>Pizza Shop</h1>
 </div><!--end of header -->
Read more ...

Basic PHP Crash Course (part 4)

Tuesday, April 3, 2012
This post is part 4 of the Basic PHP Crash Course. If you have never red before this Crash Course, you should read part 1, part 2 and part 3 first. In this post we will discuss about PHP Session. It is very useful when you write a web application. Let's think about our pizza shop website. After the user have submitted the order form, our application will call the process.php. Then I want to show the order form again, if the submitted form has the errors. To do so, we have a problem. We cannot know the error data from process.php when we show the order form again. So we must use PHP Session.

What is PHP Session?

Session is a way to store information for the individual user in our server by using session ID. This ID is automatically generated by PHP and store also on the user computer for the lifetime of a session. The content of the session data is store at the server.

Simple Example

<?php session_start(); ?>
<html>
<head>
 <title>Page 1</title>
</head>
<body>
<?php
 $_SESSION['user_name'] = "John";
?>
</body>
</html>
Before we use the session data, we need to load session_start() function. This function will check whether there is already a session. If not, it will create one. Now we can use the superglobal $_SESSION array. So I set the user_name as John. Below is the another page.
<?php session_start(); ?>
<html>
<head>
 <title>Page 2</title>
</head>
<body>
<?php
 echo "Hi ".$_SESSION['user_name'];
?>
</body>
</html>
In this page, we echo the user_name from session data array. If you run the page2, you will see "Hi John". In this way, you can save the data you need for other page. However session data is not permanent storage. For permanent storage, we will use the database like MySql(I will also explain about MySql database later in this series.).

Enhancing our pizza shop website

Now it is time to enhance our pizza shop site. Below is our index.php.
<?php session_start(); ?>
<html>
<head>
 <title>Order Process</title>
</head>
<body>
<?php
 if(isset($_POST['submit']))
 {
  if( $_POST['cus_name'] != '' && $_POST['quantity'] != '' && $_POST['address'] !='' )
  {
   $cus_name = $_POST['cus_name'];
   $quantity = $_POST['quantity'];
   $address = $_POST['address'];
?>
 <p>Thank <?php echo $cus_name; ?> !</p>
 <p>You order <?php echo $quantity; ?> pizza.</p>
<?php
 $total = $quantity * 10;
?>
 <p>It will cost you <?php echo $total; ?> $.</p>
 <p>We will send withing 2 hours to <?php echo $address; ?>.</p>
<?php
 }
 else
 {
  $_SESSION['error'] = "You need to fill all data.";
  header("location: index.php");
 }
?>
<?php } ?>
</body>
</html>
As you know, firstly we need to call the session_start() function to use the session data. If the user submit the form without filling all data, we will set the session error data as "You need to fill all data.". And then redirect our site to index.php by using header() function. This function will tell the browser to load the page that you send as parameter. In our index.php file, we will also start with session_start() function. Then we will retrieve the error data from the session array and will echo before our order form. Below is our index.php.
<?php session_start(); ?>
<html>
<head>
 <title>Pizzs Show: Home</title>
</head>
<body>
<?php 
 if($_SESSION['error'] != '')
 {
  echo $_SESSION['error']; 
  $_SESSION['error']='';
 }
?>
 <h3>Pizza Shop Order Form</h3>
 <form action="process.php" method="post">
 <p>
  <label for="cus_name">Customer Name:</label>
  <input type="text" name="cus_name" />
 </p>
 <p>
  <label for="address">Shipping Address:</label>
  <input type="text" name="address" />
 </p>
 <p>
  <label for="quantity">Pizza Quantity:</label>
  <input type="text" name="quantity" />
 </p>
 <p>
  <input type="submit" name="submit" value="Submit Order" />
 </p>
 </form>
</body>
</html>
Read more ...

Basic PHP Crash Course (part 3)

Thursday, March 29, 2012
This post is part 3 of the Basic PHP Crash Course. If you have never red before this Crash Course, you should read part 1 and part 2 first. Before enhancing our site, I want to explain you about the function.

What is a function?

A function is a block of code that has a name. It is not executed when we load the page. We need to call its name when we need to execute that block of code. And then we can call any time whenever we need it. If you have some code that need to use many time, you should use as the function. The function name can start with a letter or underscore (not a number) To understand the idea of function, we need to create our own simple function.

Simple Function

Let's start with the hello world function.
<?php
  function hello()
  {
   echo "Hello World!";
  }
hello();
?>

Function with parameter

Parameter is a variable that we send to our function to use withing our function.
<?php
  function hello($name)
  {
   echo "Hello ".$name;
  }
hello("Jack");
?>

Function with return value

Return value is a variable that return from our function. Sometime we need a return value
<?php
function add($a,$b)
{
 $result = $a + $b;
 return $result;
}
$total = add(5,3);
echo $total;
echo add(3,5);
?>
We can use the return value like above example and like int our web site.

Enhancing our site

There is no need to change in our index.php. For our process.php, we need to test whether the user fill the form data or not. To determine whether a variable is set, we will use isset() function. In PHP, there are many built-in functions. I create a tutorial about this here. Following is our new process.php
<html>
<head>
 <title>Order Process</title>
</head>
<body>
<?php
if($_POST['submit']=='Submit Order')
{
 if( isset($_POST['cus_name']) && isset($_POST['quantity']) && isset($_POST['address']) )
 {
  $cus_name = $_POST['cus_name'];
  $quantity = $_POST['quantity'];
  $address = $_POST['address'];
 ?>
 <p>Thank <?php echo $cus_name; ?> !</p>
 <p>You order <?php echo $quantity; ?> pizza.</p>
 <?php
  $total = $quantity * 10;
 ?>
 <p>It will cost you <?php echo $total; ?> $.</p>
 <p>We will send withing 2 hours to <?php echo $address; ?>.</p>
 <?php
    } 
 else
  echo "You need to fill all data";
 ?> 
 <?php } ?>
</body>
</html>

Explanation

Let's talk about isset() function. We will pass at least one parameter and it will return boolean true or false. It will return TRUE if var exists; FALSE otherwise.

Logical Operators

If you need to combine the results of logical conditions, you must be use logical operators. Following table is PHP logical operators.
!NOT
&&AND
||OR
andAND
orOR
xorXOR
Read more ...

Basic PHP Crash Course (part 2)

Sunday, March 25, 2012
This post is part 2 of the Basic PHP Crash Course. In part 1, we discuss about the html form, php variable, post method and some operator. In this part, we will discuss about php if() statement and comparison operators by enhancing our pizza shop site.

We created two file index.php and process.php in part 1.
There is no need to change in our index.php.

For our process.php,
we should know whether the form is submitted or not before we process the form. If the user run this url "http://localhost/pizza_shop/process.php" directly, user can get error.
So we have to change in our process.php. Our new process.php is as follow.


<html>
<head>
 <title>Order Process</title>
</head>
<body>
 <?php
 if($_POST['submit']=='Submit Order')
 {
  $cus_name = $_POST['cus_name'];
  $quantity = $_POST['quantity'];
  $address = $_POST['address'];
 ?>
 <p>Thank <?php echo $cus_name; ?> !</p>
 <p>You order <?php echo $quantity; ?> pizza.</p>
 <?php
  $total = $quantity * 10;
 ?>
 <p>It will cost you <?php echo $total; ?> $.</p>
 <p>We will send withing 2 hours to <?php echo $address; ?>.</p>
 <?php } ?>
</body>
</html>

Explanation

Sometime we need to do some action depend on other condition. For this saturation we will use if statement. Below is the idea of the "if" conditional statement.

if

if (this condition is true)
this action will be execute

if...else

if (condition one is true)
action one will be execute
else
action two will be execute

if...elseif...else

if (condition one is true)
action one will be execute
elseif (condition two is true)
action two will be execute
.
.
.
else
last action will be execute

Examples

Below is syntax of the "if" conditional statements.

if


<?php
 $a = 5;
 $b = 3;
 if($a > $b)
  echo "a is greater than b";
?>

if...else


<?php
 $a = 5;
 $b = 7;
 if($a > $b)
  echo "a is greater than b";
 else
  echo "a is less than b"; 
?>

if...elseif...else


<?php
 $a = 5;
 $b = 5;
 if($a > $b)
  echo "a is greater than b";
 elseif($a < $b)
  echo "a is less than b"; 
 else
  echo "a is equal to b";
?>

Note: If your action code is more than one statement, you must use the curly braces {}.

Comparison Operators

PHP has the following comparison operators.
==is equal to
===is identical
!=is not equal
!==is not identical
<>is not equal
>greater than
<less than
>=greater than or equal
<=less than or equal
Read more ...

Basic PHP Crash Course (part 1)

Thursday, March 22, 2012
I want to create a tutorial serie for the PHP beginner. So I have been writing this Crash Course. I think it is more interesting to learn PHP by creating the real website.

What do you need to know to learn this PHP Crash Course?

You have already know about HTML and CSS. It is more good if you know about JavaScript.

Have you ever run php script in localhost? If you haven't, read this tutorial first.

I believe that you have already known about the HTML form.

Let's start with very little web application. Assuming that we sell the pizza. So create a folder below your c:xampphtdocs folder and name it pizza_shop. And then open a new document in your favourite editor. Type below code in your new document and save as index.php in your pizza_shop folder.

<html>
<head>
 <title>Pizzs Show: Home</title>
</head>
<body>
 <h3>Pizza Shop Order Form</h3>
 <form action="process.php" method="post">
  <p>
   <label for="cus_name">Customer Name:</label>
   <input type="text" name="cus_name" />
  </p>
  <p>
   <label for="address">Shipping Address:</label>
   <input type="text" name="address" />
  </p>
  <p>
   <label for="quantity">Pizza Quantity:</label>
   <input type="text" name="quantity" />
  </p>
  <p>
   <input type="submit" name="submit" value="Submit Order" />
  </p>
 </form>
</body>
</html>

Run your browser and check this address http://localhost/pizza_shop
You will see like below screenshot.


Why? When you run your site, index.php will be called as default.

When the customer fills and submits the form, process.php page will be called because you set in the form action attribute to "process.php". The process.php file will get the form variables by using POST method because you set in from method attribute to "post". I will also explain about post method later in this post.

Now we need to create process.php. Type below code in your new document and save as process.php in your pizza_shop folder.

<html>
 <head>
  <title>Order Process</title>
 </head>
<body>
 <?php
  $cus_name = $_POST['cus_name'];
  $quantity = $_POST['quantity'];
  $address = $_POST['address'];
 ?>
 <p>Thank <?php echo $cus_name; ?> !</p>
 <p>You order <?php echo $quantity; ?> pizza.</p>
 <?php
  $total = $quantity * 10;
 ?>
 <p>It will cost you <?php echo $total; ?> $.</p>
 <p>We will send withing 2 hours to <?php echo $address; ?>.</p>
</body>
</html>

Explanation


Variable

Let me introduce you to PHP variable to understand above code.

Variable is look like a box. You can store values like string, number, object etc. And then we can reuse this variable through our code.

Have you noticed that every variable start with dollar sign($) in our site?

All variable name must start with dollar sign($) and can contain alphabetic character, under score and number. But it cannot start with number. Check below example.

$string = "Hello World!";  //Valid
$string2 = 'Hi everyone!'; //Valid

$_number = 3;   //Valid
$number = 2.2;  //valid

$flag = true;   //valid

$1number = 5;       //Invalid
$2string = "Second String"; //Invalid

The first two are assigned string. If you want to assign string value you must be use double quotes or single quotes.

The second two are integer and float numbers.

The third one is boolean and you don't need double quotes or single quotes to assign boolean value.

Last two is invalid because they start with the number.

You don't need to declare the variable type before use like C. In PHP, the type of variable is determined by the value assigned to it.
Variable names are case sensitive.

POST

There are two types to access the form variables from one page to another. They are POST and GET that you set in your html form method. They have their own usefulness. In this post I will tell you only about POST.

$_POST is the superglobal variable which contain all POST data. It is an associated array of variables passed to the current script via the HTTP POST method. They are automatically set for you by PHP.
You can access the each form field as PHP variable whose name relates to the name of the form.

Operator

We also use two operators in our pizza site. They are assignment operator equal(=) and multiplication (*).
There are many operators in PHP like other languages. I want to introduce you some operators for this post.

Assignment(=) e.g $number = 1;

Mathematical Operators
Addition (+) e.g $total = 1+1;
Subtraction (-) e.g $result = 2-1;
Division (/) e.g $result = 5/2; //result will be 2.5
Modulus (%) e.g $result = 5%2; //result will be 1
Read more ...

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