PHP Code Blocks

In this chapter you will learn:

  1. What is Code Blocks
  2. Syntax to create PHP code blocks
  3. Example - Use code block with if statement

Description

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/*from   j av a  2 s  . c  o  m*/

$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.

Next chapter...

What you will learn in the next chapter:

  1. What is a comment
  2. Syntax to mark comments
  3. Note for comments
Home » PHP Tutorial » PHP Statements
PHP Code Blocks
PHP Comments
PHP if
PHP if else
PHP switch
PHP foreach
PHP while loop
PHP do while loop
PHP for loop
PHP break
PHP continue
PHP Mixed-Mode Processing
PHP include/require