The Basic of PHP
Date: Wednesday, May 22 2002
Topic: Programming


PHP stands for the Hypertext Preprocessor. PHP is embedded in HTML files and is extremely easy to learn and code. PHP is a server side scripting language, that is, the server processes the PHP code and sends the output to you. Thus, it is not possible to view the source of the PHP code. PHP code cannot be run on your computer unless you are running a server, download PHP Triad so you can test your PHP code on your computer.

PHP syntax is very much familiar to C/C++ syntax and should be easy to pick up if you have a sound background in C or C++. PHP code can be included anywhere in your web-page, much like Javascript. Now, let us make our first PHP page which displays the customary "Hello World" text. First, you must know that all PHP code must be included between <? and ?> tags. The basic function to print text to the screen is ECHO even though there are other functions such as printf which we will discuss later on. Here is the code for our first PHP page ?

<HTML>

<HEAD>

<TITLE> My first PHP program! </TITLE>

</HEAD>

<BODY>

<?

Echo "Hello World!";

?>

<BODY>

</HTML>

Nearly all PHP commands end with a ; except "if", "for" and some other functions, just like in C. Remember the text you want to be printed must be included between the " and he ". An article I read states that it is better to use <?php instead of only <? Because languages like XML may confuse <? with one of their tags. Now, let us learn how to use variables, they do not have to be declared, just like in BASIC and Perl. Variables in PHP begin with a $ sign, for example $nikhil. Variables in PHP are of a single type, unlike in C or other languages, that means you can store both numbers and characters in a variable. This is how you create a variable ?

$nikhil = "Hello Nikhil";

Lets integrate this into our page ?

<HEAD>

<TITLE> My first PHP program! </TITLE>

</HEAD>

<BODY>

<?

$nikhil = "Hello Nikhil";

$number = 350;

Echo $nikhil;

Echo $number;

?>

<BODY>

</HTML>

Nice huh? The output of this program would be "Hello Nikhil350". Note, that there is no need to use "" when assigning integer values to a variable. Remember that variables in PHP are case-sensitive, that means, $nikhil and $NIKHIL are completely different. Now lets learn how to do some basic math in PHP. Firstly to add we use +, to subtract we use -, to divide we use /, to multiply we use *. We will now create a page which demonstrates all of these functions ?

<HEAD>

<TITLE> My first PHP program! </TITLE>

</HEAD>

<BODY>

<?

$one = 4;

$two = 2;

echo one + two;

echo "<br>";

echo one - two;

echo "<br>";

echo one * two;

echo "<br>";

echo one / two;

echo "<br>";

?>

<BODY>

</HTML>

The echo "<br"> line makes PHP leave a line. Notice that if I used -

echo "one + two<br>";

The output would be "one + two" and not the number six. The final output of the page we coded above will be ?

6

2

8

2

That?s all for The Basics of PHP, watch out though, I?ll be writing another tutorial on PHP which will discuss Loops, Files and Advanced Math




This article comes from osforge.com
http://www.osforge.com

The URL for this story is:
http://www.osforge.com/news/00767.html