2

EDIT: so sorry, the original code was an iteration while i was trying to figure the problem out, I've edited the code This question started similar, but here's the real problem I'm having:

In this code:

<?php
    $a = 2:

    function incr(){
        $a++;
    echo $a;
    }

    echo $a;
    incr();
?>

the output is 2 and then 1, and that signals to me that the variable $a gets initialized to 0 inside the function. Does the server know just by the virtue that i used the increment syntax to initialize the variable to 0?

Community
  • 1
  • 1
Denis Kralj
  • 633
  • 2
  • 12
  • 27
  • your problem is that `$a` is not in scope inside of your function. inside of the function, `$a` is not defined – Jeff Lambert Jun 10 '14 at 19:27
  • 2
    http://www.php.net//manual/en/language.variables.scope.php – Weltschmerz Jun 10 '14 at 19:27
  • possible duplicate of [Giving my function access to outside variable](http://stackoverflow.com/questions/2531221/giving-my-function-access-to-outside-variable) – Jeff Lambert Jun 10 '14 at 19:29
  • 1
    It's interesting you got 1 out of the echo incr(); call. Within incr, $a is undefined. So, $a++ is undefined and you shouldn't get any output. Is this a simplification of a problem, or are you actually seeing that output? Stick it into http://writecodeonline.com/php/ and only 2 is output. – Barbara Laird Jun 10 '14 at 19:30
  • 1
    @Barbara undefined variables are only a notice, and doing (basically) `null + 1` will coerce the null value to an integer type, apparently the value `0` prior to attempting the addition. See the manual on [type juggling](http://www.php.net//manual/en/language.types.type-juggling.php). You can configure php so that notices are treated like errors, but by default they are not. – Jeff Lambert Jun 10 '14 at 19:34
  • @BarbaraLaird thanks for the heads up, that made me realize that i pasted the wrong code block in. Fixed – Denis Kralj Jun 10 '14 at 19:44

4 Answers4

7

$a is undefined, then when you increment it, it goes to 1.

function incr(){
    $a++; // $a is undefined
    return $a;
}

As pointed out in the manual:

Note:

The increment/decrement operators only affect numbers and strings. Arrays, objects and resources are not affected. Decrementing NULL values has no effect too, but incrementing them results in 1.

You could use global, and it will return as expected.

function incr(){
    global $a;
    $a++;
    return $a;
}
Community
  • 1
  • 1
hlscalon
  • 7,304
  • 4
  • 33
  • 40
3

If you want it to be properly incremented within the function, you need to change your code to this:

<?php
    $a = 2:

    function incr($a){
        return $a++;
    }

    echo $a;
    echo incr($a);
?>

The $a inside and the $a outside the function are two entirely different variables in your code snippet, they are not in the same scope.

You can read about variable scope here : http://www.php.net//manual/en/language.variables.scope.php

You can also use global variables, also mentioned in the previous link, however their use is usually not recommended.

Dany Caissy
  • 3,176
  • 15
  • 21
3

use this:

<?php
    $a = 2:

    function incr($a){
        return $a++;
    }

    echo $a;
    echo incr($a);
?>
Locke
  • 661
  • 1
  • 6
  • 17
1

In php global variables have to be declared global inside the function it is using them:

<?php
    $a = 2:

    function incr(){
        global $a;
        $a++;
    }

    echo $a;
    incr();
    echo $a;
?>

Don't mean to be rude, but this is explained in the manual: http://www.php.net/manual/en/language.variables.scope.php

Or pass them as variables as the other answers by suggest. Note that PHP also allows to pass variables by reference (at least in PHP 5).

questing
  • 162
  • 1
  • 10