Reputation: 125
I know the following PHP code is incorrect, but I would like to know how this idea can be accomplished.
function A(){
$a = 'Hello';
$b = 'World';
$c = $a.' '.$b;
echo 'blah blah blah times 1,000';
function B($c){
return $c;
}
}
echo function B();
Upvotes: 0
Views: 108
Reputation: 37065
There are a few approaches. Here's the easiest:
function B($c){
return $c;
}
function A(){
$a = 'Hello';
$b = 'World';
$c = $a.' '.$b;
echo 'blah blah blah times 1,000';
B($c);
}
}
echo B();
But given that B
does nothing, you could just go with:
function A(){
$a = 'Hello';
$b = 'World';
$c = $a.' '.$b;
echo 'blah blah blah times 1,000';
return $c;
}
echo A();
I have a suspicion that you are wanting to swap values between functions to get some grander output. For that, you should investigate object-oriented programming, which PHP is catching up as best it can.
Class ChatterBox {
public function sayHello() {
$a = 'Hello';
$b = 'World';
$c = $a.' '.$b;
return $c
}
public function rambleOn() {
echo 'blah blah blah times 1,000';
}
}
$small_talk = new ChatterBox();
echo $small_talk -> sayHello(); // Hello World!
$small_talk -> rambleOn() // 'blah blah blah times 1,000';
The benefit you get is not only having one variable with multiple functions (methods), but setting a value in one method that is global to the Class can be accessed by the other methods, so suppose you wanted to always include your name, you could go a bit further:
Class ChatterBox {
function __construct($name) {
$this -> name = (!empty($name)) ? $name : 'Mysterion';
}
public function set_intro() {
$this -> intro = "My name is " . $this ->name . ".";
}
public function sayHello() {
$a = 'Hello';
$b = 'World';
$c = $a.' '.$b;
return $c . $this -> set_intro();
}
public function rambleOn() {
echo $this -> intro . 'blah blah blah times 1,000';
}
}
$small_talk = new ChatterBox("Rick");
echo $small_talk -> sayHello(); // Hello World! My name is Rick.
$small_talk -> rambleOn() // 'My name is Rick. blah blah blah times 1,000';
Of course, OOP gets way more sophisticated (and like I said before, PHP is still catching up), but as a starter, I think it's neat to have all of your functions tied together and able to share variables, etc. Perhaps lambda functions are the way things are moving, but as I'm still getting the hang of OOP, I think it's best to dabble in both rather than pick which one is "best". Especially since in your example, the lambda (anonymous) function doesn't really make sense (to me) why the one function sits inside the other. In the OOP, it makes sense in that you may have different options/choices/etc that you want to call from based on context, while not wanting to lose track of data in the object or have to pass the values back in forth between functions when they are going to be used across the object.
Upvotes: 3
Reputation: 224963
If you have PHP 5.3 or higher, it's easy with anonymous functions:
function A() {
global $B;
$a = 'Hello';
$b = 'World';
$c = $a.' '.$b;
echo 'blah blah blah times 1,000';
$B = function() use ($c) {
return $c;
};
}
A(); // blah blah blah times 1,000
echo $B(); // Hello World
If not, you can accomplish the same thing using create_function
.
Upvotes: 2
Reputation: 2017
The way your script works, B is undefined by default. When you call function A, B becomes defined to the rest of the script. EG:
echo B('blah'); // Returns a 'function doesn't exist' error
A();
echo B('blah'); //Echos 'blah', as B is now defined.
Upvotes: 0