Functions and reference variables

Discussion in 'PHP' started by Rahul Bose, Jul 8, 2010.

  1. #1
    From the below code can anyone explain me why the value of the variable "$value" is returned as 56 in both echo's and not as 101 and 1001??


     
    Rahul Bose, Jul 8, 2010 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Because the first time you call your function you use '&increment()' and the second time you use 'increment()'. I'm not sure it's technically legal to have a function name that starts with an '&'. You might consider changing the function name to make things simpler.
     
    rainborick, Jul 8, 2010 IP
  3. Artifactal Connection

    Artifactal Connection Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What does the & symbol in front of your function name stands for ?
     
    Artifactal Connection, Jul 8, 2010 IP
  4. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #4
    You are incorrectly trying to call the function by reference which will not work..... you might want to check here for details on how - and when - you might want to do that.

    http://www.php.net/manual/en/language.references.pass.php

    Also, you are not storing the returned value before echoing it.....

    $value=increment(100);
    print "$value<br />";
    $value=increment(1000);
    print "$value<br />";
    PHP:
    output: 101<br />1001<br />
     
    lukeg32, Jul 9, 2010 IP