PHP tutorial - Write php script and defining variables


Write Php Script And Defining Variables

As I had said previously in the introduction page, php script has a special tags that is used to tell where php script begin and where does it ends. The special tags is <?php and ?> for opening and closing delimiters!

<?php

PHP script is located here

?>

All of your php codes should be placed within this tags or else it will be treated as a common HTML codes.

In order for you to display a text or whatever calculation result within the browser for a visitor to see, you need to use 'echo' or 'print' command. Take a look at the simple script below to prints out the word 'Hello there!'

<?php

echo "Hello there!";

?>

When this script is executed, you will see the word 'Hello there!' in your browser. The double quotes are for non-variables and numeric while the ; tells php that it is the end of one command. Give it a try and you will find that php is pretty simple language to learn.

In php, it is possible for you to define your own variables. Now we are going to define variables with a simple calculations

<?php

$revenue = 27000;
$expenses = 20000;
$income = $revenue - $expenses;
echo "My monthly income is " .$income;

?>

In this php code, there are 3 variables defined. First is the revenue, second is expenses and the last one is the calculation on finding the net income. Thus, here is what it does!

Firstly, PHP will tend to read those variables and store it within the server. After reading the first line, PHP knows that variable revenue has the value of 27000. Then it continues to second line. Now it get another variable. Since there still no calculation command founded, it store those variable on the server for later use. It had collect 2 variables and then continue to the third line. Oh .... it is a calculation. A new variable is again defined by substracting the value of $revenue and $expenses. So, PHP takes those variables which had already defined previously for this calculation and store the new one on the server as well. Now, it moves to the next line. It has the echo command. So the result will be shown like this:

My monthly income is 7000

Notice on how the echo command is written. First, it has the double quotes to wrap the text and a single dot ' . ' known as concatenation which tells PHP to combine calculation result stored in the $income variable into a string.

Try this

Write me a script to calculate the area of a land which has a length of 120m and 60m width. If thi皜 land cost me $20,000 how much is 1m square worth at the end of the year if it has the growth potential of 10%.

**The operator is:
+ is addition
* is multiplication
- is substraction
/ is division
% is modulus

  

Optimize Website - Webmasters optimize their site and gain higher search engine ranking

Webmaster tools - Free online webmaster toolkits, website reporting and free PHP opensource scripts.

Free Photo Hosting - Free File Hosting, Free ZIP file Hosting, Free Image Hosting, Free MP3 Hosting.

Funny Videos, Stupid Videos

Stupid Videos - Custom Pimped Motorcycle Pictures, Videos, and Articles

Planet Diaz - Programming Community - Online programming resource for programmers all around the world

Forectory - Webmaster Directory

St Louis Computer Networking

Website Templates - 1000s of Templates $5 - Many Templates to help you build your site!

 


Each and every tutorial published in PHPhelps.com is originally written by PHPHELPS OWNERS. None of the content is allowed to be replicated without prior notice to writter.
Copyright © 2006 PHPhelps.com