PHP Syntax
PHP is a popular server-side scripting language used for web development. It stands for "Hypertext Preprocessor" and is known for its simplicity and flexibility. Here’s an overview of PHP syntax with examples:
1. PHP Tags
PHP code is embedded in HTML using the <?php
and ?>
tags.
<?php
// This is a single-line comment
echo "Hello, World!";
?>
2. Variables
Variables in PHP start with a dollar sign $
and do not need to be declared before use.
<?php
$variable = "Hello, World!";
echo $variable;
?>
3. Data Types
PHP supports several data types: integers, floats, strings, arrays, objects, and booleans.
<?php
$integer = 10;
$float = 10.5;
$string = "Hello";
$boolean = true;
?>
4. Strings
You can use single quotes or double quotes for strings. Double quotes allow for variable interpolation.
<?php
$name = "John";
echo 'Hello, $name'; // Outputs: Hello, $name
echo "Hello, $name"; // Outputs: Hello, John
?>
5. Concatenation
Strings can be concatenated using the .
operator.
<?php
$firstName = "John";
$lastName = "Doe";
echo $firstName . " " . $lastName; // Outputs: John Doe
?>
6. Arrays
PHP supports both indexed and associative arrays.
Indexed Array:
<?php
$fruits = array("Apple", "Banana", "Cherry");
echo $fruits[1]; // Outputs: Banana
?>
Associative Array:
<?php
$person = array("first_name" => "John", "last_name" => "Doe");
echo $person["first_name"]; // Outputs: John
?>
7. Loops
PHP supports several types of loops: for
, while
, do-while
, and foreach
.
For Loop:
<?php
for ($i = 0; $i < 5; $i++) {
echo $i . " ";
}
// Outputs: 0 1 2 3 4
?>
While Loop:
<?php
$i = 0;
while ($i < 5) {
echo $i . " ";
$i++;
}
// Outputs: 0 1 2 3 4
?>
Foreach Loop (for arrays):
<?php
$colors = array("Red", "Green", "Blue");
foreach ($colors as $color) {
echo $color . " ";
}
// Outputs: Red Green Blue
?>
8. Conditional Statements
PHP uses if
, else
, elseif
, and switch
for conditional statements.
If-Else:
<?php
$age = 18;
if ($age < 18) {
echo "Minor";
} elseif ($age == 18) {
echo "Just turned adult";
} else {
echo "Adult";
}
// Outputs: Just turned adult
?>
Switch:
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week";
break;
case "Friday":
echo "End of the week";
break;
default:
echo "Midweek";
}
// Outputs: Start of the week
?>
9. Functions
Functions are declared using the function
keyword.
<?php
function greet($name) {
return "Hello, " . $name;
}
echo greet("Alice"); // Outputs: Hello, Alice
?>
10. Superglobals
PHP has built-in global arrays such as $_GET
, $_POST
, $_SESSION
, and $_COOKIE
.
Example of Using $_GET
:
<?php
// URL: example.com/?name=John
echo "Hello, " . $_GET["name"];
// Outputs: Hello, John
?>
These are the basics of PHP syntax. Each topic can be explored in much greater detail depending on what you need!
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.