php

PHP Variables


Variables are container that stores value.

A variable starts with the $ sign & followed by the variable name.

<?php
	$var = "Welcome to LearnCoding";
	echo $var;
?>

 

Note: - As PHP is a loosely typed language, so no need to declare the data types of the variables. It automatically analyses the values and makes conversions to its correct datatype.

  • A variable must start with a dollar($) sign, followed by the variable by the variable name.
  • It can obly contain alpha-numeric character and underscore (A-z, 0-9, _).
  • A variable name must start with a letter or underscore (_) character and no numbers.
  • A PHP variable name cannot contain spaces.
  • One thing to be kept in mind that the variable name cannot start with a number or special symbols.
  • PHP variables are case-sensitive, i.e. $name, $NAME and $Name every variable is treated as different variables.
  • A constant is used as variable for a simple value that cannot be changed. It is also case-sensitive.
  • Assignment of variables is done using the assignment operator i.e. "eual to (=)". The variable names are on the left of equal and the expression or values are to the right.

 

PHP Varaible: Declaring Datatypes

<?php  
    $str="LearnCoding";  
    $x=299;  
    $y=423.3;
    echo "String is: $str <br/>";  
    echo "Integer is: $x <br/>";  
	echo "Float is: $y <br/>";
?>

As, PHP employs dynamic typing, meaning it automatically assigns a data type to a variable based on its value. Unlike strict typing, where data types are explicitly defined, PHP allows flexbility.

 

Variable Scope:

In PHP, you have the flexibility to declare variables at any point in your script. The scope of a variable in a script determines where in the code that variable can be used or referred to.

In PHP there are three types of Variable scope which are as follows:

  • Local variable
  • Global variable
  • Static variable

 

1. Local Variable:

Local variable can only be used within the function or code block where it's created.

It's not visible outside of that of that specific area, and once the function or block finishes its execution, the variable's value if forgotten. This design promotes cleaner code organization and helps avoid clashes with variable names in other parts of the program.

 

Example:

<?php
    function localTest() {
    $y = 5; //local scope
    echo "<p>Variable value: $y</p>";
    }
    localTest();
?>

Did you Know?

It's possible to use the same name for local variables in different functions because each function recognizes and operates with its own set of local variables.

 

2. Global Variables:

A global variable is available throughout the entire script, including within functions and methods. These variables are defined outside any specific function or class and their values can be modified from any part of the script. However, it's crucial to mindful that excessive use of global variables can result in disorganized code and may lead to conflicts with variable names across different parts of the program.

 

Example:

<?php  
    $lang = "PHP";  //Global Scope  
    function globalTest()  
    {  
    	global $lang;  
        echo "Interested Language: ". $lang; // accessing as global
        echo "</br>";  
    }  
    globalTest();  
	echo "Interested Language: ". $lang; // accessing as local
?>

Did you know?

You can access a global variable within a function using the predefined $GLOBALS array. This array holds all global variables, allowing you to refer to a specific global variable by its name within the function

 

3. Static Keyword

A static variable, like a local variable, is limited to the function or code block where it's defined. However, unlike local variables, the value of a static variable persists across multiple calls to the function. This feature is handy for preserving a state or keeping track of how many times a function has been called.

In simple words, Static variables exist only in a local function, but it does not free its memory after the program execution leaves the scope. Understand it with the help of an example:

 

Example:

<?php  
function staticTest()  
{  
    static $stat = 1; //static variable  
    $nonStat = 5; //Non-static variable  

    $stat++; //increment in non-static variable  
        
    $nonStat++;  //increment in static variable
    echo "Static value: " .$stat ."</br>";  
    echo "Non-static value: " .$nonStat ."</br>";  
}  
    
//first function call  
staticTest();  

//second function call  
staticTest();  
?>

 

Explanation:

You have noticed that $stat regularly increments after each function call, whereas $nonStat does not. This is why because $nonStat is not a static variable, so it freed its memory after the execution of each function call.