PHP Coding Structure

Summary:

Here, we will discuss the following:

  • PHP tags.
  • Brackets and their types.
  • Keywords used by PHP for various purposes.

PHP is primarily used as a scripting language for web development and integration into HTML documents. That’s why the PHP parser always expects PHP code in opening and closing tags <?php and ?>.

Unlike other programming languages like Java, C++, or JavaScript; PHP syntax needs to be enclosed within starting and ending tags regardless of whether they are separate files or inside HTML documents.

Here is the basic structure of PHP code.

<?php
	//PHP code goes here.
	echo "Hello World! I'm coding in PHP.";
?>

Note:

The closing tag ?> is optional when PHP is not embedded into an HTML document.

PHP brackets

Brackets in PHP have different uses and are used for different objectives and purposes. There are three types of brackets used in PHP. Here is a brief overview of their uses and functionalities.

Round brackets ():

The main purpose of round brackets also called parentheses in PHP is to group or enclose a set of parameters and variables. The round brackets in PHP are used for function parameters, function calls, and conditional statements.

Besides, its uses in PHP’s code, the round brackets can also be used in mathematical calculations as normal maths rules to separate or prioritize one set of calculations from another.

For example, in the expression $a * ($b - $c), the round brackets calculation should take place before the multiplication.

Square brackets []:

Square brackets in PHP are mostly used for array declaration and array offset. Here is an example of using square brackets.

$myarray = ['item1' => 'item1Value', 'item2' => 'item2Value'];

Now the above array elements can be called using square brackets as follows.

echo $myarray['item1']; //The output is going to be "item1Value".

Curly brackets {}:

Curly brackets also known as curly braces, can be found everywhere in PHP coding. They are mostly used to group or block syntax and statements to change the flow of overall code, or to repeat calling that group of statements and syntax to get a specific result. Curly brackets can be used in declaring functions or in conditional statements, as given below.

<?php 
function functionName(){
	// code goes here.
}
//OR
if($parameters){
	//coding goes here.
}  
?>

Control Structures

PHP control structures control the flow of statements, syntax, and output.
There are conditional statements like if, else, elseif, and switch which run a block of code on conditional bases. PHP also has several types of loops such as for, while, and foreach, used to repeat the execution of a block of code.

<?php
    if (condition1 is true) {
        echo "Condition 1 statements.";
    } elseif (condition2 is true) {
        echo "Condition 2 statements.";
    } else {
        echo "No condition is true default value.";
    }
	
	$employee = array("John", "Abdul", "Joseph", "Adam");
	
	foreach ($employee AS $name){
		echo $name . '<br>';
	}
?>

Functions

PHP function structure is defined with the keyword `function` as follows.

<?php
    function myfunction($parameters) {
        //Function code goes here
    }
?>

Common questions related to this topic.

Can different types of brackets be used interchangeably or vice versa?

The short answer is no. Different bracket types have different meanings for PHP parsers. Only in rare situations, the same result can be achieved using different brackets, but the method of syntax or statements is changed.
For example, in declaring an array the square and round brackets are used interchangeably, but the method of declaration has changed.

<?php 
  	//Declaring array using round brackets
	$numbers = array(1, 2, 3, 4, 5);
	//Declaring array using square brackets
	$numbers = [1, 2, 3, 4, 5];
?>

Can multiple opening and closing tags be used in pure PHP files?