codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

2 / 56 topics
1Getting Started with PHP2PHP Syntax3Variables in PHP4Data Types in PHP5Constants in PHP6Operators in PHP
Tutorials/PHP/PHP Syntax
🐘PHP

PHP Syntax

Updated 2026-05-15
10 min read

PHP Syntax

Introduction

PHP (Hypertext Preprocessor) is a widely-used open-source scripting language that is especially suited for web development. It allows you to embed code within HTML and execute it on the server side. Understanding the core syntax of PHP is essential for any developer looking to work with this powerful language.

In this tutorial, we will cover the basic syntax of PHP, including how to write and structure your PHP scripts. We'll start by setting up a simple PHP environment and then dive into key concepts like comments, variables, data types, operators, control structures, and functions.

Concept

Basic Structure

A PHP script is executed on the server, and the output is sent to the browser as plain HTML. The basic structure of a PHP script includes opening (<?php) and closing (?>) tags. Anything between these tags is considered PHP code.

<?php
// Your PHP code goes here
?>

Comments

PHP supports single-line comments, multi-line comments, and inline comments:

  • Single-line comments start with //:

    // This is a single-line comment
    
  • Multi-line comments are enclosed within /* */:

    /*
     * This is a multi-line comment.
     * It can span multiple lines.
     */
    
  • Inline comments start with # (though this is less common):

    # This is an inline comment
    

Variables

Variables in PHP are denoted by the $ symbol followed by the variable name. Variable names must start with a letter or underscore and can only contain letters, numbers, and underscores.

<?php
$greeting = "Hello, World!";
echo $greeting; // Outputs: Hello, World!
?>

Data Types

PHP supports several data types:

  • String: A sequence of characters enclosed in quotes.

    <?php
    $name = "Alice";
    ?>
    
  • Integer: Whole numbers without a decimal point.

    <?php
    $age = 25;
    ?>
    
  • Float: Numbers with a decimal point.

    <?php
    $height = 5.9;
    ?>
    
  • Boolean: Represents true or false values.

    <?php
    $isStudent = true;
    ?>
    
  • Array: An ordered map of key-value pairs.

    <?php
    $fruits = array("apple", "banana", "cherry");
    ?>
    
  • Object: Instances of classes.

    <?php
    class Car {
        public $color;
        function __construct($color) {
            $this->color = $color;
        }
    }
    
    $myCar = new Car("red");
    ?>
    
  • NULL: Represents a variable with no value.

    <?php
    $nullVar = null;
    ?>
    

Operators

PHP supports various operators for arithmetic, comparison, logical, string, and array operations.

Arithmetic Operators

<?php
$a = 10;
$b = 5;

echo $a + $b; // Addition: 15
echo $a - $b; // Subtraction: 5
echo $a * $b; // Multiplication: 50
echo $a / $b; // Division: 2
echo $a % $b; // Modulus: 0
?>

Comparison Operators

<?php
$a = 10;
$b = 5;

var_dump($a == $b); // Equal: bool(false)
var_dump($a != $b); // Not equal: bool(true)
var_dump($a > $b);  // Greater than: bool(true)
var_dump($a < $b);  // Less than: bool(false)
var_dump($a >= $b); // Greater than or equal to: bool(true)
var_dump($a <= $b); // Less than or equal to: bool(false)
?>

Logical Operators

<?php
$a = true;
$b = false;

var_dump($a && $b); // And: bool(false)
var_dump($a || $b); // Or: bool(true)
var_dump(!$a);      // Not: bool(false)
?>

String Operators

<?php
$str1 = "Hello";
$str2 = " World";

echo $str1 . $str2; // Concatenation: Hello World
echo $str1 . ", " . $str2 . "!"; // Concatenation with comma and space: Hello, World!
?>

Array Operators

<?php
$array1 = array(1, 2, 3);
$array2 = array(4, 5, 6);

$result = $array1 + $array2; // Union: array(0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6)
?>

Control Structures

PHP supports various control structures for decision-making and looping.

If-Else Statements

<?php
$age = 18;

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

Switch Statements

<?php
$day = "Monday";

switch ($day) {
    case "Monday":
        echo "It's Monday!";
        break;
    case "Tuesday":
        echo "It's Tuesday!";
        break;
    default:
        echo "It's another day.";
}
?>

Loops

For Loop
<?php
for ($i = 0; $i < 5; $i++) {
    echo "Number: " . $i . "<br>";
}
?>
While Loop
<?php
$i = 0;
while ($i < 5) {
    echo "Number: " . $i . "<br>";
    $i++;
}
?>
Do-While Loop
<?php
$i = 0;
do {
    echo "Number: " . $i . "<br>";
    $i++;
} while ($i < 5);
?>

Functions

Functions in PHP are defined using the function keyword. They can take parameters and return values.

<?php
function greet($name) {
    return "Hello, " . $name . "!";
}

echo greet("Alice"); // Outputs: Hello, Alice!
?>

Examples

Let's put together a simple PHP script that demonstrates some of the concepts we've covered.

<?php
// Define variables
$name = "John";
$age = 30;

// Function to display information
function displayInfo($name, $age) {
    echo "Name: " . $name . "<br>";
    echo "Age: " . $age . "<br>";
}

// Call the function
displayInfo($name, $age);

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

// Loop to display numbers
for ($i = 1; $i <= 5; $i++) {
    echo "Number: " . $i . "<br>";
}
?>

Running the Script

To run this script, save it with a .php extension (e.g., example.php) and place it in your web server's document root. Then, access it through your browser by navigating to http://localhost/example.php.

What's Next?

In the next section, we will delve deeper into variables in PHP, exploring how to declare, manipulate, and use them effectively in your scripts.

Stay tuned for more tutorials on PHP!


PreviousGetting Started with PHPNext Variables in PHP

Recommended Gear

Getting Started with PHPVariables in PHP