Hello World In Php
"Hello world." is the first program most beginning programmers will learn to write in any given language. Here is an example of how to print "Hello world!" in PHP.
CODE/PROGRAM/EXAMPLE
<?php
echo "Hello world!";
?>
//Output: Hello world!
This is as basic as PHP gets. Three simple lines, the first line identifies that everything beyond the <?php tag, until the ?> tag, is PHP code. The second line causes the greeting to be printed (or echoed) to the web page. This next example is slightly more complex and uses variables.
Hello World With Variables This example stores the string “Hello world!” in a variable called $string. The following lines show various ways to display the variable $string to the screen.
CODE/PROGRAM/EXAMPLE
//PHP Code:
<?php
// Declare the variable ‘string’ and assign it a value.
// The <br /> is the HTML equivalent to a new line.
$string = ‘Hello world!<br />’;
// You can echo the variable, similar to the way you would echo a string.
echo $string;
// You could also use print.
print $string;
// Or, if you are familiar with C, printf can be used too.
printf(‘%s’, $string);
?>
//PHP Output: Hello world!<br />Hello world!<br />Hello world!<br />
HTML Render:
Hello world!
Hello world!
Hello world!
The above example contained two outputs. PHP can output HTML that your browser will format and display. The PHP Output box is the exact PHP output. The HTML Render box is approximately how your browser would display that output. Don't let this confuse you, this is just to let you know that PHP can output HTML. We will cover this much more in depth later.