A Brief PHP Tutorial: #1 Hello World

My lack of posting has driven me to begin this PHP tutorial series. I suppose seeing as my main focus at the present is working on WicketPixie, I should teach others PHP while the subject is sharp in my mind.
We will start with everyone’s favorite, Hello World. This post will show you basic syntax and a few of PHP’s functions. Without further ado, let’s get started.

In PHP your code starts with ‘<?php’ and ends with ‘?>’.
[cc lang="php"]
// Code
?>
[/cc]
What’s that ‘// Code’ line? It’s a comment. Comments are useful for documenting your code as you write it. It also helps other developers get an idea of what your code is doing. Besides ‘//’ you can also surround text that you wish to comment with ‘/* text */’ This allows for multi-line comments like so:
[cc lang="php"]
/*
Hello World program
*/
?>
[/cc]

So that was a brief run-down of commenting in PHP. Trust me, you’ll want to make use of them when you code in PHP.

Now, to output text you simple use the echo function. There are aliases for this function such as print and printf, all of which are either tweaked slightly differently or are just redirects for echo.
[cc lang="php"]
echo "This is some text.";
print "This is also some text.";
printf "Yet Another Line Of Text.";
?>
[/cc]
Notice the semi-colon (;) at the end of each line. A semi-colon tells PHP when to end a statement. Using semi-colons you can put multiple statements on a line.
[cc lang="php"]
echo "Text 1";print "Text on the same line.";
?>
[/cc]

Okay, so now that you know how to output text, we can write our Hello World program:
[cc lang="php"]
/**
* Hello World program
**/
echo "Hello World!";
?>
[/cc]

And that is it! How was that for my first PHP tutorial? We covered comments and outputting text. The next tutorial will deal with variables and numbers in PHP. Hope this helps someone. (I’m talking to you Joel! ;) )

2 Comments

Just to point out, you can comment too using #, its basically the same as //

Leave a Comment