Declaring variables is part of the routine of any programmer. In PHP, the statement occurs in a very simple way, as you can see in the example below:
<?php
$a = 1;
?>
In the example, we declare the variable “a” taking the integer value 1.
Unlike some languages, the declaration of variables in PHP uses the “$” character and there is no need to declare the variable type (integer, decimal, string etc). So, over the processing of information, a variable can take several forms:
<?php
$a = 1;// integer
$a = “Test”; // string
$a = 1,0; // decimal
$a = NULL;// null
?>
By default, every PHP variable is declared in the local scope, so it can only be used by set elements and includes in the block used. If there is a need for one variable to be used globally, in other calls, that one must be declared as follows:
<?php
global $a;
?>
You can still use what we call static variables. This type of variable does not lose its value in every method call in the local scope and it is capable of storing information.
Static variables are widely used in recursive functions, check below for an example:
<?php
function Test()
{
$a = 0;
echo $a;
$a++;
}
?>
In the above function, in each call the variable will be reset and incremented at the end, but in each new call function the value will be reset again and so the logic loses its function. In that case you must use a static variable that will store a new value to each call and so the self-increment will work properly.
<?php
function Test()
{
static $a = 0;
echo $a;
$a++;
}
?>
By understanding the variable declaration of resources it is possible to optimize the application performance and thus make them more effective in their processes.
GLOBAL VARIABLES IN SCRIPTCASE
Within Scriptcase you can register on the administrative panel some global variables that can be used in any application built on the tool, all in a simple and uncomplicated WAY, the main characteristic of our tool.
In addition, you can also access some global variables that are already available in a standard way in our tool, as you can see in the example below:
[var_login] = {login};
Check out more content on our blog!
Learn all about Scriptcase.
You might also like…