I searched for PHP variables

Premise (what you are learning)

――I learned Java for a few months --I learned a little about CSS and JavaScript --Python is just a touch --PHP has never been touched

What I looked up

The following content is quoted from ** PHP Manual **.

--First PHP files start with ** ** --In principle, 1 scope per variable

<?php
$a = 1;
include'b.inc'; / * $ a can also be used for files read by include or require. * /
?>

The variable $ a is also available in the included b.inc script

--There is a scope range such as global and local

<?php
$ a = 1; / * Global scope * /

function test()
{ 
echo $ a; / * Reference to local scope variable * /
} 

test();
?>

Nothing is displayed when test () is executed with ↑. (Because it has a different scope.)


-If you use a global scope variable in a method, use the global keyword

<?php
$a = 1;
$b = 2;

function Sum() 
{
   global $a, $b;
   $b = $a + $b;
} 

Sum();
echo $b;  /*$b=3*/
?>

↑ My understanding: The secret word for using the defined global scope?

・ $ GLOBALS can be used instead of the global keyword

<?php
$a = 1;
$b = 2;

function Sum() 
{
    $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
} 

Sum();
echo $b;
?>

It seems that $ GLOBALS is Super Global. what is that. .. ..

$GLOBALS (PHP 4, PHP 5, PHP 7)

$ GLOBALS — A reference to all variables available in global scope

Description An associative array containing references to all variables currently defined in the script's global scope. The variable name is the key to the array.

<?php
function test() {
   $foo = "local variable";

   echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
   echo '$foo in current scope: ' . $foo . "\n";
}

$foo = "Example content";
test();
?>

Output result
$foo in global scope: Example content
$foo in current scope: local variable

In other words, if you assign it to the global variable $ foo, it will be applied to all$ GLOBALS [" foo"]regardless of whether it is in a method or not. Is the value assigned? (In Java, setter is required if private is attached, so I'm afraid that it can be easily assigned, ...)

・ Super Global does not need a global declaration, otherwise a global declaration is required

<?php
function test_global()
{
// Most predefined variables are not "super", but in a function
// You must call'global'to enable it in local scope.
   global $HTTP_POST_VARS;
   
   echo $HTTP_POST_VARS['name'];
   
// Super Global is valid in any scope
// No need to call'global'.
// Super Global is available in PHP 4.1.0 and above.
// HTTP_POST_VARS is now deprecated.
   echo $_POST['name'];
}
?>

-Static variable (static)

Static variables exist only in the local function scope, but they do not lose their value when program execution goes outside this scope.

<?php
function test()
{
   static $a = 0;
   echo $a;
   $a++;
}
?>

↑ Without static, it is initialized with $ a = 0 every time test () is called. With static, if test () is called, $ a ++ will increase the value of $ a.

・ Recursive function

<?php
function test()
{
   static $count = 0;

   $count++;
   echo $count;
   if ($count < 10) {
       test();
   }
   $count--;
} / * $ count increases to 1,2,3, ..., 9,10 * /
?>

↑ If there is no static here, it will be initialized with $ count = 0 every time.

By the way, be careful about how to declare static variables! ** **

<?php
function foo(){
static $ int = 0; // correct
static $ int = 1 + 2; // Correct (but only for PHP 5.6 and above)
static $ int = sqrt (121); // Incorrect (using function)

   $int++;
   echo $int;
}
?>

Summary

I searched for variables, but I understood it unexpectedly. However, it seems confusing if you do not always be aware of the scope range. I don't know if I will use PHP. .. ..

I will continue to write in my own way.

Recommended Posts

I searched for PHP variables
I searched for URL encoding
I searched for CSRF in Ruby's Gem
I compared PHP and Java constructors
I sincerely hope for Stream # reject