PHP Variables
In PHP, variables are used to store data, which can then be used and manipulated throughout your script. Here’s a detailed overview of how variables work in PHP:
Declaring Variables
- A variable in PHP starts with the
$
sign, followed by the name of the variable. - Variable names in PHP are case-sensitive.
php<?php
$message = "Hello, World!";
$age = 25;
$price = 19.99;
?>
Variable Naming Rules
- A variable name must start with a letter or an underscore (
_
). - A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and
_
). - Variable names are case-sensitive (
$age
and$AGE
are two different variables).
php<?php
$name = "Alice";
$Name = "Bob";
echo $name; // Outputs: Alice
echo $Name; // Outputs: Bob
?>
Variable Types
PHP is a loosely typed language, meaning you do not need to declare the data type of a variable. PHP automatically converts the variable to the correct data type, depending on its value.
Common Data Types:
-
String:
php<?php $text = "Hello, World!"; ?>
-
Integer:
php<?php $number = 42; ?>
-
Float (Double):
php<?php $price = 19.99; ?>
-
Boolean:
php<?php $is_valid = true; $is_complete = false; ?>
-
Array:
php<?php $colors = array("Red", "Green", "Blue"); ?>
-
Object:
php<?php class Car { public $color; public function __construct($color) { $this->color = $color; } } $myCar = new Car("Red"); ?>
-
NULL:
php<?php $empty = NULL; ?>
Variable Scope
Variables can be declared in different scopes, which determines their accessibility.
-
Global Scope:
- Variables declared outside any function have a global scope and can be accessed outside functions.
php<?php $x = 10; function myTest() { // Will generate an error because $x is not accessible inside the function echo $x; } myTest(); ?>
-
Local Scope:
- Variables declared within a function have a local scope and can only be accessed within that function.
php<?php function myTest() { $y = 5; // Local scope echo $y; } myTest(); // Will generate an error because $y is not accessible outside the function echo $y; ?>
-
Global Keyword:
- Use the
global
keyword to access a global variable inside a function.
php<?php $x = 10; $y = 20; function myTest() { global $x, $y; $y = $x + $y; } myTest(); echo $y; // Outputs 30 ?>
- Use the
-
Static Variables:
- Static variables retain their value between function calls.
php<?php function myTest() { static $count = 0; echo $count; $count++; } myTest(); // Outputs 0 myTest(); // Outputs 1 myTest(); // Outputs 2 ?>
Superglobals
PHP provides several built-in superglobal arrays that are always accessible, regardless of scope. These include:
$_GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
Example using $_POST
:
php<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
echo $name;
}
?>
By understanding how to use variables effectively in PHP, you can store and manipulate data in your applications efficiently.