PHP Variables

PHP Tutorial

PHP Variables – Complete Beginner Guide With Examples

What Are PHP Variables?

A variable in PHP is a container used to store data. This data can be a number, text, true/false value, or even more complex information like arrays and objects.

Variables allow your website to store and manipulate dynamic information such as:

  • User names

  • Form inputs

  • Product prices

  • Database results

  • Calculations

Without variables, PHP cannot create dynamic websites.


Why Do We Need Variables?

Imagine you want to store a user’s name:

John

If you directly write it in HTML, it never changes.

But using a variable:

Now you can:

  • Change it anytime

  • Store different values

  • Use it in calculations

  • Reuse it multiple times

Variables make your website dynamic.


How to Declare a Variable in PHP

In PHP, every variable:

  • Starts with $

  • Is followed by the variable name

  • Uses = to assign a value

Basic Syntax:

Example:


Rules for PHP Variable Names

When creating variables, follow these rules:

  1. Must start with $

  2. Must begin with a letter or underscore

  3. Cannot start with a number

  4. Can contain letters, numbers, and underscores

  5. Case-sensitive

Valid Examples:

Invalid Examples:


PHP Variable Case Sensitivity

PHP variables are case-sensitive.

Example:

$name and $Name are different variables.

Always use consistent naming.


PHP Data Types in Variables

PHP automatically assigns a data type depending on the value.

 String

Used for text.

 Integer

Whole numbers.

 Float

Decimal numbers.

 Boolean

True or false values.

 Array

Stores multiple values.


Printing Variables

To display variables, use:

echo

print

Both display output to the browser.


Combining Variables With Text

You can combine variables with strings using:

Method 1: Concatenation (.)

Method 2: Double Quotes

Important:
Double quotes allow variable expansion.
Single quotes do NOT.

Wrong:

Output:

My name is $name

Changing Variable Values

Variables can be updated anytime.

Example:

Output:

5

The latest value replaces the old one.


Variable Scope in PHP

Scope means where a variable can be accessed.

 Local Scope

Variables declared inside a function can only be used inside that function.

Outside the function, $x cannot be used.


 Global Scope

Variables declared outside a function are global.


 


 Static Variables

Static variables keep their value between function calls.


 

Output:

123

PHP Variable Variables

PHP allows dynamic variable names.

Example:


 

This creates:

$name = "John"

Use carefully, as it can reduce code readability.


Common Beginner Mistakes

 Forgetting $

Wrong:

Correct:


 Case Mistakes

$name is not same as $Name.


 Using Single Quotes for Variables

Wrong:

Correct:


Best Practices for PHP Variables

  •  Use meaningful names
  • Avoid very short names like $a, $b
  •  Follow consistent naming style
  •  Use camelCase or snake_case
  •  Comment complex logic

Example:

This improves readability.


Real-World Example Using Variables

Example: Simple Calculator


 

Output:

Sum is: 15

Variables allow dynamic calculations.


Why PHP Variables Are Important

Variables are essential because they allow you to:

  • Handle user input

  • Store database data

  • Perform calculations

  • Create login systems

  • Build dynamic websites

Without variables, PHP would not function properly.


FAQs About PHP Variables

1. What is a variable in PHP?

A variable is a container used to store data such as text, numbers, or arrays.


2. Why does PHP use $ before variable names?

The dollar sign helps PHP identify variables.


3. Are PHP variables case sensitive?

Yes. $name and $Name are different variables.


4. Can variable values change?

Yes. You can reassign a variable anytime.


5. What is variable scope?

Scope defines where a variable can be accessed (local, global, static).


Conclusion

PHP variable are fundamental building blocks of PHP programming. They store data, perform calculations, and help create dynamic content.

To master PHP:

  • Understand how variables are declared

  • Learn data types

  • Practice variable scope

  • Avoid beginner mistakes

Strong understanding of variables will make learning loops, functions, and databases much easier.

You may also like...