Hello World! In several different ways!
Now for that ever coveted Hello World! script. Time to learn some PHP! All sections of PHP code begins and ends like this:
begin
<?PHP
end
?>
All your PHP code will be contained within those brackets. Unlike with HTML, syntax is fairly strict. This means you can't just type anything you want between those brackets and expect it to work. You'll get a nasty looking error message. So what if we want to do the very basic task of displaying something on the screen? Simple, we echo it.
The Echo Statement
<?PHP
echo "hello world!";
?>
This is the echo statement. It prints whatever is contained within the quotes (both single and double will work. If your statement uses double quotes then use single quotes to contain it.) All PHP statement end with a semicolon (;) and of course, if you forget PHP will spit out a nasty message at you. The Echo statement can be used to shoot out HTML but that would annoying wouldn't it? typing echo in line after line? Luckily there's a better way to do it. We would want to use the print command.
The Print Statement
<?PHP
print<<<END
Hello World!
Hello again world!
END;
?>
This achieves the same thing but it supports multiple line. Remember when I said every PHP statement ends with a semicolon? If you notice there no semicolon after print<<<END that's because it's technically one statement that end at END; You don't have to use the word END, it can be anything
<?PHP
print<<<CHICKEN
This does the same thing!
CHICKEN;
?>
So let's wrap this up real quick shall we? Try it. Copy one of the groups of code above into a text editor and save it in your webroot as hello.php and run it from your web browser. That's it! Your first PHP script. It's boring I know, but it gets better, trust me.