I figure we are going to have many PHP rookies looking for some basic help so I decided to get this on tutorial out of the way.
Hello World PHP Tutorial brought you by Silverlime Communications
PHP is a server-side programming language popular for it's rapid development speed and power (along with the fact that it's free and widely distributed).
Today, we'll learn some basics of a PHP file and learn the ability of printing some text which can be seen in a web browser!
File Structure
Basically, this is the structure of a PHP file:
We opened the PHP parsing with the
<?php followed by a newline. When we didn't want PHP to parse any more code, we closed it with the
?> tag. Straight HTML can be placed outside of these tags if you want that HTML to be sent straight to the browser as a normal .html file would.
Printing
PHP has a few different methods to output text to the screen. The most common,
echo, is a language construct and a function (there's no need to say
echo("Text"); because
echo "Text"; does the same thing). Echo and it's counterpart,
print, are almost identical with both printing and both being language constructs. For more information on the differences, visit
PHP.FAQTs. Here's a printing demonstration using both:
Single vs. Double Quotes
Most PHP programmers find single quotes, `'`, to be faster than double quotes, `"`. You might find evidence of this when outputting large pieces of text or when parsing variables with double quotes (something single quotes don't do). Single quotes will output whatever is within the quotes without parsing for newlines,
\n, carriage returns,
\r, variables.
$variablename, etc. Here's an example:
would print
This variable is $variable
while...
would print
This variable is cool
How can we use single quotes to print variables? We add variables to the strings like so:
I even attached a period
after the variable is printed. So, here's what we would get:
This variable is cool.
That system provides very clean code and looks much better in syntax highlighting PHP code-editing software. The same can be done with double quotes.
Hello World
Using all of our knowledge, we are now going to print hello world twice using linebreaks,
echo(), and
print().
would print
Hello world!
Hello world!
Recapping
We learned...
- the basic structure of a PHP file.
- how to print using echo() and print() as functions and language constructs.
- how to use single and double quotes properly.
- how to set the value of variables
- how to properly include variables while outputting basic text
Questions? Comments? Please post below or contact me.