How To Start PHP With Basic Coding Scripts

In order to start coding with PHP, you have to install PHP and the web server on your computer at first. We have already discussed on the previous posts about the methods of installing Apache Server and PHP on your computer. Now we are going to discuss on how you can start writing basic coding scripts in order to display some dynamic data on the browser screen. In this tutorial you will learn about the basic syntax for writing PHP code along with the ways of using short code 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. 

Basic Syntax for Writing PHP Code


Following are the basic syntax for writing PHP code 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 on the image below, you will see the following output on your browser. 

 Default Syntax for Writing PHP Code

Writing With Short Tags


There is another option also available who don't wants 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 at first.

Here is an example with sample codes 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.

 Short Tag Syntax for Writing PHP Code

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 your code, which are given below.

Single Line Syntax

PHP supports C++ style single-line comment syntax. It can be done using double slash (//) symbol at the beginning of each lines of code as given on 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 (#) the beginning of each lines of code as given on 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 line commenting syntax on PHP. This type of comment begins with /* and concludes with */ sign, which is logically warrants 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 a 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 used 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 produces 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 that feature of echo( ) statement.
<?php
          $color1= "Red";
          $color2= "Green";
          echo $color1, "and", $color2, "are the colors.";
?>
The statements written above produces the following outputs to the browser.
Red and Green are the colors.

The printf( ) Statement

If  you want to generate data output both of the static text and dynamic data on the single line that are 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 to insert 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
 
How To Start PHP With Basic Coding Scripts
2017-10-04T08:38:00-07:00
Shuseel Baral
PHP|Programming Tutorials|