- Declaring Variables in PHP
- PHP Data Types
- PHP Arrays
- Types of PHP Operators
- PHP Strings
- PHP Expressions
- PHP Control Structures
- PHP Functions
- PHP Form Handling – Read Form Inputs & Handle File Uploads
- How to Connect PHP to MySQL Database Using MySQLi
- Executing Simple Queries in PHP
- Handling Results in PHP
- Handling Sessions and Cookies
Declaring Variables in PHP
Introduction
In PHP, variables are like containers that hold information or data. You can give them short names like $x
, $y
, or more meaningful names like $age
, $carName
, or $total_volume
. Understanding how to declare and use variables properly is essential for writing effective PHP programs.
Rules for Declaring Variables in PHP
A variable starts with the
$
sign, followed by the variable name.The variable name must start with a letter (A-Z, a-z) or an underscore
_
.Variable names cannot start with a number.
Variable names can only contain letters, numbers, and underscores (A–Z, 0–9,
_
).Variable names are case-sensitive — for example,
$age
and$AGE
are different variables.Use descriptive names to make your code easy to understand.
Example: Declaring Variables and Outputting Them
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
Output:
Hello world!
5
10.5
Outputting Variables in PHP
Example 1: Using Variables Inside Strings
<?php
$txt = "cmrtpoint.com";
echo "I love $txt!";
?>
Output:
I love cmrtpoint.com!
Example 2: Concatenating Strings and Variables
<?php
$txt = "cmrtpoint.com";
echo "I love " . $txt . "!";
?>
Output:
I love cmrtpoint.com!
Example 3: Using Variables in Arithmetic Operations
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
Output:
9
PHP Is a Loosely Typed Language
You don’t need to declare the data type of a variable in PHP. PHP automatically converts the variable to the correct data type depending on the value you assign. This is different from languages like C, C++, or Java, where you must declare the type explicitly.
Variable Scope in PHP
A variable’s scope is the part of the script where it can be accessed or used. PHP has three main variable scopes:
Local Scope – Variables declared inside a function, accessible only within that function.
Global Scope – Variables declared outside a function, accessible throughout the script (except inside functions unless explicitly declared global).
Static Scope – Variables that retain their value even after the function finishes executing.
Global and Local Variables
Example: Global Scope Variable
<?php
$x = 5; // global scope
function myTest() {
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
Output:
Variable x inside function is:
Variable x outside function is: 5
Example: Local Scope Variable
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
Output:
Variable x inside function is: 5
Variable x outside function is:
Using the global
Keyword
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // Outputs 15
?>
Using the $GLOBALS
Array
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // Outputs 15
?>
The static
Keyword
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest(); // Outputs 0
echo "<br>";
myTest(); // Outputs 1
echo "<br>";
myTest(); // Outputs 2
?>
Conclusion
Declaring variables in PHP is simple but must follow naming rules. Understanding how variables work — especially their scope (local, global, static) — helps you write cleaner and more efficient code. PHP’s flexibility with data types and variable scope makes it beginner-friendly and powerful.