0

Say I have...

function one($x){
     return $a + $x;
}

function two(){
     $a = 5;
     echo one(3);
}

Will this show the answer "8" or "3"? In other words, will function one get the value of $a or do I need to declare it global somewhere?

N.B. I haven't tried it yet, but I'm asking it here to understand WHY it acts one way or the other.

8
  • 1
    Instead of using global you should pass it as a parameter, if you get to the point where your function needs lots of parameters then you should build a class, then all property's are available to that method. Dont make your code ugly!!! Commented May 9, 2012 at 3:53
  • @LawrenceCherone Can you give an example of using a class with my example (because I effectively have about 20 variables to pass)? Commented May 9, 2012 at 3:57
  • That's a bit broad, as it goes back to basic questions about software design and can hardly be answered with one example. Start with similar questions like: stackoverflow.com/questions/5861163/… Commented May 9, 2012 at 4:01
  • @deceze Considering all this, a simple if/else with the functions would do the job instead of messing around with variables. However, would a syntax of classes/functions be optimal in terms of performance over if/else structure ? Commented May 9, 2012 at 4:28
  • Performance is entirely secondary. Grouping code into functions, classes and files first and foremost serves to make it maintainable and logical. That's why you're writing PHP instead of Assembler. Your program is not going to see a significant drop in performance from an extra function call or two. Commented May 9, 2012 at 4:34

4 Answers 4

2

No function one does not know about $a. But this can be done.

$a = 5;

function one($x){
 global $a;
 return $a + $x;
}

function two(){
 global $a;
 $a = 5;
 echo one(3);
}

Now two() would echo 8

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

9 Comments

Using global scope is of course not what you usually want to do. Limited function scope is a feature that works to your advantage, not something that needs to be "worked around" by using global.
@Broncha My variable $a needs to be declared in function two..how would I do it?
@Adam Pass it as a parameter into one(). If the function one needs that value to do its job, then it should be an explicit parameter.
@AdamStrudwick yes you define your variable in function two and call functino one with both the parameters
@Adam Think of a function as a standalone piece of code. If you copy-pasted it into a file of its own, it should work just like that. If you create a dependency of the function to some global variables, you're very quickly going to be writing unmaintainable spaghetti code.
|
1

Functions do not inherent scope from the function that calls them. (Nor do they inherit global variables by default - that's what the global keyword is for.)

Thus, $a will be completely undefined inside of one() and you'll get a notice about it.

For more details, see the Variable Scope page in the PHP manual.

Comments

1

You won't get 8 or 3. You'll get a Notice since $a has not been defined in the scope of the function one, and you attempt to read it:

PHP Notice:  Undefined variable: a in - on line 3
PHP Stack trace:
PHP   1. {main}() -:0
PHP   2. two() -:11
PHP   3. one() -:8

Comments

1

If you to use a class as close as to your example, Notice no global usage, just assign your variables $this->* then there global scope within the class and its methods/functions you can also access them from outside of the class like $functions->a:

<?php 
Class functions{

    function one($x){
        return $this->a + $x;
    }

    function two(){
        $this->a = 5;
        echo $this->one(3);
    }

}

$functions  = new functions();

$functions->two(); //8

echo $functions->a;//5
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.