Php Syntax


PHP Syntax

In order to program in PHP you must abide by the proper PHP syntax. Specifically, this means that a PHP script must:

  • Be a plain-text file
  • Have the correct file extension
  • Use PHP tags to mark the PHP code
  • Format statements, structures, and comments according to exact rules

Although this may sound demanding, one of PHP’s greatest strength’s is its easy-to-learn syntax.

Plain-Text File

PHP code goes into a plain-text file, which can be created in any text editor, WYSIWYG editor, or IDE, like NuSphere’s PhpED. Many applications can be used to program in PHP, but you could not, for example, create a PHP Microsoft Word document (it must be a plain-text file). Furthermore, not all text editors are created equal (Notepad, in particular, is a notably bad editor). Any real PHP programming is best done using a dedicated Integrated Development Environment (IDE), such as NuSphere’s PhpED. With it, you’ll be able to program more quickly and with less frustration, thanks to features like

  • Code Completion
  • Syntax Highlighting
  • PHP Syntax Checking
  • Function syntax and usage
  • Integrated HTML support
  • Built-in PHP parser
  • A top-notch debugger

http://d3gswd4a9vhkat.cloudfront.net/flags_eCommerce.png

Commenting PHP Code:

A comment is the portion of a program that exists only for the human reader and stripped out before displaying the programs result. There are two commenting formats in PHP:

Single-line comments: They are generally used for short explanations or notes relevant to the local code. Here are the examples of single line comments.

<?
# This is a comment, and
# This is the second line of the comment
// This is a comment too. Each style comments only
print "An example with single line comments";
?>

Multi-lines printing: Here are the examples to print multiple lines in a single print statement:

<?
# First Example
print <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
END;
# Second Example
print "This spans
multiple lines. The newlines will be
output as well";
?>

Multi-lines comments: They are generally used to provide pseudocode algorithms and more detailed explanations when necessary. The multiline style of commenting is the same as in C. Here are the example of multi lines comments.

<?
/* This is a comment with multiline
    Author : Mohammad Mohtashim
    Purpose: Multiline Comments Demo
    Subject: PHP
*/
print "An example with multi line comments";
?>

PHP is case sensitive:

Yeah it is true that PHP is a case sensitive language. Try out following example:

<html>
<body>
<?
$capital = 67;
print("Variable capital is $capital<br>");
print("Variable CaPiTaL is $CaPiTaL<br>");
?>
</body>
</html>

This will produce following result:

Variable capital is 67
Variable CaPiTaL is

Running PHP Script from Command Prompt:

Yes you can run your PHP script on your command prompt. Assuming you have following content in test.php file

<?php
   echo "Hello PHP!!!!!";
?>

Now run this script as command prompt as follows:

$ php test.php

It will produce following result:

Hello PHP!!!!!

Hope now you have basic knowledge of PHP Syntax.

Leave a comment