PHP Syntax

A PHP script starts with <?php and ends with ?>. Every statement ends with a semicolon.

PHP
<?php
  echo "Hello";
  echo " world!";
?>

The closing tag is optional

In a file that is pure PHP (no HTML after it), leave off the closing ?>. This is the recommended style — a stray space or newline after ?> gets sent to the browser and can break headers and cookies later.

PHP
<?php
  echo "This file is pure PHP — no closing tag needed.";

Case sensitivity

Keywords (if, echo, class, function) and function names are case-insensitive. Variable names are case-sensitive.

PHP
<?php
  ECHO "works";
  echo "also works";

  $name = "Sita";
  echo $NAME; // Warning: undefined variable — different variable!

Write everything in lowercase anyway; mixed case is legal but nobody does it.

echo vs print

Both output text. echo is marginally faster and can take several comma-separated arguments; print returns 1 so it can be used in an expression. Use echo.

PHP
<?php
  echo "One", " ", "call";
  print "Also fine";

Knowledge Check

4 questions — test your understanding

1

What is the purpose of PDO in PHP?

2

Why use prepared statements instead of string concatenation?

3

What does `PDO::FETCH_ASSOC` return?

4

What does `try/catch` do in PHP?