1

Im trying to change the value of a declared variable which is outside the function in use of a function

<?php
$test = 1;
function addtest() {
    $test = $test + 1;
}
addtest();
echo $test;
?>

but it seems it couldn't. only variables declared as parameters in the function only work. is there a technique for this? thanks in advance

6
  • Can addtest be altered to accept arguments? Commented Sep 12, 2014 at 16:23
  • 2
    If it can be altered to accept global $test;, then it can be altered to accept arguments Commented Sep 12, 2014 at 16:24
  • presume you can add params to the function addtest , then what could be the good way to change the first initialized $test ? Commented Sep 12, 2014 at 16:29
  • By passing it to the function. Commented Sep 12, 2014 at 16:30
  • You need to declare $test global inside your function. But you really don't want to write code that relies on global state, it's not considered a good practice. Commented Sep 12, 2014 at 16:32

4 Answers 4

6

Change the variable inside the function to a global -

function addtest() {
    global $test; 
    $test = $test + 1;
}

There are a lot of caveats to using global variables -

  • your code will be harder to maintain over the long run because globals may have an undesired affect on future calculations where you might not be aware how the variable was manipulated.

  • if you refactor the code and the function goes away it will be detrimental because every instance of $test is tightly coupled to the code.

Here is a slight improvement and doesn't require global -

$test = 1;
function addtest($variable) {
    $newValue = $variable + 1;
    return $newValue;
}

echo $test; // 1
$foo = addtest($test);
echo $foo; // 2

Now you haven't had to use a global and you have manipulated $test to your liking while assigning the new value to another variable.

Sign up to request clarification or add additional context in comments.

3 Comments

is there something that's negative security-wise when using it?
Note also that function add() { $test = $test + 1; } function addTest() {$test = 1; add(); echo $test; } addTest(); would fail.... global really means global
i wouldnt want that to happen, i hope there is a cleaner way to do this
1

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.

2 Comments

now i have a new problem, seems my function has more than 1 parameters, and i need to change more than 1 variable outside of it.
you can always return an array, and use PHP's list
0

Use the global keyword.

<?php
$test = 1;
function addtest() {
    global $test;
    $test = $test + 1;
}
addtest();
echo $test; // 2
?>

Comments

0

Maybe you can try this .

<?php 

function addTest(&$val){
  # Add this & and val will update var who call in from outside 
  $val += 1 ;
}

$test = 1;
addTest($test);
echo $test;

// 2 

$anyVar = 5;
addTest($anyVar);
echo $anyVar;

// 6

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.