PHP Variables (original) (raw)
Last Updated : 11 Apr, 2025
A variable in PHP is a container used to store data such as numbers, strings, arrays, or objects. The value stored in a variable can be changed or updated during the execution of the script.
- All variable names start with a dollar sign ($).
- Variables can store different data types, like integers, strings, arrays, etc.
- PHP is loosely typed, so you don’t need to declare a data type explicitly.
- Variable values can change during the script’s execution.
Declaring Variables in PHP
To declare a variable in PHP, you simply assign a value to it using the $ symbol followed by the variable name. PHP variables are case-sensitive and must start with a letter or an underscore, followed by any number of letters, numbers, or underscores.
**Syntax:
$variable_name = value;
**Now, let us understand with the help of the example:
PHP `
`
Variable Naming Conventions
In PHP, it’s important to follow certain naming conventions for PHP variables to ensure readability and maintainability:
- **Start with a Letter or Underscore: Variable names must begin with a letter or an underscore (_), not a number.
- **Use Descriptive Names: Variable names should be descriptive of their purpose, e.g., userName,userName, userName,totalAmount.
- **Case Sensitivity: PHP variable names are case-sensitive, meaning nameandname and nameandName are different variables.
- **Avoid Reserved Words: Do not use PHP reserved words or keywords as variable names (e.g., function, class, echo).
**Example of Valid and Invalid Variable Names
PHP `
`
**PHP Variable Scope
The scope of a variable refers to where it can be accessed within the code. PHP variables can have local, global, static, or superglobal scope.
**1. Local Scope or Local Variable
Variables declared within a function have local scope and cannot be accessed outside the function. Any declaration of a variable outside the function with the same name (as within the function) is a completely different variable.
**Example: This example shows the local variable in PHP.
PHP `
`
Output
Variable num inside function is: 50 Variable num outside function is: 60
**2. Global Scope or Global **Variable
The variables declared outside a function are called global variables. These variables can be accessed directly outside a function. To get access within a function we need to use the “global” keyword before the variable to refer to the global variable.
**Example: This example shows the Global Variables in PHP.
PHP `
`
Output
Variable num inside function: 20 Variable num outside function: 20
**3. Static Variables
It is the characteristic of PHP to delete the variable. Once it completes its execution and the memory is freed. But sometimes we need to store the variables even after the completion of function execution. To do this, we use the static keywords and the variables are then called static variables. PHP associates a data type depending on the value for the variable.
**Example: This example shows the Static variable in PHP.
PHP `
`
**Note: You must have noticed that $num regularly increments even after the first function call but _$sum doesn’t. This is because _$sum is not static, and its memory is freed after the execution of the first function call.
4. Superglobals
Superglobals are built-in arrays in PHP that are accessible from anywhere in the script, including within functions. Common superglobals include GET,_GET, GET,_POST, SESSION,_SESSION, SESSION,_COOKIE, SERVER,and_SERVER, and SERVER,and_GLOBALS.
PHP `
`
Variable Variables
- PHP allows us to use dynamic variable names, called variable variables.
- Variable variables are simply variables whose names are dynamically created by another variable’s value.
**Example: This example shows the declaration of a variable by the use of the another variable.
PHP `
a=′hello′;//helloisvalueofvariablea = 'hello'; //hello is value of variable a=′hello′;//helloisvalueofvariablea a = 'World'; //$($a) is equals to $(hello) echo hello;//hello; //hello;//hello is World i.e. $hello is new variable with value 'World' ?>`
Best Practices for Using Variables
- Use descriptive names like totalAmountinsteadoftotalAmount instead of totalAmountinsteadofx.
- Use camelCase consistently.
- Avoid using reserved keywords as variable names.
- Initialize variables before use.
Conclusion
Variables are a basic yet powerful part of PHP. They allow you to store and manipulate data dynamically as your script runs. By understanding how to declare, name, and use variables properly—along with knowing their scope—you’ll be better prepared to write efficient and organized PHP code.