codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

3 / 56 topics
1Getting Started with PHP2PHP Syntax3Variables in PHP4Data Types in PHP5Constants in PHP6Operators in PHP
Tutorials/PHP/Variables in PHP
🐘PHP

Variables in PHP

Updated 2026-05-15
10 min read

Variables in PHP

Introduction

Welcome to the world of PHP! In this tutorial, we will dive into one of the fundamental concepts of any programming language: variables. Variables are essential for storing data that can be manipulated and used throughout your code. Whether you're a beginner or an intermediate developer, understanding how to declare and use variables in PHP is crucial.

Concept

In PHP, variables are used to store information. They act as containers for values, which can be numbers, strings, arrays, objects, etc. Variables in PHP start with the $ symbol followed by the variable name. Variable names must start with a letter or an underscore and can only contain letters, numbers, and underscores.

Declaring Variables

To declare a variable in PHP, you simply assign a value to it using the = operator. Here's the basic syntax:

$variable_name = value;

For example, let's create a variable called $greeting and assign it the string "Hello, World!":

php
1$greeting = "Hello, World!";
2echo $greeting;

When you run this code, it will output:

Output
Hello, World!

Types of Variables

PHP is a dynamically typed language, which means that you don't need to explicitly declare the type of a variable. PHP automatically determines the type based on the value assigned to it.

Here are some common types of variables in PHP:

  • Strings: A sequence of characters enclosed in quotes.
php
1$name = "Alice";
  • Integers: Whole numbers without a decimal point.
php
1$age = 30;
  • Floats (or Doubles): Numbers with a decimal point.
php
1$height = 5.9;
  • Booleans: Represents true or false values.
php
1$isStudent = true;
  • Arrays: A collection of values.
php
1$colors = array("red", "green", "blue");
  • Objects: Instances of a class.
php
1$person = new Person();

Variable Scope

Variables in PHP have different scopes depending on where they are declared:

  • Local Scope: Variables declared inside a function are local to that function and cannot be accessed outside it.
php
1function myFunction() {
2 $localVar = "I am local";
3 echo $localVar;
4}
5myFunction();
6// echo $localVar; // This will cause an error
  • Global Scope: Variables declared outside any function are global and can be accessed anywhere in the script.
php
1$globalVar = "I am global";
2function myFunction() {
3 global $globalVar;
4 echo $globalVar;
5}
6myFunction();
  • Static Scope: Variables declared with the static keyword retain their value between function calls.
php
1function myFunction() {
2 static $count = 0;
3 $count++;
4 echo $count;
5}
6myFunction(); // Outputs: 1
7myFunction(); // Outputs: 2

Examples

Let's look at some practical examples to solidify our understanding of variables in PHP.

Example 1: Basic Variable Usage

php
1<?php
2$name = "Alice";
3$age = 30;
4$isStudent = true;
5
6echo "Name: " . $name . "<br>";
7echo "Age: " . $age . "<br>";
8echo "Is Student: " . ($isStudent ? 'Yes' : 'No');
9?>
Output
Name: Alice
Age: 30
Is Student: Yes

Example 2: Variable Scope

php
1<?php
2$globalVar = "I am global";
3
4function myFunction() {
5 $localVar = "I am local";
6 echo $localVar . "<br>";
7}
8
9myFunction();
10echo $globalVar;
11?>
Output
I am local
I am global

Example 3: Static Variables

php
1<?php
2function myFunction() {
3 static $count = 0;
4 $count++;
5 echo "Count: " . $count . "<br>";
6}
7
8myFunction(); // Outputs: Count: 1
9myFunction(); // Outputs: Count: 2
10myFunction(); // Outputs: Count: 3
11?>
Output
Count: 1
Count: 2
Count: 3

What's Next?

Now that you have a solid understanding of variables in PHP, the next step is to explore different data types and how they can be used effectively in your code. We will cover this topic in the next section titled "Data Types in PHP".

Stay tuned for more tutorials on PHP and keep coding!


PreviousPHP SyntaxNext Data Types in PHP

Recommended Gear

PHP SyntaxData Types in PHP