PHP Tutorial [Part 1]

Back to Main

The Basics

I assume we have installed PHP successfully, so now we are going to move on to the basics.

1
2
3
<?php
echo "Hello World!";
?>

<?php – This is how we start EVERY script. It lets the browser know that this section is php.
?>- This is how we end EVERY script.
If we do not include both <?php and ?> the browser will not know that it is php and will display it as plain text and not run the script.

Ok, on line 2 we see echo “Hello World!”;. echo is the same as print in other languages for the most part. It tells the browser to display words or variables. All the words must be encased in quotes. Notice the semi-colon after the command. That tells the browser where the command ends. You must put a semi-colon at the end of EVERY command or the the entire script won’t run. Typically, if you are seeing errors, check for a semi-colon at the end of every command.

1
2
3
<?php
echo "Hello World!"."<br>Hi!";
?>

Output:

Hello World!
Hi!

You notice we just added .”<br>Hi!” to the first script. The period is a linker. It is used to combine strings. It is like a + for integers. The period will display the first string, followed by the second string.

Also in this example is “<br>” which you will recognize as HTML code. As php runs through the script, it creates the output, which is then processed by the browser:

PHP Script --> PHP Processor --> Output Created --> Sent to web server -->
Parsed for HTML Code --> Displayed to user

So we can echo a whole HTML document and the webserver will process it and display it the proper way to the user. If you are using cmd.exe or terminal, it never gets processed by the webserver, so you will see “<br>”.

Next up – Variables

Pages: 1 2 3 4 5 6