Latest web development tutorials

PHP variables

Variable is used to store information in a "container":

Examples

<?php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>

Running instance »

Algebraic similar

x = 5
y = 6
z = x + y

In algebra, we use the letters (such as x), and give it a value (such as 5).

From the above expression z = x + y, we can calculate the value of z 11.

In PHP, these letters are calledvariables.

lamp Variable is a container for storing data.


PHP variables

Similar to algebra, can be assigned to a PHP variable value (x = 5) or expressions (z = x + y).

Variable can be a short name (such as x and y), or a more descriptive name (such as age, carname, totalvolume).

PHP variable rules:

  • Variables begin with $ symbol, followed by the variable name
  • Variable names must begin with a letter or an underscore character
  • Variable names can only contain alphanumeric characters and the underscore (Az, 0-9, and _)
  • Variable names can not contain spaces
  • Variable names are case-sensitive ($ y and $ Y are two different variables)
lamp PHP PHP statements and variables are case-sensitive.

Create (statement) PHP variables

PHP does not command the variable declaration.

Variable is created the first time you assign to it when:

Examples

<? Php
$ Txt = "Hello world!";
$ X = 5;
$ Y = 10.5;
?>

Running instance »

In the above statement is executed, the variabletxt will hold the value Hello world!,And the variable x will hold the value 5.

Note: When you assign a text value to a variable, the quotation marks around the text value.


PHP is a weakly typed language

In the example above, we note, you do not have to declare the data type of the variable to PHP.

PHP will be based on the value of the variable, the variable is automatically converted to the correct data type.

In a strongly typed programming language, we must first declare the type and name (defined) variables before using variables.


PHP variable scope

Scope of a variable is the script variables can be referenced / used parts.

There are four different PHP variable scope:

  • local
  • global
  • static
  • parameter

Local and global scope

In all the variables defined outside the function has global scope. In addition to the functions, global variables can be accessed by any part of the script, to access a global variable in a function, you need to use the global keyword.

PHP variable declared inside a function are local variables can only be accessed inside the function:

Examples

<? php
$ x = 5; // global variables

function myTest ()
{
$ y = 10; // local variable
echo "<p> Test function within the variable: <p>";
echo "variable x is: $ x";
echo "<br>";
echo "variable y is: $ y";
}

myTest ();

echo "<p> Test function external variables: <p>";
echo "variable x is: $ x";
echo "<br>";
echo "variable y is: $ y";
?>

Running instance »

In the above example myTest () function defines variables $ x and $ y. $ X variable outside a function declaration, so it is a global variable, $ y variable declared inside a function it is a local variable.

When we call myTest () function and output values ​​of two variables, the function will output the value of $ y is a local variable, but not the output value of $ x, because the $ x variable defined outside a function can not be used within a function, if to access a global variable in a function, you need to use the global keyword.

Then we myTest () function is outside the output values ​​of two variables, the function will output full value of the local variable $ x, but the output value of $ y can not, because the $ y variable defined within a function, is a local variable.

Note You can use the same variable name in different functions, because these functions within the defined variable names are local variables, act only within the function.

PHP global keyword

global keyword is used to access the global variables within a function.

Call a global variable defined outside the function within a function, we need prior to the function variables plus global Keywords:

Examples

<? Php
$ X = 5;
$ Y = 10;

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

myTest ();
echo $ y; // output 15
?>

Running instance »

PHP will all global variables are stored in an array called$GLOBALS [index] in. Save name index variable. This array can be accessed inside the function can also be used to update global variables directly.

Examples of the above can be written like this:

Examples

<?php
$x=5;
$y=10;

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

myTest();
echo $y;
?>

Running instance »


Static scope

When a function is completed, it is usually all variables will be deleted. However, sometimes you want a local variable is not deleted.

To do this, use thestatic keyword when you first declare a variable:

Examples

<?php

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

myTest();
myTest();
myTest();

?>

Running instance »

Then, each time the function is called, the variable will retain a value before the function is called.

Note: This variable is still local variables of the function.


Parameters Scope

Parameters are passed by value to the calling code will function's local variables.

Parameters are declared in the parameter list, as part of a function declaration:

Examples

<?php

function myTest($x)
{
echo $x;
}

myTest(5);

?>

We'll PHP function section made it a more detailed discussion.