PHP Syntax

PHP (Hypertext Preprocessor) is a widely-used open-source scripting language suited for web development. Here's a quick overview of PHP syntax:


1. PHP Tags

PHP code is embedded in HTML using  tags:

echo "Hello, world!"; ?>

2. Variables

Variables start with a $ sign:

$name = "Alice"; $age = 25; ?>

3. Data Types

Common types: String, Integer, Float, Boolean, Array, Object, NULL.


4. Echo and Print

Used to output text:

echo "Hello!"; print "World!";

5. Conditional Statements

if ($age >= 18) { echo "Adult"; } else { echo "Minor"; }

6. Loops

// While loop while ($i < 5) { echo $i; $i++; } // For loop for ($i = 0; $i < 5; $i++) { echo $i; }

7. Functions

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

8. Arrays

$fruits = array("Apple", "Banana", "Cherry"); echo $fruits[0]; // Apple

9. Superglobals

Predefined variables like:

  • $_GET, $_POST – form data

  • $_SERVER, $_SESSION, $_COOKIE