Welcome back, my young padavan 😉 Our series of tutorials Learning PHP for absolute beginners continues on, and in this one we’ll meet the PHP script (or code) for the first time. Don’t worry. I’ll make sure that it’s as pleasant as possible in these circumstances.
The first thing you need to do is create a file entitled test.php in the root directory of our server. If you installed WAMP with the default settings the root directory will be – C:/wamp/www. So this is exactly where you need to place your file. Open the file with any text or HTML editor and paste the following code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>First PHP script</title>
</head>
<body>
<?php
echo 'Holy Macaroni';
?>
</body>
</html>
Save the file and preview the result in your browser by visiting http://localhost/test.php You should see something like this
Most of the code we have there is standard HTML, but you should notice some new stuff in the body section of the document. Let’s break it down.
<?php – this one is called the opening PHP tag. Once the server reaches the line of code with this tag, it switches from plain-old HTML mode to PHP. And processes the following code as PHP script until it reaches the closing PHP tag
?> – and here’s the closing PHP tag. It basically tells the server: “Stop reading as PHP here, and switch to HTML mode”.
Basically PHP can be placed anywhere in the document, not necessarily between the <body> </body> tags, but if you want to see the output in your browser, you should place it in the body of the document.
Can you hear the echo? … hear the echo? …. the echo?
So what does the echo() function do? – It outputs one or more strings into the browser.
Let’s take our example and see what we can learn from it – echo ‘Holy Macaroni’ ;
- the data we want to output must be wrapped with single ‘ or double quotes ” (we’ll learn the difference a bit later)
- Each statement in PHP must end with a semi-colon. It defines the end of the current statement, and PHP looks for the next one or the end of PHP code.
Let’s see some examples of how you can use echo()
EXAMPLE CODE | BROWSER OUTPUT | NOTES |
echo 123; | 123 | We can omit the quotes when outputting numeric values |
echo ‘Hello user 123’; | Hello user 123 | The output is quite predictable 🙂 |
echo ‘Hello’, ‘user’; | Hellouser | We can output multiple arguments by separating them with a comma. Notice how the text is merged together in the browser output and compare it with the next example |
echo ‘Hello ‘, ‘user ‘; | Hello user | Notice the whitespace after the Hello word. It will be outputted in the browser as it’s part of the argument |
echo ‘Hello ‘, ‘user ‘ | Hello user | The result would be the same as above. PHP script will output only one whitespace, as it cuts down more then one |
Let’s summarize our knowledge:
- PHP can be embedded into html pages. Just don’t forget to change the extension of the file to .php
- PHP code must be placed within tags
- PHP is easy and you can learn it 🙂
- You’ve also learned your first PHP function echo, which allows to output data into the browser