Simple Hello world program using PHP

0

As we all know this programming language is almost similar to C and C++. The only difference is C and C++ works on any platform and PHP is a server side scripting and works on only servers that can compile PHP on both windows and Linux base operating systems. A php compiler only can read a file with .php extension.  So we need to save all the script files with .php extension. If we go through the below links we can understand the difference between a traditional high level language and php language.

  1. Basics of PHP language
  2. Basics of C & C++

On this article we will show a hello world program with basic structure of PHP scripting.

A php script starts with a special set of characters which is “<?php”. This set of characters means that the scripting start from here and the scripting ends with “?>” set of characters. The complier reads the scripts in between these two character set. It looks like below.

<?php

All the scripting should be in here.

?>

If we want to print hello world on our web browser then there are few pseudo codes we can use to show this.

  1. Echo
  2. Print
  3. Print_r

Echo usually just prints the character or characters in the double quotes. Also we can show the content of a variable using this pseudo code.  We can write this like below.

$a=”Hello world”;

echo $a.

Or

echo “Hello world”;

If we need we can also use print. Print is same as echo. We just have to replace echo with print. It seems as below.

Print “Hellow world”;

Or print “$a”;

These are the simple way to print contents in PHP. There is also another pseudo code which works same.

Print_r();

Now the last one is a bit different than the previous two. The last one usually prints an array in a formatted manner.

Let’s see how the program seems as a whole.

<?php

$a=”Hello world”;

echo $a . “\n”;

echo “Hello world \n”;

print “Hello world\n”;

print “$a\n” ;

?>

Suppose we want to use print_r function then we have to declare an array, then we can print an array using this function.

$b=(“me”,”you”,”him”, “her”);

Print_r($b);

The output will be.

[0] => me

[1] => you

[2] => him

[3] => her

These are the simple output generating pseudo codes.

In php program we have to understand the architecture of the program and how it works only then we can make a simple and easier program using this language.

On my next article I will try to describe about the different loops in this language.

 

Leave A Reply

Your email address will not be published.