PHP: "Hello World"



"Hello world." is the first program most beginning programmers will learn to write in any given language. Here are four examples of the syntax for "Hello world!" in PHP.


Code:

<?php

$string = "Hello world!";

print $string;

// You could also use

echo $string;

//Or

echo 'Hello world!';

// Or, if you are familiar with C, printf can be used too.

printf("%s", $string);

?>

Now, you can see the following output where "Hello World!" is printed 4 times once from each command. Note, that each sentence in php ends with a semi-colon(;). It tells the php interpreter that the command has ended and can be executed.


Output:

Hello world!Hello world!Hello world!Hello world!

 

Notice that all the code in php is enclosed in <?php and ?>. This makes it easier to separate the php code from HTML when emmeded in it. The following example shows how php can be embbed in HTML to print "Hello World"!.

 

<html>

<head>

<title> Hello World!</title>

</head>

<body>

<?php echo "Hello World!";?>

</body>

</html>

 

Notice, how php code is embeded in html. Echo prints the command on the browser.

Output:

Hello world!