โœ๏ธ PHP Basics
Estimated reading: 3 minutes 44 views

๐Ÿงบ PHP Variables โ€“ Create, Assign, and Use Effectively

PHP variables are the foundation of dynamic programming. They are used to store and manipulate data, whether it’s text, numbers, or complex data structures like arrays and objects.

This guide will walk you through everything you need to know about PHP variables โ€” how to declare, assign, and use them, as well as scoping, types, best practices, and real-world examples.


๐Ÿ’ก What Are PHP Variables?

A variable in PHP is a symbolic name for storing data that can change during program execution. Variables in PHP are always prefixed with the dollar sign ($).

$name = "John";
$age = 25;

โœ๏ธ Declaring Variables in PHP

โœ… Syntax:

$variable_name = value;

โœ… Naming Rules:

  • Must start with $
  • Must begin with a letter or underscore (_)
  • Cannot start with a number
  • Can contain letters, numbers, and underscores

โŒ Invalid:

$1name = "Invalid";   // Starts with a number

๐Ÿ“‚ PHP Variable Types

PHP is a loosely typed language, meaning you don’t need to declare a variable’s data type.

๐Ÿ”ธ String

$greeting = "Hello World";

๐Ÿ”ธ Integer

$score = 100;

๐Ÿ”ธ Float (Double)

$price = 99.99;

๐Ÿ”ธ Boolean

$isValid = true;

๐Ÿ”ธ Array

$colors = array("Red", "Green", "Blue");

๐Ÿ”ธ Object

class Car {
    public $brand = "Toyota";
}
$car = new Car();

๐Ÿ”ธ NULL

$data = null;

๐Ÿงช Type Juggling

PHP automatically converts variable types when needed:

$number = "10";    // string
$sum = $number + 5;  // PHP converts "10" to 10 (integer)
echo $sum; // 15

๐Ÿ“ Variable Scope in PHP

๐Ÿ”ธ Local Scope

Variables declared inside a function:

function greet() {
  $message = "Hello";
}

๐Ÿ”ธ Global Scope

Accessible outside functions:

$siteName = "My Website";

To use a global variable inside a function:

function showSite() {
  global $siteName;
  echo $siteName;
}

๐Ÿ”ธ Static Scope

Retains value after the function ends:

function countVisits() {
  static $count = 0;
  $count++;
  echo $count;
}

๐Ÿงฐ PHP Superglobals

These are predefined global arrays accessible from anywhere in a script:

SuperglobalUse
$_GETHTTP GET variables
$_POSTHTTP POST variables
$_REQUESTGET + POST + COOKIE
$_SERVERServer environment info
$_SESSIONSession variables
$_COOKIECookie data
$_FILESUploaded files info
$_ENVEnvironment variables
$GLOBALSReferences all global variables

๐Ÿงผ Best Practices for Using Variables

โœ… Always initialize variables
โœ… Use meaningful names ($username > $x)
โœ… Follow camelCase or snake_case
โœ… Avoid unnecessary global variables
โœ… Comment complex variable usage
โœ… Clean up large unused variables


๐Ÿงช PHP Variable Example Exercises

Example 1: String Concatenation

$firstName = "John";
$lastName = "Doe";
echo "Welcome, " . $firstName . " " . $lastName;

Example 2: Arithmetic Operations

$a = 10;
$b = 5;
$sum = $a + $b;
echo $sum; // 15

Example 3: Associative Array

$user = array("name" => "Alice", "age" => 30);
echo $user["name"]; // Alice

Example 4: Global and Static Variables

$count = 1;
function showCount() {
  global $count;
  static $localCount = 0;
  $count++;
  $localCount++;
  echo "Global: $count, Static: $localCount";
}

๐Ÿงพ Summary

  • PHP variables store data that changes during script execution.
  • All variables begin with $ and follow specific naming rules.
  • PHP supports various data types and auto-converts them as needed.
  • Scope determines where a variable can be accessed.
  • Superglobals provide access to input, sessions, cookies, and more.
  • Best practices improve maintainability and debugging.

โ“ FAQ on PHP Variables

๐Ÿ”น Can I declare a variable without a value?

Yes. It will be initialized as NULL:

$var;

๐Ÿ”น What happens if I use an undeclared variable?

PHP may throw a notice or warning depending on error reporting settings.

๐Ÿ”น Is PHP case-sensitive for variables?

Yes, $name and $Name are different variables.

๐Ÿ”น How to check if a variable is set?

Use isset($var).


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿงบ PHP Variables

Or Copy Link

CONTENTS
Scroll to Top