SEO Title: PHP Variables, Constants, and Data Types Explained for Beginners
Meta Description: Learn PHP variables, constants, and data types with practical examples. A beginner-friendly guide to mastering PHP fundamentals.
PHP Variables, Constants, and Data Types Explained
In the previous article, we learned what PHP is and how it works. In this tutorial, we’ll explore three fundamental concepts every PHP developer must understand:
- Variables
- Constants
- Data Types
These concepts form the foundation of PHP programming and are used in every PHP application, including WordPress and Laravel projects.
What is a Variable in PHP?
A variable is used to store data that can be used and modified throughout your program.
In PHP, variables start with a dollar sign ($).
Syntax
$variable_name = value;
Example
<?php
$name = "Loki";
$age = 25;
echo $name;
echo "<br>";
echo $age;
?>
Output
Loki
25
Rules for Naming Variables
Valid Variable Names
$name = "John";
$user_age = 25;
$_city = "Surat";
Invalid Variable Names
$1name = "John";
$user-age = 25;
Rules
- Must start with
$ - Must begin with a letter or underscore
- Cannot start with a number
- Cannot contain spaces
- Variable names are case-sensitive
Example:
$name = "Loki";
$Name = "Ali";
echo $name;
echo $Name;
These are treated as different variables.
Assigning Values to Variables
String
$name = "Loki";
Integer
$age = 25;
Float
$price = 99.99;
Boolean
$isLoggedIn = true;
Displaying Variables
Using echo:
$name = "Loki";
echo $name;
Output:
Loki
Combining Variables with Text
Method 1: Concatenation
$name = "Loki";
echo "My name is " . $name;
Output:
My name is Loki
Method 2: Double Quotes
$name = "Loki";
echo "My name is $name";
Output:
My name is Loki
What is a Constant in PHP?
A constant is a value that cannot be changed after it has been defined.
Unlike variables, constants do not use the $ symbol.
Syntax
define("SITE_NAME", "My Website");
Example
<?php
define("SITE_NAME", "Loki Blog");
echo SITE_NAME;
?>
Output:
Loki Blog
Why Use Constants?
Constants are useful for values that never change.
Examples:
- Website name
- Database configuration
- API URLs
- Company information
define("COMPANY_NAME", "ABC Solutions");
define("SUPPORT_EMAIL", "support@example.com");
PHP Data Types
A data type defines what kind of value a variable can store.
PHP supports several data types.
1. String
A string is a sequence of characters.
$name = "Loki";
Example:
echo $name;
Output:
Loki
2. Integer
Integers are whole numbers.
$age = 25;
Examples:
$count = 100;
$year = 2026;
3. Float (Double)
Floats contain decimal values.
$price = 99.99;
Example:
$rating = 4.5;
4. Boolean
A Boolean can only have two values:
true
false
Example:
$isAdmin = true;
$isPaid = false;
5. Array
Arrays store multiple values in a single variable.
Example:
$colors = ["Red", "Green", "Blue"];
Accessing an array item:
echo $colors[0];
Output:
Red
6. NULL
NULL means the variable has no value.
Example:
$name = null;
Checking Data Types
PHP provides the var_dump() function.
Example:
$name = "Loki";
var_dump($name);
Output:
string(4) "Loki"
Another example:
$age = 25;
var_dump($age);
Output:
int(25)
Real-World Example
Imagine you’re creating a user profile.
<?php
$name = "Loki";
$age = 25;
$isPremium = true;
define("SITE_NAME", "Loki Tutorials");
echo "Welcome to " . SITE_NAME;
echo "<br>";
echo "Name: " . $name;
echo "<br>";
echo "Age: " . $age;
?>
Output:
Welcome to Loki Tutorials
Name: Loki
Age: 25
Common Beginner Mistakes
Forgetting the Dollar Sign
Wrong:
name = "Loki";
Correct:
$name = "Loki";
Using Quotes Around Constants
Wrong:
echo "SITE_NAME";
Correct:
echo SITE_NAME;
Variable Case Sensitivity
$name = "Loki";
$Name = "Ali";
These are different variables.