How To Write PHP Code? - PHP Syntax And Comments

Objective

How to write PHP Code?
What is PHP Syntax and Comments?

PHP Syntax can be merged with Plain HTML Page and it doesn’t get displayed on client side. It is executed in server and returns plain HTML page to the client. You can write any PHP logic inside the following block in your HTML page.

<?php
 
//Your PHP Code and Logic goes here.
 
?>

Complete Programming Example

Step 1: Create a folder inside c:/wamp/www folder named phptutorial
Step 2: Create a page inside phptutorial folder named index.php
Step 3: Now paste the following code in index.php page.

<!DOCTYPE html>
<html>
<head>
  <title>PHP Syntax and Comments</title>
</head>
<body>
  <h1>PHP Syntax and Comments</h1>
  <?php
    //This is PHP Comments Example
    /* This is PHP Comments Example */
    # This is PHP Comments Example
    $str="Hello World";
    echo $str;
  ?>
</body>
</html>

Step 4: Run this code in a web browser like Chrome, Mozilla or Internet Explorer. Open localhost/phptutorial/index.php to run this script.

Output
Hello World

More facts about PHP Syntax and Comments

  1. All the PHP page have .php extensions
  2. All the PHP code must be written under <?php //Your code block ?> code block.
  3. There is three famous comments style inside PHP.
    1. // Your Comments
    2. /* Your Comments */
    3. # Your Comments
  4. PHP keywords, functions and classes are not case sensitive so, you can use PHP function or classes either in capital letter or small letter. For example
    echo "Hello World" and ECHO "Hello World" are same.
  5. PHP Variables are case sensitive so the $str and $sTR are the different variables.

Programming Example

<!DOCTYPE html>
<html>
<head>
    <title>PHP Syntax and Comments</title>
</head>
<body>
    <h1>PHP Syntax and Comments</h1>
    <?php
      //This is PHP Comments Example
      /* This is PHP Comments Example */
      # This is PHP Comments Example
      $str="Hello World";
      $sTR="How are you?";
      echo $str;
      eCHo "<br />";
      ECHO $sTR;
    ?>
</body>
</html>
Output
Hello World
How are you?

SUMMARY

In this tutorial you learn how to and where to write PHP script in html page. You also learn how to use commenting in PHP Code. In the next chapter you will learn PHP Echo and Print function.