Changing a global variable from inside a function PHP
A. Use the global keyword to import from the application scope.
$var = "01-01-10";
function checkdate(){
global $var;
if("Condition"){
$var = "01-01-11";
}
}
checkdate();
B. Use the $GLOBALS array.
$var = "01-01-10";
function checkdate(){
if("Condition"){
$GLOBALS['var'] = "01-01-11";
}
}
checkdate();
C. Pass the variable by reference.
$var = "01-01-10";
function checkdate(&$funcVar){
if("Condition"){
$funcVar = "01-01-11";
}
}
checkdate($var);
changing a global variable value in php
The global $y
and the $y
resulting from $y = $x + $y
are the same variable. global
doesn't define a different variable, it defines the 'scope' of the variable, ie, where it can be accessed within the script. So $y = $x + $y
changes the value of the global variable.
If, for example, you rewrote the function like this:
function myTest() {
$x, $y;
}
$x
and $y
would be different from the previously-defined variables because you did not define them as global.
How to change global variable dynamically inside a function with foreach loop
Like Dave said in the comment, you have to use the global
keyword to reference a global variable. But you should also try to make your code scalable for more possible variables (i.e. use an array).
I would suggest to change the function something like this:
<?php
define('INPUT_FIELDS', array('fullname', 'phone', 'email'));
/** create a global var that holds all form data **/
$FORM = array();
/** initialize $FORM based on defined input fields **/
foreach (INPUT_FIELDS as $key)
$FORM[$key] = false;
function isPostValid(){
/** access global variable $FORM **/
global $FORM;
/** verify POST request **/
if ($_SERVER['REQUEST_METHOD'] !== 'POST')
return false;
/** check all FORM keys for POST values **/
foreach($FORM as $key => $init_value) {
/** How does checkInputsAndValidate() work in your code?
* -> change here if necessary
**/
$new_value = isset($_POST[$key]) ? checkInputsAndValidate($key) : false;
if (!$new_value)
return false;
$FORM[$key] = $new_value;
}
/** if all is OK, return true **/
return true;
}
?>
Changing global and local variables
brother, local variable define in the method and its authority is only in that method. But global variable you can use everywhere on that page or class.
in your case, your answer is
outside
inside
inside
because when you call first $x because of its globally define it prints the outside. when you call that function then it displays the function value inside and after that is store the value in $x and again when you print $x it display the inside.
thanks in advance hope you understand.
php use function to change a value of a variable outside function
Not sure if this is a contrived example or not, but in this case (as in most cases) it would be extremely bad form to use global
. Why not just return the results and assign the return value?
$test = 1;
function increment($val) {
return $val + 1;
}
$test = increment($test);
echo $test;
This way, if you ever need to increment any other variable besides $test
, you're done already.
If you need to change multiple values and have them returned, you can return an array and use PHP's list
to easily extract the contents:
function incrementMany($val1, $val2) {
return array( $val1 + 1, $val2 + 1);
}
$test1 = 1;
$test2 = 2;
list($test1, $test2) = incrementMany($test1, $test2);
echo $test1 . ', ' . $test2;
You can use func_get_args
to also accept a dynamic number of arguments and return a dynamic number of results as well.
can't change global variable inside function using parameter in python
Before using the global statement, you need to define a global variable...
variable = None
def bla():
global variable
variable = 22
How should you edit a global variable within a wordpress page template?
Global variables are global but NOT persistent, i.e. they do not retain their values from one invocation of WordPress to the next invocation. In your case I think you need persistence per user so HTTP cookies would work as these are persistent over a browser session. If you want a PHP solution you need to store something in the MySQL database as only data in the database will persist from one invocation of WordPress to the next. Actually, storing something in the file system would also be persistent but in WordPress persistent data is conventionally store in the database.
Related Topics
Removing Black Borders 4:3 on Youtube Thumbnails
How to Connect PHP with Microsoft Access Database
Use a Variable Within Heredoc in PHP
In PHP, How to Get the First and Last Date of a Month
Secure User Image Upload Capabilities in PHP
Utf-8 Special Characters Not Displaying
Allow User Submitted HTML in PHP
PHP Artisan Migrate Throwing [Pdo Exception] Could Not Find Driver - Using Laravel
Add Class="Active" to Active Page Using PHP
How Validate Unique Email Out of the User That Is Updating It in Laravel
Using Array_Intersect on a Multi-Dimensional Array
PHP Get All Arguments as Array
How to Give Container as Argument to Services
Add ".Active" Class to the Current Page's Link in a Menu Using Jquery or PHP