In this tutorial, you will learn about the basic syntax required to start PHP code along with the ways of using shortcode tags. You will also get some knowledge on different ways of writing comments and learn about the different statements like print( ), echo( ), printf( ), and sprintf( ) that can be used for displaying dynamic output to the browser.
Table of Contents
Start PHP Coding With Basic PHP Syntax
The following is the basic syntax if you want to start PHP coding in order to display data output on the browser.
Writing With Default Syntax
In the default syntax, every PHP code begins with <?php and concludes with ?>. Following are the syntax and the examples of writing basic PHP scripts.
Syntax: <?php ……….. PHP code will be written here …….. ?>
Example:
<!DOCTYPE html>
<html>
<head>
<title>Basic Syntax for Writing PHP Code</title>
</head>
<body>
<h1>Basic Syntax for Writing PHP Code</h1>
<p>Writing With Default Syntax</p>
<?php
echo "<p>Here is the sample output</p>";
?>
</body>
</html>
Output: Copy the code given above and paste it on a text editor application i.e. Notepad then save it on the root directory of your web server where the “phpinfo.php” file is located. When you open the file from your browser as seen in the image below, you will see the following output on your browser.
Writing With Short Tags
There is another option also available who don’t want to type longer texts while coding, it is known as short tags. It enables you to write codes without using default syntax. In order to use this feature, you have to enable PHP’s short_open_tag directive first.
Here is an example with sample codes were written using short tags.
<!DOCTYPE html>
<html>
<head>
<title>Basic Syntax for Writing PHP Code</title>
</head>
<body>
<h1>Basic Syntax for Writing PHP Code</h1>
<p>Writing With Short Tags</p>
<? echo "It is the sample code using one short-tag."; ?>
<br/>
<?= "It is another sample code using another short-tag."; ?>
</body>
</html>
Following is the output of the PHP code given above.
Commenting The Code
Writing comments are very important in order to organize and maintain the code while coding in any programming language. PHP offers three types of syntax for commenting on your code, which are given below.
Single Line Syntax
PHP supports C++-style single-line comment syntax. It can be done using a double slash (//) symbol at the beginning of each line of code as given in the example below.
<?php
// Title: commenting the PHP code
// Author: Shuseel Baral
echo "This is the single line syntax of PHP comment";
?>
Shell Syntax
PHP also supports another single-line C++-style syntax called shell syntax. It can be written using hash marks (#) at the beginning of each line of code as given in the example below.
<?php
# Title: commenting the PHP code
# Author: Shuseel Baral
echo "This is the single line shell syntax of PHP comment";
?>
Multiple Line Syntax
You can also use multiple lines commenting syntax on PHP. This type of comment begins with /* and concludes with */ sign, which is logically warranted in numerous lines.
<?php
/* Title: commenting the PHP code
Author: Shuseel Baral */
echo "This is the multiple line syntax of PHP comment";
?>
Outputting The Data To The Browser
Event for creating the simplest dynamic website, you have to generate data output to the browser. PHP offers several methods for displaying the data to the browser. You can generate data output to the browser using the statements like print( ), echo( ), printf( ) and sprintf( ).
The print( ) Statement
The print( ) statement generates output what data you have passed to it. Following is an example including the possible use of print( ) statement in PHP.
<?php
print("<p> Outputting The Data To The Browser Using print( ) </p>");
?>
<?php
$statement = "print( )";
print("<p> Outputting The Data To The Browser Using $statement </p>");
?>
<?php
print("<p> Outputting The Data To The Browser
Using print( ) </p>");
?>
The statements written above produce the following outputs to the browser.
Outputting The Data To The Browser Using print( )
Outputting The Data To The Browser Using print( )
Outputting The Data To The Browser Using print( )
The echo( ) Statement
You can also use the echo( ) statement in the place of print( ) statement in order to display data output to the browser. The echo( ) statement can be implemented just like the print( ) statement given below.
<?php
echo "<p> Outputting The Data To The Browser Using echo( ) </p>";
?>
The echo( ) statement is capable of displaying data generated through multiple arguments. Following is an example of showing the feature of echo( ) statement.
<?php
$color1= "Red";
$color2= "Green";
echo $color1, "and", $color2, "are the colors.";
?>
The statements written above produce the following outputs to the browser.
Red and Green are the colors.
The printf( ) Statement
If you want to generate data output both the static text and dynamic data on the single line that is stored in one or several variables, the printf( ) statement can be used. The printf( ) statement allows you to control how the dynamic information is rendered to the screen in terms of its type, precision, alignment, and position.
Here is an example that allows inserting a single dynamic integer value along with the static text.
<?php
printf("There are %d pages on the book", 300);
?>
In the above code % d is the placeholder which is known as type specifier that can be presented as a signed decimal number.
The sprintf( ) Statement
The speintf( ) statement is very similar to the print( ) statement but the output is assigned to a string rather than rendering to the browser.
Here is an example that shows the output generated using sprintf( ) statement.
<?php
Total $cost of the Books = sprintf("$%2f", 93.2);
?>
The output of the code given above will be as follows.
Total $cost of the Books = 93.20
Read Next: What Are PHP-Supported Data Types
Comments are closed.