Let your friends know!
While you coding on PHP you have to follow some basic rules or syntax to write properly structured program. Below I will give some ideas of very basic syntax of PHP.
Escaping to PHP
The PHP parsing engine needs a way to differentiate PHP code from other elements in the page. The mechanism for doing so is known as “escaping to PHP”. When PHP parses a file, it looks for opening and closing tags. All PHP code must be contained within this tag.
The most popular and effective PHP tag is
Or the shorthand PHP tag that requires shorthand support to be enabled on php.ini configuration file directive. But it is discouraged by PHP official.
If a file is a pure PHP file that is it has PHP codes only, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted problem.
<?php echo "Hello world"; // ... more code echo "Last statement"; // the script ends here with no PHP closing tag
Example of a simple PHP file that has embedded PHP code with HTML
<!DOCTYPE html> <html> <body> <h1>PHP inside HTML</h1> <?php echo "Hello World!"; ?> </body> </html>
How to Save Your PHP Pages
The default file extension for PHP files is “.php”. If you have a file which contains PHP then you must save the file with a .php extension, instead of the standard .html extension.
Commenting PHP Code
A comment is the part of a code that exists only for the human reader but nor for execute. PHP supports ‘C’, ‘C++’ and Unix shell-style. Purpose of commenting is to let others understand what you are doing and remind yourself of what you did.
Single-line comments: They are generally used for short explanations to the code. Below are the examples of single line comments.
<?php # This is a comment, and # This is the second line of the comment // This is also a single line comment in another style ?>
Multi-lines comments: They are generally used to provide more detailed explanations when necessary. The mult-ilines style of commenting is the same as in C. Below are the example of multi-lines comments.
<?php /* This is a comment with multiline Author : Mohammad Shareful Purpose: Multiline Comments Example Subject: PHP */ ?>
PHP Case Sensitivity
Yes it is true that PHP is a case sensitive language. Try out following example:
<?php $a = 6; print("Variable a is $a \n"); // echo does the same thing as print echo("Variable A is $A \n"); ?>
The above code will produce following output:
Variable a is 6 Variable A is
But all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive. All three echo statements below are legal and equal in the following example:
<?php ECHO "Hello World! \n "; echo "Hello World! \n "; EcHo "Hello World! \n "; ?>
PHP whitespace insensitive. This means it is OK to have one line of PHP code, then multiple lines of blank space before the next line of PHP code. Also can have tab to indent code, the PHP interpreter will ignore those spaces as well.
<?php echo "Hello World!"; echo "Hello World!"; ?>
Hello World!Hello World!
Printing is the most useful feature in a language to output from the program. There are two basic ways to get output: echo and print in PHP. echo and print are more or less the same one small differences. echo can take multiple parameters while print can take one argument. echo is marginally faster than print.
Below is the example of printing using echo
<?php echo "I will be in the screen.\n"; echo "This ", "string ", "was ", "made ", "with multiple parameters."; ?>
I will be in the screen. This string was made with multiple parameters.
PHP supports both single line printing and multiline printing. Below is an example of single line printing.
<?php print "An example with single line comments using print method"; // print and echo does the same thing echo "An example with single line comments using echo method” ?>
Below is an example of multiline printing
<?php # 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";
A statement in PHP is any expression that is followed by a semicolon (;). As you may or may not have noticed in the above example, there was a semicolon after the line of PHP code. The semicolon signifies the end of a PHP statement and should never be forgotten.
<?php $greeting = "Welcome to PHP!"; ?>
Expressions are combinations of tokens. The smallest building blocks of PHP are the tokens, such as numbers , strings , variables , constants , and the keywords that make up the syntax of PHP itself like if, else, while, for and so forth.
Let your friends know!
Latest posts by Anurag Sharma (see all)
- Wireless Speakers vs Wired Speakers? Which is a Better Choice? - March 29, 2017
- Teen Patti Gold Mod Apk: How to Hack Teen Patti Gold? - February 25, 2017
- COC MOD 2017: Clash of Clans Mod Apk Download (Hack Gems,Gold, Elixir) - February 7, 2017