PHP Tutorial - PHP Statement






PHP Code Blocks

A code block is a list of statements grouped as one unit.

Syntax

In PHP we use {} to wrap code blocks and notify the compiler by saying, "hey this is a group of statements."

Example

The following code group two print statements together and outputs A and B if the sky color is blue.

<?PHP

$sky_color = "blue";

if($sky_color == "blue") {
     print("A \n");
     print("B \n");
}

print("Here I am from java2s.com.");

?>

The code above generates the following result.





Example 2

The following code shows how to create three Simple statements.

<?php
  //an expression statement
  2 + 3;

  //another expression statement
  print("PHP!");

  //a control statement
  if(3 > 2)
  {
    //an assignment statement
    $a = 3;
  }
?>


The code above generates the following result.





PHP Comments

In programming a comment is the description for our code.

Syntax to mark comments

There are three ways to create comments in PHP:

  • //
  • /* */, and
  • #.

Note 2

// and # mean "Ignore the rest of this line,"

     <?php
             print "This is printed\n";
             // print "This is not printed\n";
             # print "This is not printed\n";
             print "This is printed\n";
      ?>
      

The code above generates the following result.

/* means "Ignore everything until you see */."

     <?php
             print "This is printed\n";
             /* print "This is not printed\n";
             print "This is not printed\n"; */
      ?>

The code above generates the following result.