09.13.07

Basic PHP Syntax

Posted in PHP at 11:53 am by admin

Before you can go on and plan things out in the style of PHP, you need to understand how the basic syntax of PHP works. It is difficult to start right at the end without having the fundamentals in place and ultimately this is why you need to know for sure exactly how PHP syntax works before you go ahead into creating more complicated scripts.

Placement, Starting and Ending

You can use PHP scripting within your HTML files by using the proper syntax to denote the PHP script. PHP scripts always open with “<?php” and always close with “?>”.

In other words, the following would be the correct way to right a PHP script:

<?php echo “This is a Test”; ?>

The following PHP script would tell the server to print the phrase This is a Test and because the basic syntax of opening and closing is correctly placed, the script would execute perfectly.

However, if you were to structure your PHP script like this:

<php echo “This is a Test”; ?>

People that visit your website would end up seeing an error on the page because of your improper syntax within the PHP script. Remember that proper syntax is the key to making things work and this is as true for PHP as it is for HTML or any other web-based or programming-based language.

Commenting

Whether you are building the script for someone else or are just interested in documenting your progress so that you remember what you were thinking about later, PHP commenting can come in quite useful for either purpose. PHP commenting is done differently from HTML commenting however, with single-line comments denoted by their starting with “//” and multiple-line comments being bounded above by “/*” and below by “*/”

For example:

<?php

//Single-line comment

/*
Multiple
Line
Comment
*/

?>

This PHP script would actually not return anything in terms of viewable material by a person visiting your website for the simple reason that all it has within it are two comments.

Of course, if you wanted to combine the two ideas:

<?php

//single-line comment

Echo “This is a Test”;

/*
Multiple
Line
Comment
*/

?>

The end result of this PHP code would once again be the printing of the phrase This is a Test. The two comments would not show up because comments in any programming language do not affect the output that language creates; they are merely there for the author to remark on a particular piece of code or alternatively for multiple authors of the same code to exchange information.

Scriptycan is a great software repository featuring both free and commercial PHP scripts and applications for developers and programmers.

Leave a Comment

You must be logged in to post a comment.