Introduction to PHP

Variables & Data Types

A PHP variable starts with a dollar sign $name = "Ali";. PHP is loosely typed, meaning you don't declare a type; it's decided automatically from the value. Common types are string (text), int (whole numbers), float (decimals), bool (true/false), and array.

Why this matters

  • ·No need to declare types faster to write for small scripts
  • ·gettype() lets you check a variable's type anytime

Common mistakes

  • ·Forgetting the $ sign in front of a variable name causes an error
  • ·Mixing types carelessly (e.g. adding a string to a number) can give unexpected results
Your codePHP
Expected output
Name: Ali, Age: 20

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Frequently asked questions

Use the code editor above edit the example freely and check the expected output against your own reasoning.

Practice questions

Question 1

Create two variables $city and $year and echo them together in one sentence.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Quick check

Just for this topic answer honestly, then check yourself.

1 / 1

Which symbol starts every PHP variable?