Tag Archive | "comments in php"

Tags: , , , , , , ,

PHP Basic Syntax Tutorial

Posted on 03 September 2011 by Moki

PHP execution is done on the server although the information sent to the web browser is in plain HTML. This is one security benefit to PHP over other older languages.

You always start a PHP script with <?php and end the script with ?>. Some servers that have shorthand support will allow you to start the code with <? (without the php) although you should always use the <? php to stay with the standards. You don’t have to do this at the top and bottom of the entire page but it has to be before and after any PHP code. You can also have PHP code anywhere within your website as many times as you need to call it.

Most PHP pages are HTML with PHP scripts within them. To demonstrate we will do the well known “Hello World” beginner page.

<html>
<body>

<?php echo "Hello World"; ?>

</body>
</html>

Every line of PHP code must end in a semicolon as a seperator to distinguish each set of instructions. We cover this by adding the semicolon after World” in the example above.

While there are two ways to output text in PHP you will use echo a majority of the time. The two ways are echo and print.

Keep in mind that any time you save a file you have to save it as PHP (meaning it will have the .php extensions) or it will not run the PHP code. While .php files will run the php code along with any HTML that you have in it as well, HTML will only run the HTML code.

In PHP to comment out a single line of code you precede it with // and to comment out multiple lines of code you should place /* before the block of code and */ at the end of it to tell it where to end the comment.

<html>
<body>

<?php
//This is a single line comment

/*
This is a multiple line comment
This is a multiple line comment
This is a multiple line comment
*/
?>

</body>
</html>

Comments (0)