WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP Variables


Variable can "containers" store information


Creating (Declaring) PHP Variables

In PHP, a variable begins with the $ sign, trailed by the name of the variable:

Example

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

After the execution of the announcements over, the variable $txt will hold the esteem Hello PHP!, the variable $a will hold the esteem 2, what's more, the variable $b will hold the esteem 5.5.

Note: When you dole out a content an incentive to a variable, put statements around the value.


PHP Variables

A variable can have a short name (like an and b) or an increasingly expressive name (age, pay, petname).

Rules for PHP variables:

  • A variable begins with the (dollar)$ sign, trailed by the name of the variable
  • A variable name must begin with a letter or the underscore character
  • PHP factors are Perl-like.
  • A variable name can just contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variables utilized before they are doled out have default values.
  • A variable name can't begin with a number
  • Variable names are case-touchy ($petname and $PETNAME are two distinctive variables)

Output Variables

The PHP reverberation articulation is regularly used to yield information to the screen.

The following precedent will tell the best way to yield content and a variable:

Example

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

The following precedent will deliver indistinguishable yield from the model above:

Example

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

The following precedent will yield the aggregate of two variables:

Example

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


PHP Variables Scope

In PHP, factors can be announced anyplace in the content code.

PHP Variables Scope can be characterized scope of accessibility a variable has to the program in which it is declared.

PHP has three diverse variable scopes:

  • local
  • global
  • static

Global and Local Scope

A variable announced outside a capacity has a GLOBAL SCOPE and it tends to be gotten to in any piece of the program:

Example

<?php
$x = 12;/worldwide scope

function myTest() {
   /utilizing x inside this capacity will create an error
    reverberation "<p>Variable x inside capacity is: $x</p>";
}
myTest();

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

output 12

A variable pronounced within a capacity has a LOCAL SCOPE and can as it were be gotten to inside that function:

Example

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

// utilizing x outside the capacity will create an error
echo "<p>Variable x outside capacity is: $x</p>";
?>

PHP The worldwide Keyword

The worldwide catchphrase is utilized to got to in any piece of the program

To do this, utilization the worldwide catchphrase before the factors (inside the function):

Example

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

work myTest() {
    worldwide $x, $y;
    $y = $x + $y;
}

myTest();
reverberation $y;/yields 8
?>

PHP likewise stores every single worldwide variable in an exhibit called $GLOBALS[index]. The index holds the name of the variable. Worldwide before the variable that ought to be perceived as global.

The precedent above can be modified like this:

Example

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

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

myTest();
reverberation $y;/yields 5
?>

Example

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

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

A variable announced in a capacity is viewed as neighborhood; that is, it tends to be referenced exclusively in that function..