techmore.in

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:

  1. String:

    php
    <?php $text = "Hello, World!"; ?>
  2. Integer:

    php
    <?php $number = 42; ?>
  3. Float (Double):

    php
    <?php $price = 19.99; ?>
  4. Boolean:

    php
    <?php $is_valid = true; $is_complete = false; ?>
  5. Array:

    php
    <?php $colors = array("Red", "Green", "Blue"); ?>
  6. Object:

    php
    <?php class Car { public $color; public function __construct($color) { $this->color = $color; } } $myCar = new Car("Red"); ?>
  7. NULL:

    php
    <?php $empty = NULL; ?>

Variable Scope

Variables can be declared in different scopes, which determines their accessibility.

  1. 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(); ?>
  2. 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; ?>
  3. 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 ?>
  4. 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.