"Hello World"
PHP files and How they Work
- PHP files, which usually have a file extension of ".php", ".php3", or ".phtml", consist entirely of plain text, just like HTML documents.
- Unlike HTML documents, however, when a user tells their browser to access a PHP file, the server parses the document with PHP, which executes all of the PHP commands contained in the documents. These commands can do any number of things, from simple arithmetic, to accessing a database, pulling information from it, and then formatting it with HTML.
- After the document is parsed by PHP, it is then sent by the server to the browser of the user that requested it without any of the PHP commands that exist in the server side version of the document. If the parsed document contains HTML, the user's browser renders it as it would any HTML document.
Basic Syntax
Now that you have a basic understanding of how the magic happens, you are ready for the fundamentals of writing PHP scripts.
- A block of PHP script always begins with "<?php" or "<?" and ends with "?>".
- This lets PHP know that it needs to parse everything within the open and close PHP tags. Everything else is ignored by the parser.
- Here is an example of a simple PHP script that we will look at in more detail later:
<?php echo "Hello world!"; ?>- In the example above, we opened a PHP block, provided a command, and then closed the PHP block
- Variables: Like most programming languages, PHP depends heavily on the use of variables.
- All variables in PHP start with the "$" symbol.
- Unlike C and C++, which PHP's syntax borrows heavily from, you do not have to declare your variables before you use them! The parser will figure out the data type for you!
- Operators: For reference, here is a list of some of the operators that may be used in this session. Their use will be explained as they appear in examples.
- = : Assignment
- .= and . : Concatenation.
- == : equal to
- > : great than
- < : less than
- >= : greater than or equal to
- <= : less than or equal to
- != : not equal to
- && : and
- || : or
- Arithmetic operators: Standard arithmetic operators are also available.
- + : addition
- - : subtraction
- * : multiplication
- / : divison
- % : modulus or remainder
- Commenting: There are three methods for commenting your code. They are demonstrated in the following example.
<!-- This is an html comment and is not a valid method for commenting inside PHP blocks. This will cause PHP to return an error if inside a PHP block. -->
<?php
# Familiar with shells?
# Use this method to comment # a single line
// Like the above
// method, this also
// comments a single line
/*
Use this method to
to comment
multiple lines
*/
?>
Embedding PHP in HTML
You are almost ready to write your first PHP script, but before we move on to that, it is important that you understand how your PHP code and your HTML will relate to one another.
- The parser will ignore everything outside of your PHP block, that is everything outside of the <?php and ?> tags.
- Any output of your PHP script, such as text generated using the echo or print functions, will exist in the document sent to the client's web browser.
"Hello World"
You are now ready to write your first PHP script.
First, we should start off with an HTML document such as the following.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello world!</title>
</head>
<body>
<h1>This is going to be my first PHP script</h1>
</body>
</html>
The next step is to open a PHP block and start writing some PHP!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello world!</title>
</head>
<body>
<h1>This is going to be my first PHP script</h1>
<?php
/*
As discussed earlier, this is a comment. I will be using comments to describe
what the PHP script is doing.
The first thing that we will do is use the echo function to output the message
"hello world" in our document.
*/
echo ("Hello world!");
/*
In the parsed version of this document, the only thing that will appear in place
of this PHP block is the text "Hello world!".
This is not the only way to print "Hello world!" however, we could
use a variable instead.
*/
$hello = "Hello world, I love evil kittens!";
/*
The Variable $hello, now contains the string "Hello world, I love evil
kittens!", we can output this string by passing the variable to the echo
function in the same way that we passed the string earlier.
*/
echo ($hello);
/*
After the document is parsed, we should now see the following in place of the
PHP block:
Hello world!Hello world, I love evil kittens! You can see this hello world file in action by clicking here.
*/
?>
</body>
</html>
Notice the lack of formatting in the text outputted by our PHP script. We can resolve this issue by modifying our echo statements to include html tags.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello world!</title>
</head>
<body>
<h1>This is going to be my second PHP script</h1>
<?php
echo ("<p>Hello world!</p>");
$hello = "<p>Hello world, I love evil kittens!</p>";
echo ($hello);
?>
</body>
</html>
Now our block of PHP script will be replaced by the two strings wrapped in paragraph tags. To see the difference, view this file.