PHP Base Note function global variable

Source: Internet
Author: User
Tags config constant php file

Cases

The code is as follows Copy Code

<?php

$a = 12;


function fn ()
{
Global $a;//using outer $a variables, $a as local variables without using this method
$a +=12;
}

echo ' $a = '. $a//output result

?>

Results of output: $a =24


Summary: Global variables defined in the function body can be used outside the function body, and global variables defined outside the function body cannot be used in the function body.

The code is as follows Copy Code

$glpbal $a;
$a = 123;

function f ()
{
echo $a; Error
}

Take another look at the following example

function f ()
{
Global $a;
$a = 123;
}

f ();
echo $a; Correct, you can use

The above example is just the basic global variables knowledge, and let's look at the complex points below:

The code is as follows Copy Code

a.php file

<?php
function Test_global ()
{
Include ' b.php ';
Test ();
}

$a = 0;
Test_global ();
echo $a;
?>

b.php file

<?php
function Test ()
{
Global $a//declaration the $a variable used within sum is global global variable
$a = 1;
}
?>

Why the output is 0?!!

In a user-defined function, a local function range is introduced. Any variable that is used inside a function is limited by default to the scope of the local function (including variables within the file included in the Include and require import)!
Explanation: The internal test_global of the a.php file is a well-defined third-party function that uses include to import $a global variables within the b.php file, so $a is limited to test_global local functions, so the $ in b.php file The scope of a is within the test_global, not the whole a.php ....

Solution:

1. Flush out local function

The code is as follows Copy Code

a.php file

<?php
function Test_global ()
{
Test ();
}
Include ' b.php '; To remove an include from a local Test_global function
$a = 0;
Test_global ();
echo $a;
?>

b.php file

<?php
function Test ()
{
Global $a;
$a = 1;
}
?>

Global Problem Resolution:

Question: I defined some variables ($a) in config.inc.php, in other files, outside of the function include ("config.inc.php"), the function needs to use these variables inside $a, if not declared, echo $ A is not a print out of anything. So declare global $a, but there are a lot of functions and many variables, you can't keep repeating that statement? If there is any good solution, please advise.
Answer1: First define constants in config.inc.php: Define (constant name, constant value)
and then require ' config.inc.php ' in other places that need to be used,
Then you can use this constant directly in this file.
Answer2: I also have the option of defining an array, such as $x[a], $x, so as to declare the global $x one.
Answer3: I tried this method of yours, I can't.
Answer4: Change your php.ini file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.