PHP Syntax

By Shahnail Khan Last updated : November 27, 2023

A PHP script typically consists of three main parts-

  1. Opening PHP Tag: The opening PHP tag, denoted by <?php, marks the beginning of the PHP code block.
  2. PHP Code: The PHP code contains the actual instructions and data manipulation that the script executes.
  3. Closing PHP Tag: The closing PHP tag, denoted by ?>, which marks the end of the PHP code block.

Syntax of PHP Tags and Structure

PHP code is enclosed within <?php ?> tags.

This the basic syntax of PHP program:

<?php
// PHP code goes here
?>

Syntax of Comments in PHP

Comments are essential for code documentation. PHP supports both single-line (//) and multi-line (/* */) comments. For example -

// Single-line comment
/* 
   Multi-line comment
   for longer explanations
*/

Syntax of Variables

Variables in PHP start with the $ symbol followed by a name (e.g., $variable_name). For Example -

$name = "John";
$age = 25;
$isStudent = true;

Syntax of PHP Operations

PHP uses operators for arithmetic (+, -, *, /), comparison (==, !=, >, <), logical (&&, ||, !), and assignment (=, +=, -=) operations. For Example -

$sum = $num1 + $num2;

$isGreater = ($num1 > $num2);

Syntax of Conditional Statements

PHP offers conditional statements like if, else if, else, and switch for decision-making. For Example -

if ($age >= 18) {
    echo "You are an adult.";
} else {
    echo "You are a minor.";

Syntax of Loops

PHP provides for, while, do-while, and foreach loops for repetitive tasks. For Example -

for ($i = 0; $i < 5; $i++) {
    echo $i;

Syntax of Functions

Functions in PHP are defined using function keyword followed by a name and parameters. For Example -

function greet($name) {
    echo "Hello, $name!";
}
greet("Alice");

Example 1

This is a basic PHP program with three variables.

<?php
// Define three variables
$greeting = "Hello";
$name = "John";
$age = 25;

// Display the variables' values
echo $greeting . ", " . $name . "! You are " . $age . " years old.";
?>

The output of the above program:

php syntax - example 1 output

In this example, three variables are defined:

  1. $greeting is assigned the value "Hello".
  2. $name is assigned the value "John".
  3. $age is assigned the value 25.

The echo statement is then used to display a message that includes the values of these three variables. Save this code in a file with a ".php" extension and run it to see the output in the browser.

Example 2

This is a PHP program with variables and arithmetic operations.

<?php
// Define variables
$baseSalary = 50000;
$bonusPercentage = 0.1;
$numberOfYearsWorked = 3;

// Calculate total salary including bonus
$totalSalary =
    $baseSalary + $baseSalary * $bonusPercentage * $numberOfYearsWorked;

// Display the result
echo "Employee's base salary: $" . $baseSalary . "<br>";
echo "Bonus percentage: " . $bonusPercentage * 100 . "%<br>";
echo "Number of years worked: " . $numberOfYearsWorked . "<br><br>";
echo "Total salary including bonus: $" . $totalSalary;
?>

The output of the above program:

php syntax - example 2 output

In this example-

  • $baseSalary represents the base salary of an employee.
  • $bonusPercentage represents the bonus percentage (10% in this case).
  • $numberOfYearsWorked represents the number of years the employee has worked.

The program calculates the total salary by adding the base salary to the bonus amount (calculated based on the bonus percentage and the number of years worked). Finally, the result is displayed using the echo statements.

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.