Basic PHP syntax and variables: script structure and tags; declaring variables and constants; echo and print; comments and formatting conventions

PHP's grammar in one page: code lives between <?php and ?>, every statement ends with a semicolon, variables start with $, constants do not change, and echo prints output.

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

A new language, mostly familiar

You have written C, Java, JavaScript, VB.NET and Dart. PHP is your sixth language, and the good news is that 80% of it will feel familiar: braces, semicolons, if and for, all present.

The 20% that is new is worth learning precisely, because small syntax slips cause most beginner PHP bugs: the <?php tags, the $ on every variable, the DOT for joining strings (not +), and the difference between single and double quotes. Master this grammar and the event portal's pages will read cleanly.

Theory

Structure, variables, constants

Tags: PHP code lives between <?php and ?>, embedded anywhere in HTML. Every statement ends with a semicolon.

Variables start with $: $name = "Riya";. They are case-sensitive ($name and $Name differ) and loosely typed (you never declare the type; PHP infers it from the value).

Constants never change once set: define one with define("MAX_SEATS", 350); or const MAX_SEATS = 350;. By convention they are UPPERCASE and take no `$`. Trying to reassign a constant is an error.

Practical

The grammar, with predicted output

<?php
  $event = "Garba Night";      // variable: starts with $
  $seats = 350;                // loosely typed: no type declared
  define("PORTAL", "EventHub"); // a constant: no $, uppercase

  // echo with the DOT operator to join strings:
  echo "Welcome to " . PORTAL . "<br>";   // Welcome to EventHub
  echo "Event: " . $event . "<br>";        // Event: Garba Night

  // Double quotes expand variables; single quotes do NOT:
  echo "Seats left: $seats <br>";  // Seats left: 350
  echo 'Seats left: $seats';       // Seats left: $seats  (literal!)
?>

Theory

echo, print, and the quote rule

echo and print both output text. Differences worth a mark: echo can take several comma-separated arguments and returns nothing; print takes one argument and returns 1 (so it can sit inside an expression). In practice echo is the everyday choice.

Joining strings uses the dot operator: "Hi " . $name. Using + (as in JavaScript) does arithmetic in PHP, a classic cross-language slip.

The quote rule matters constantly: double quotes expand variables ("Hello $name" becomes Hello Riya), while single quotes are literal ('Hello $name' prints the dollar sign and the word). Comments are // or # for one line, /* */ for blocks.

Quiz

$name = "Riya"; What does echo 'Hello $name'; output? (note the SINGLE quotes)

  1. Hello Riya
  2. Hello $name
  3. Hello
  4. An error: variables cannot be used in strings
Show the answer

Hello $name

Single-quoted strings are LITERAL: PHP does not expand variables inside them, so $name prints as the 5 characters $name, giving 'Hello $name'. To get 'Hello Riya' you would use DOUBLE quotes: "Hello $name". This single-vs-double distinction is one of PHP's most common beginner bugs and a frequent exam question. Option A is what double quotes would give. Option C imagines the variable vanishing. Option D is false: variables work fine in double-quoted strings. Rule: double quotes interpolate, single quotes are literal.

Think first

Spot the four beginner slips

A student writes: <?php echo 'Total: ' + $seats ?> and gets unexpected output with a warning. Find as many mistakes as you can, then tap.

Show the answer

Two real problems. (1) The + operator: PHP joins strings with the DOT (.), not +; using + tries to ADD, and 'Total: ' as a number is 0, so you get a numeric result plus a warning, not the text you wanted. Correct: 'Total: ' . $seats. (2) The missing semicolon: the statement inside <?php ... ?> should end with ; before ?>. (A subtler point: single quotes here are fine since there is no variable inside them to interpolate.) The 2 lessons: dot to join strings, and end every statement with a semicolon: the 2 slips that produce most day-one PHP errors.

Watch out

Syntax traps

+ instead of .: PHP joins strings with the dot; + does arithmetic.

Single vs double quotes: only double quotes expand $variables.

Missing semicolons: every statement needs one; a missing semicolon before ?> is a parse error.

Forgetting the $: variables ALWAYS start with $; constants NEVER do.

Case sensitivity: $Name is not $name (variables are case-sensitive; note function names are not, but rely on consistent casing anyway).

Theory

Grammar down, meaning next

You can now write and output PHP correctly: tags, variables, constants, echo, and the all-important quote and dot rules. Next the portal needs to work with real DATA of different kinds: strings, numbers, booleans, and especially ARRAYS (a registration list is an array), plus the OPERATORS to compute with them. Data types and operators are the next lesson.

Summary

Key takeaways

  • PHP code lives between <?php and ?>; every statement ends with a semicolon.
  • Variables start with $, are case-sensitive, and are loosely typed (no type declared).
  • Constants (define() or const) are UPPERCASE by convention, take no $, and cannot change.
  • echo outputs text (multiple args, no return); print takes one arg and returns 1.
  • Join strings with the DOT operator (.), not + (which does arithmetic).
  • Double quotes expand $variables; single quotes are literal; comments are // # or / /.
  • Memory hook: dollar for variables, dot to join, double quotes to expand.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Core PHP Programming

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Basic PHP syntax and variables: script structure and tags; declaring variables and constants; echo and print; comments and formatting conventions · Web Framework and Services (Major-12) · Gri-Learn