Changing a global variable from inside a function PHP - php

I am trying to change a variable that is outside of a function, from within a function. Because if the date that the function is checking is over a certain amount I need it to change the year for the date in the beginning of the code.
$var = "01-01-10";
function checkdate(){
if("Condition"){
$var = "01-01-11";
}
}

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);

Just use the global keyword like so:
$var = "01-01-10";
function checkdate(){
global $var;
if("Condition"){
$var = "01-01-11";
}
}
Any reference to that variable will be to the global one then.

All the answers here are good, but... are you sure you want to do this?
Changing global variables from within functions is generally a bad idea, because it can very easily cause spaghetti code to happen, wherein variables are being changed all over the system, functions are interdependent on each other, etc. It's a real mess.
Please allow me to suggest a few alternatives:
1) Object-oriented programming
2) Having the function return a value, which is assigned by the caller.
e.g. $var = checkdate();
3) Having the value stored in an array that is passed into the function by reference
function checkdate(&$values) {
if (condition) {
$values["date"] = "01-01-11";
}
}
Hope this helps.

Try this pass by reference
$var = "01-01-10";
function checkdate(&$funcVar){
if("Condition"){
$funcVar = "01-01-11";
}
}
checkdate($var);
or Try this same as the above, keeping the function as same.
$var = "01-01-10";
function checkdate($funcVar){
if("Condition"){
$funcVar = "01-01-11";
}
}
checkdate(&$var);

Related

Get a parent variable using a function

I am trying to access $my_var from within a function, I know I can use a global $my_var to do so, but that IMO isn't a good way to do this since if $my_var is outside of the call_user_func it will use that one instead of the one within. I can't use use since the function isn't an anonymous function.
Is there a good way to do this without using a class?
call_user_func(function(){
$my_var = null;
function myFunc($value1, callable $callback){
// Access $my_var
}
});
myFunc('value 1', function(){});
There are several ways. The simplest is to simply pass $var as a parameter, and update it through the function's return value:
$var = null;
function foo( $var ) { $var++; }
$var = foo( $var );
Another way is to declare the parameter as a reference (& $var):
$var = null;
function foo( & $var ) { $var++; }
foo( $var );
A third way is to use an anonymous function and specify the use of the variable:
$var = null;
$foo = function() use( & $var ) { $var++; }
$foo();

Getting function scope variable value after declaring a global variable with the same name

so imagine I have this PHP code
<?php
$var = 10;
function foo($var)
{
global $var;
echo $var;
}
foo(2);
?>
The output here is 10. I want to know if there is a way to refer back to the function scope variable $var (which in my example has a value of 2).
Maybe not an exact answer but here is a way:
$var = 10;
function foo($var)
{
$array = get_defined_vars();
global $var;
echo $var;
echo $array['var'];
}
foo(2);
You could also use func_get_arg() or func_get_args(), but whatever you do would need to be before the global statement.

PHP: Global var not being picked up inside a function

This is blowing my mind...
I've got a standalone PHP file, and a simple function with a global var.
<?php
$var = 4;
function echoVar()
{
echo $var;
}
echoVar();
?>
When I call echoVar() nothing is returned... However if I place the $var inside the function it will return 4.
What's going on here? Shouldn't $var be global in this case?
If a variable is set outside of a function, it's not visible inside that function. To access it, you must declare it global with the global keyword. This is called a scope.
<?php
$var = 4;
function echoVar() {
global $var;
echo $var;
}
echoVar();
Note: This is generally considered bad practice. Read this for more information.
A good alternative would be to pass in the variable as an argument:
<?php
$var = 4;
function echoVar($var) {
echo $var;
}
echoVar($var);
Lots of options here... like
<?php
$var = 4;
function echoVar($var)
{
echo $var;
}
echoVar($var);
?>
or
<?php
$var = 4;
function echoVar()
{
global $var;
echo $var;
}
echoVar();
?>
You can either have $var as an argument, like this:
$var = 4;
function echoVar($var)
{
echo $var;
}
echoVar($var);
or use global, like this:
$var = 4;
function echoVar()
{
global $var;
echo $var;
}
echoVar();
When you call any function it's create local variable so you have to pass argument in calling function part.
$var = 4;
function echoVar($var)
{
echo $var;
}
echoVar($var);
Just going to clarify since everyone seems to be posting mostly rubbish.
Do not use global $var;
Do not echo out inside of a function
Output from a function does not need to be assigned to a variable before being echo'd
This is how it "should" be done.
<?php
$var = 4; //set initial input var this is external to the function
function echoVar($internalvar) { /*notice were accepting $var as $internalvar I'm doing this to clarify the different variables so you don't end up getting confused with scope $internalvar is local to the function only and not accessible externally*/
return $internalvar; //now we pass the function internal var back out to the main code we do this with return you should never echo out your output inside the function
}
echo echoVar($var); //call function and pass $var in as an arguement
?>

Accessing global variables from inside a function in PHP

I want to declare a global variable using PHP and be used inside functions.
I have tried:
$var = "something";
function foo()
{
echo $var;
}
yet I receive an error stating that the $var is undefined.
How can I solve this?
$var = "something";
function foo()
{
global $var;
echo $var;
}
use the term "global" when you need to use variables that were declared outside your function scope.
PHP variables have function scope. I.e., variables inside a function can't be accessed from outside it and global variables can't (by default) be accessed from inside functions. While using the global keyword inside functions to im-/export variables is a solution, you should not do it. Functions should be self-contained; if you need a value inside a function, pass it as a parameter, if the function needs to modify global values, return them from the function.
Example:
function foo($arg)
{
echo $arg;
}
$var = "something";
foo($var);
Please read: http://php.net/manual/en/language.variables.scope.php

Converting all vars to global scope

I know I can use $GLOBALS['var'] inside a function to refer to variables in the global scope.
Is there anyway I can use some kind of a declaration inside the function so I will be able to use $var without having to use $GLOBAL['var'] every time?
Joel
Although it's not recommended, but if you do want to, here's what you can do:
If you only want to GET the values from the vars (and not SET the values), just use extract:
extract($GLOBALS);
This will extract and create all the variables in the current scope.
How about using static class?
such as
class example
{
public static $global;
public static function set($arr)
{
foreach ($arr as $key=>$val)
{
self::$global[$key] = $val;
}
}
}
function example_function()
{
var_dump( example::$global );
}
example::set( array('a'=>1, 'b'=>2) );
example_function();
You can use the global keyword, So you can use type $var instead of $GLOBALS['var'] inside a function.
function myFunc() {
global $var;
$var = 3;
}
$var = 1;
myFunc();
echo $var;
Output: 3

Categories