TUTSCODE PHP 003: DECLARE AND USE VARIABLES IN PHP


Posted on 2015-07-08 07:53:56


Variables in PHP are similar to variables in other languages, they serve to temporarily store information. For example:
1
2
3
4
5
6
<? Php
$x=3;
$ Y = 4;
$ Z = 2 * $ x + 3 * $ y ;
echo $z;
?>
Variables can store not only the value, but it also stores the expressions example: $ z = 2 * x + 3 * $ $ y

1. Some naming variables in PHP

  • Variables must begin with a "$" sign followed by the name of the variable it is.
  • Variables must start with one character or an underscore "_".
  • Variable names must not start by 1 digits.
  • Variable names can only contain letters from A-> Z, a-> z, numbers from 0-> 9 and underscores.
  • Variable names are case sensitive.

2. Declare variables in PHP

In PHP, it is not necessary to declare variables before use, we usually declare it and assign it directly to a default value. For example:
1
2
3
4
5
<? Php
$txt="Hello world!";
$x=5;
$ Y = 10.5;
?>
Note: when assigning a string to one variable we need to use two marks. " Through this example, we see that when declaring variables in PHP do not need to do one type of value for its report because it will know themselves convert into appropriate data type. In some languages ​​such as C, C ++, Java is required to report the names and data types for variables before use.

3. The scope of use of variables in PHP

In PHP variables declared anywhere in the file, however, it is not used elsewhere. PHP can give us three scopes for the variables:
  • local
  • global
  • static

* Local và Global

- Turn off the function declaration with the Global scope and can only be used outside of the function. - The variable declared in a function with the Local scope and can only be used within the function. The following example shows the scope of use of global variables and local variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<? Php
$x=10; // global scope
function blankaTest()
{
$y=22; // local scope
echo "<p>Check variable inside function:<p>";
echo "Result of x is: $x";
echo "<br>";
Echo "Result and $ and" ;
}
blankaTest ();
echo "<p>Check variable outside function:<p>";
echo "Result x is: $x";
echo "<br>";
Echo "Result and $ and" ;
?>
In the above example we have two variables $ x, $ y and a blankaTest function (): + $ x is a global variable as it is declared in the outer function. + $ y is a local variable as it is declared inside blankaTest function (). In blankaTest function () we print the value of the variable $ x and $ 2 y but only $ y is printed out as it is declared within the function are variable $ x is not printable because it is a global variable and can not be used inside the function. When the external variable function test also prints out the value of the two variables $ x and $ y but only the $ x is printed out that variable $ y is not . The reason is that $ y is a local variable declared in blankaTest function () and when outside the function, it no longer had any effect anymore and so we can not use. Note: You can declare more local variables of the same name in the different functions because local variables are recognized only within the function it is declared only.

* The keyword "global" in PHP

Keywords global used to access the global variables from within the function. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
<? Php
$x=5;
$ Y = 10;
function blankaTest()
{
global $x,$y;
$ Y = $ x + $ y ;
}
blankaTest ();
echo $y; // outputs 15
?>
PHP stores all global variables in the array $ GLOBALS [ index ] with "index" is the name of the variable. This array enables inside functions can access and update the value of a global variable. For example:
1
2
3
4
5
6
7
8
9
10
11
12
<? Php
$x=5;
$ Y = 10;
function blankaTest()
{
$ GLOBALS [ 'and' ] = $ GLOBALS [ 'x' ] + $ GLOBALS [ 'y' ];
}
blankaTest ();
echo $y; // outputs 15
?>

* Tag "static" in PHP

Normally, when a function is done, all its variables will be erased. But occasionally there are cases where we do not want to remove it and to use it for other purposes. To do that we use the word "static" when the variable is declared for the first time. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? Php
function blankaTest()
{
static $x=0;
echo $x;
$x++;
}
blankaTest ();
blankaTest ();
blankaTest ();
?>
Every time mytest function called the variable's value remains as the last time the function is called. * Conclusion: through this lesson we have a better overview of how to declare and use variables in PHP, the scope of use and how to use them to do so effectively. If you have any questions regarding this section may put questions to us via http://tutscode.com/question-answer . Source Reference: w3school.com