PHP - Review of PHP fundamentals

Variables

In advanced PHP programming, variables play a crucial role in storing and manipulating data. PHP variables are used to hold values such as numbers, strings, arrays, objects, and more. Here's a brief overview of variables in advanced PHP:

Variable Declaration and Naming:

In PHP, variables start with the dollar sign $ followed by the variable name. Variable names must begin with a letter or underscore, followed by letters, numbers, or underscores. Variable names are case-sensitive.

$name = "John";

$age = 30;

Data Types:

PHP variables are loosely typed, meaning you don't need to explicitly declare the data type. PHP automatically determines the data type based on the value assigned to the variable. Common data types include: 

String: "Hello, PHP!", 'example'

Integer: 42, -15

Float: 3.14, -0.5

Boolean: true, false

Array: $colors = array("red", "green", "blue");

Object: Created using classes and instantiated with the new keyword.

Variable Scope:

PHP variables have different scopes that determine where they can be accessed:

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

Global Scope: Variables declared outside of any function are accessible throughout the script.

Static Scope: Used to preserve the value of a local variable across function calls.

$globalVar = "I'm global";

function example() {

    $localVar = "I'm local";

    echo $globalVar; // Accessible because it's global

}

Super Global Variables:

PHP provides several super global variables that are accessible from any scope, including function and method scopes. These are used to collect form data, manage sessions, access server-related information, etc. Examples include:

$_GET: Contains URL parameters.

$_POST: Contains data from HTML forms submitted using the POST method.

$_SESSION: Holds session variables.

$_COOKIE: Holds cookies sent by the client.

$_SERVER: Contains information about the server and request.

Variable Interpolation:

PHP allows you to directly include variables in double-quoted strings, which is called variable interpolation. 

$name = "Alice";

echo "Hello, $name!"; // Outputs: Hello, Alice!

Variable Variables:

PHP supports the concept of variable variables, which means you can create and use variables whose names are determined by the value of another variable. 

$variableName = "message";

$$variableName = "Hello, variable variables!";

echo $message; // Outputs: Hello, variable variables!

These are some essential aspects of working with variables in advanced PHP programming. Understanding how to declare, manipulate, and manage variables is fundamental for building dynamic and interactive web applications.