Complete PHP Tutorial – Chapter 2

In previous chapter, you’ve learned about basics of PHP, Installation and XAMPP and in this tutorial we’ll cover PHP Tags & Comments.

PHP Tags –

All scripts start and end with these tags:

PHP Code:

<?php
//Your code goes here.
?>

The PHP tags can be placed anywhere in your document.

Here is an example where we’ve used a function called echo to print out “Hi” on to a web page.

PHP Code:

<?php
echo “Hi”;
?>

As you can clearly see, the PHP tags are indicating to my computer where my PHP code begins and vise-versa when I use closing PHP tags. They can also be opened and closed multiple times throughout the file.

Echo

PHP has a built in function called “echo” which has the job of outputting text. It would be a mistake to call it a function as it is actually a language construct, this means it does not require parentheses.

As with all PHP statements, the echo construct must always end with a semi-colon “;” so it is clear where each line ends. We’ve already had two examples in this tutorial where we have used echo so go back and have a look at how they were written. All the outputs of echo are in plain HTML meaning you can use HTML tags within it.

PHP Code:

<?php
echo “<strong>This will output strong text!</strong>”;
?>

Comments

A comment is a line or section of code that is not executed as part of the program. Comments serve the role of communicating to others and your self how certain bits of your code work or what they are used for. The first kind of comment is a single line comment.

To begin one of these you simply do // and anything on that line after the slashes will not be executed. The other option is a multi-line comment, these are started with /* and end with a */. Anything between this two will not be treated as code.

Let’s have some examples of comments so you can understand exactly how they look in practice.

PHP Code:

<?php
//This is a single line comment. It only comments this line
echo “Hello world”;
/*
This is a multi-line comment. It comments anything between the constructs.
*/
?>

That’s all the most basic syntax out of the way. These were beginner constructs that you will need to know but don’t fall into any other categories of the tutorial.

It is also important to note that these constructs can’t be changed. However, now you should be able to write a basic PHP script.

Continue to Next Chapter – Complete PHP Tutorial – Chapter 3

Credit Goes to Mr. RYDIOO

You may also like:

Sarcastic Writer

Step by step hacking tutorials about wireless cracking, kali linux, metasploit, ethical hacking, seo tips and tricks, malware analysis and scanning.

Related Posts