start learning
Image 1

Basic Syntax

PHP (Hypertext Preprocessor) is a popular server-side scripting language used for web development. It is embedded within HTML code to create dynamic web pages.

Let's start with some basic PHP syntax and examples to help you understand how it works :

PHP Tags

PHP code is typically enclosed within <?php and ?> tags. Anything outside these tags is treated as regular HTML.

// PHP code goes here


Variables in PHP start with a dollar sign $ followed by the variable name. PHP is loosely typed, so you don't need to declare variable types.

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


You can use the echo or print statement to output data to the web browser.

echo "Hello, World!";


PHP supports single-line and multi-line comments.

// This is a single-line comment
This is a
multi-line comment

Data Types

PHP supports various data types, including integers, floats, strings, booleans, arrays, and objects.

$intVar = 42;
$floatVar = 3.14;
$stringVar = "Hello, PHP!";
$boolVar = true;


You can concatenate strings using the . operator.

$first_name = "John";
$last_name = "Doe";
$full_name = $first_name . " " . $last_name;

Conditional Statements

PHP supports standard conditional statements like if, else if, and else.

$age = 20;
if ($age < 18) {
    echo "You are a minor.";
} elseif ($age >= 18 && $age < 65) {
    echo "You are an adult.";
} else {
    echo "You are a senior citizen.";


PHP has for, while, and foreach loops for iterating through data.

for ($i = 0; $i < 5; $i++) {
    echo $i . " ";
$numArray = [1, 2, 3, 4, 5];
foreach ($numArray as $num) {
    echo $num . " ";


You can create and use functions to encapsulate reusable code.

function add($a, $b) {
    return $a + $b;
$result = add(5, 3); // $result will be 8


PHP has several predefined arrays called superglobals that provide access to various global variables like $_GET, $_POST, and $_SESSION.

$user_input = $_POST['user_input'];

These are some of the basic PHP syntax elements and examples to get you started. PHP is a versatile language used for building dynamic websites and web applications, and you can explore more advanced topics like database connectivity, object-oriented programming, and frameworks as you become more familiar with the basics.