0% found this document useful (0 votes)
0 views

PHP Variables

Uploaded by

PARKAVI.D
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

PHP Variables

Uploaded by

PARKAVI.D
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PHP Variables

1. Introduction

PHP (Hypertext Preprocessor) is a widely-used open-source server-side scripting


language that is especially suited for web development and can be embedded into
HTML. One of the fundamental building blocks in PHP, as in any programming
language, is the variable.

Variables in PHP are used to store data, such as numbers, text strings, arrays, and
objects, which can then be manipulated throughout a program. Understanding how
to declare, use, and manage variables is crucial for effective PHP programming.

2. Defining Variables in PHP

In PHP, a variable is declared using a dollar sign ($) followed by the name of the
variable.

Syntax:

$variable_name = value;

$: Every variable in PHP starts with a dollar sign.

variable_name: Must start with a letter (a–z, A–Z) or underscore (_) followed
by any number of letters, numbers, or underscores.

value: The information or data assigned to the variable.

Example:

<?php
$name = "John";
$age = 25;
?>

3. Rules for Naming Variables

A variable name must start with a letter or underscore (_).

A variable name cannot start with a number.

A variable name can only contain alphanumeric characters and underscores


(A-z, 0-9, and _).
Variable names are case-sensitive ($age and $Age are different variables).

4. Types of Variables

PHP is a loosely typed language, meaning you do not have to declare the data type of
a variable explicitly. PHP automatically converts the variable to the correct data type
based on its value.

Here are the main types:

Type Example
String $name = "Hello World";
Integer $age = 20;
Float/Double $price = 10.99;
Boolean $is_valid = true;
Array $colors = array("red", "green");
Object $car = new Car();
NULL $data = NULL;
Resource Special variables for external resources (like database connections)

5. Variable Scope in PHP

Scope refers to the context within which a variable is defined and accessible.

a) Local Scope:
Variables declared inside a function are only accessible within that function.

<?php
function myFunction() {
$x = 10; // local scope
echo $x;
}
myFunction();
// echo $x; // This would produce an error
?>

b) Global Scope:
Variables declared outside of any function have a global scope and can be accessed
anywhere outside functions.

<?php
$x = 5; // global scope

function myTest() {
global $x;
echo $x;
}
myTest();
?>

c) Static Scope:
When a function is called, all its variables are normally deleted after execution. If you
want a local variable not to be deleted, use the static keyword.

<?php
function myCounter() {
static $count = 0;
echo $count;
$count++;
}
myCounter();
myCounter();
myCounter();
?>

6. Superglobals

PHP provides several built-in variables that are always accessible, regardless of
scope.

$GLOBALS: References all variables available in global scope

$_SERVER: Server and execution environment information.

$_REQUEST: Request variables from form input.

$_POST: Form data submitted via POST method.

$_GET: Form data submitted via GET method.

$_FILES: File upload data.

$_ENV: Environment variables.

$_COOKIE: HTTP cookies.

$_SESSION: Session variables.

Example using $GLOBALS:

<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>

7. Variable Variables

PHP allows you to create a variable name dynamically.

Example:

<?php
$a = "hello";
$$a = "world";

echo $a; // outputs: hello


echo $$a; // outputs: world
echo $hello; // outputs: world
?>

Here, $$a means the value of $a ("hello") is now a variable itself.

8. Best Practices for Using PHP Variables

 Use meaningful names: $userAge is better than $ua.


 Maintain consistent casing: Decide on camelCase ($userName) or snake_case
($user_name).
 Initialize variables: Avoid using variables that haven't been set.
 Avoid overwriting superglobals: Don’t name your own variables $_POST, $_GET,
etc
 Use type casting if necessary: PHP allows type casting like (int)$var,
(string)$var.

9. Conclusion

Variables are an essential part of PHP programming. They enable developers to write
dynamic and flexible code by storing and manipulating data efficiently. By
understanding how to properly declare, use, and manage variables — along with
their scopes and types — programmers can build more robust and maintainable PHP
applications.
Mastering the handling of variables is the foundation for moving on to more complex
concepts such as functions, classes, and data management in PHP.

You might also like