WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP Variables


Variable can "containers" store information


Creating (Declaring) PHP Variables

In PHP, a variable starts with the $ sign, followed by the name of the variable:

Example

<?php
$txt = "Hello PHP!";
$a = 2;
$b = 5.5;
?>

After the execution of the statements above, the variable $txt will hold the value Hello PHP!, the variable $a will hold the value 2, and the variable $b will hold the value 5.5.

Note: When you assign a text value to a variable, put quotes around the value.


PHP Variables

A variable can have a short name (like a and b) or a more descriptive name (age, income, petname).

Rules for PHP variables:

  • A variable starts with the (dollar)$ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • PHP variables are Perl-like.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variables used before they are assigned have default values.
  • A variable name cannot start with a number
  • Variable names are case-sensitive ($petname and $PETNAME are two different variables)

Output Variables

The PHP echo statement is often used to output data to the screen.

The following example will show how to output text and a variable:

Example

<?php
$txt = "welookups.com";
echo "I like $txt!";
?>

The following example will produce the same output as the example above:

Example

<?php
$txt = "welookups.com";
echo "I like " . $txt . "!";
?>

The following example will output the sum of two variables:

Example

<?php
$a = 2;
$b = 3;
echo $a + $b;
?>


PHP Variables Scope

In PHP, variables can be declared anywhere in the script code.

PHP Variables Scope can be defined range of availability a variable has to the program in which it is declared.

PHP has three different variable scopes:

  • local
  • global
  • static

Global and Local Scope

A variable declared outside a function has a GLOBAL SCOPE and it can be accessed in any part of the program:

Example

<?php
$x = 12; // global scope

function myTest() {
    // using x inside this function will generate an error
    echo "<p>Variable x inside function is: $x</p>";
}
myTest();

echo "<p>Variable x outside function is: $x</p>";
?>

output 12

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:

Example

<?php
function myTest() {
    $x = 5; // local scope
    echo "<p>Variable x inside function is: $x</p>";
}
myTest();

// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>

PHP The global Keyword

The global keyword is used to accessed in any part of the program

To do this, use the global keyword before the variables (inside the function):

Example

<?php
$x = 6;
$y = 2;

function myTest() {
    global $x, $y;
    $y = $x + $y;
}

myTest();
echo $y; // outputs 8
?>

PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. GLOBAL in front of the variable that should be recognized as global.

The example above can be rewritten like this:

Example

<?php
$x = 2;
$y = 3;

function myTest() {
    $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}

myTest();
echo $y; // outputs 5
?>

Example

<?php
function myTest() {
    static $x = 0;
    echo $x;
    $x++;
}

myTest();
myTest();
myTest();
?>

A variable declared in a function is considered local; that is, it can be referenced solely in that function..