Next Topic :: PHP Syntax

Next Topic :: PHP Syntax

Hope this article finds you well. Today we will learn about the very basic syntax of PHP and which is most important to make your PHP fundamentals STRONG.

So read the article till the end and i will make sure you know everything about PHP syntax.

So lets start…..

A PHP script is always executed on the server and the output as plain HTML is sent back to the browser to show on your screen in human readable form.

Escaping to PHP

A PHP code can be placed anywhere in the document either top, bottom or in between. The default extension of PHP files is “.php”. The PHP parser needs to separate the PHP code from other elements on the pages like HTML, SCRIPT, STYLE tags and many more. And for that we need to use the PHP tags.

i.e. <?php, ?> where <?php tag is the tag from where PHP script/code starts and ?> identifies where the script/code end.

Below is an example-

<?php
// Place PHP code here.
?>

So the process for separating other tags from PHP tags called ‘escaping to PHP’.
There are mainly two ways in PHP to do this.

Standard PHP tags

The most commonly used PHP tag style is as below-

<?php ………… ?>

This is the most effective and standard way to code in PHP. If you use this tag there is no way to have any mistake in parsing your code and you can be sure that your tag will always be interpreted correctly every time.

Short-open tags

The short-open tag style is as below-

<? —– ?>

This is not the commonly used tags and by default this tag always set disabled. As you can see this is the easiest way to separate PHP code in the script.

But the question is if this is the easiest way then “why this is always set to disabled in default settings?

This is because there are much chance of having mistakes with this tag style because same syntax is used for XML tags and while parsing tag style you can get different OUTPUT.

As you can enable this tag style and i will tell you the ways How can you enable this tag style but i personally recommend to set this tag style to disabled while using the XML.

How to enable short-tag

– To enable this tag style, you should have access to php.ini file, if you have access to php.ini file, open this file and search for “short_open_tag” and set this setting “On” 

e.g short_open_tag = on

– if you do not have access of php.ini, no need to worry guys, there is another way to achieve this. You can do it via .htaccess file. Place below code in you .htaccess file and your short-open tags style will be enabled

e.g php_value short_open_tag 1

Note: This method depends upon your hosting settings. It may be possible that your hosting company has disabled this if you are on shared hosting:
Below is an example of a simple PHP file, with a PHP script using a built-in PHP function “echo” to output the text “Hello ShoutMe360“.

<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
echo “Hello ShoutMe360”;
?>

</body>
</html>

Note: PHP functions are case sensitive and statements end with a semicolon(;).

Commenting PHP Code

A comment is a part of the script that does nothing in the code but only for human reading. Through commenting we can define the process and can provide any information to someone who is looking at the code. Comments always get stripped before displaying the output to the browser. There are two types of comments in PHP-

Single-line comments –

This type of comment format is normally used for short explanations relevant to the local code. Below are the examples of single line comments.

<?php
# This is the first line of the comment.
# This is the second line of the comment.
// This is a comment too.
print “An example with single line comments”;
?>

Multi-lines printing with Single-line comments – Below is the example to print multiple lines in a single print statement –

<?php
# First Example
print “This spans multiple lines. The newlines will be output as well”.
Note that the here document terminator must appear on a
line with just a semicolon no extra whitespace!;
?>

Multi-lines comments – This type of comments are normally used to provide detailed information about the script.
The multi-line style of commenting is the same as in C. Below is the example of multi-lines comments.

<?
/* This is a comment with multi-line
Author : Lalit Kumar
Objective : Multi-line Comments Demo
Subject : PHP Syntax
*/

print “An example with multi line comments”;
?>

Comments can be used to:

Let others understand what you are doing or what have you done.
Remind yourself of what you did – Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did.
Comments can remind you of what you were thinking when you wrote the code.

PHP is case sensitive

Yeah like C language, PHP is also a case sensitive language. Try out following example –

<html>
<body>

<?php
$blogName = “ShoutMe360”;
print(“Blog name is $blogName<br>”);
print(“Blog Name is $blogname<br>”);
?>

</body>
</html>

This will produce the following result –

Blog name is ShoutMe360
Blog Name is

PHP is whitespace insensitive

PHP whitespace insensitive means that it never matters how many whitespace characters you have used in a row. one whitespace character is the same as many whitespace characters.

For example, PHP statements below that assigning the sum of 5 + 2 to the variable $result is equivalent –

$result = 5 + 2; // single spaces
$result <tab>=<tab>5<tab>+<tab>2 ; // spaces and tabs
$result =
5+
2; // multiple lines

Note: Whitespace refers to the characters you type that remains invisible on the screen, including spaces, tabs, and carriage returns (end-of-line characters).

Braces make blocks

As PHP code consists of statements and statements can not be combined together like expressions. So we put them together in sequence of statements
anywhere by enclosing them in a set of curly braces. A block starts with “{” and ends with “}”.
Here both statements are equivalent –

if (2 == 1 + 1)
print(“Hello Reader. You have learnt all about the PHP syntax.<br>”);

if (2 == 1 + 1) {
print(“Hello Reader. You have learnt”);
print(“all about the PHP syntax.<br>”);
}

This is it for today.
This is Lalit signing off for today.

I will catch you on next Monday with the new topic in PHP.

If you like this session, please give a thumbs up and keep it sharing.

Tada

Leave a Reply

Share18
Tweet
+11