PHP Variables

Store values with the $ sign.

✍️ 2 exercises in this lesson

PHP variables start with $ followed by the name: $name = "Ali";

  • Names start with a letter or underscore and are case-sensitive.
  • PHP is loosely typed: you do not declare a type.
  • Join strings with the dot operator .
  • Inside double quotes, variables are replaced by their values: "Hello $name".

Scope

Variables declared outside a function are not visible inside it. Use parameters, or the global keyword (sparingly). static keeps a local variable's value between calls.

Key Takeaways: PHP Variables

Mastering PHP Variables helps you write clean and reliable PHP code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

PHP Variables Code Example

PHP
<?php
$name = "Sara";
$age = 24;
$price = 1999.50;

echo "Name: $name, Age: $age<br>";
echo 'Single quotes do not parse $name<br>';
echo "Price: Rs " . number_format($price, 2);
Output
Name: Sara, Age: 24
Single quotes do not parse $name
Price: Rs 1,999.50
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

PHP Variables Practice Exercises

Check what you learned in “PHP Variables”.

/ 2 solved
Fill in the blank

Create a variable.

name = "Sara";
✓ Correct! Not quite. Try again.
Multiple choice

Which operator joins two strings in PHP?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services