Pages

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

5 comments:

  1. nice details
    we are also offering 6 Month Summer Training in various software language
    visit to http://issjaipur.com/Training/PHP_training.aspx for more details.

    ReplyDelete
  2. Superb blog I visit this blog it's extremely marvelous. Interestingly, in this blog content composed plainly and justifiable. The substance of data is exceptionally instructive.
    oracle fusion financials classroom training
    Workday HCM Online Training
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training
    Oracle Fusion HCM Classroom Training

    ReplyDelete
  3. Superb blog I visit this blog it's extremely marvelous. Interestingly, in this blog content composed plainly and justifiable. The substance of data is exceptionally instructive.
    oracle fusion financials classroom training
    Workday HCM Online Training
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training
    Oracle Fusion HCM Classroom Training

    ReplyDelete