This question already has answers here:
Accessing a variable defined in a parent function
(5 answers)
Closed 9 years ago.
Example
function a(){
$num = 1;
function b(){
echo $num; // how to get $num value?
}
}
In this case global not working, because $num isn't global variable.
function a() {
$num = 1;
function b($num) {
echo $num;
};
b($num);
}
a();
You could use the S_SESSION to get the variable?
function a(){
$_SESSION['num'] = 1;
function b(){
echo $_SESSION['num'];
}
}
Not sure nested function is the way to go btw.
Related
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
Hi I am new to php and I just run into a problem. I would like to get local variable data outside the function after it was called.
function MyFunction() {
$x = set
}
if($x == "set"){
code...
}
You can't do that. Instead return the value from the function.
function MyFunction() {
$x = "set";
return $x
}
if(MyFunction() == "set"){
code...
}
This question already has answers here:
Sharing var from one function to the other function in PHP Class
(2 answers)
Closed 4 years ago.
Let's say I have two different functions, and one of them has a defined variable. In the second function, I don't wanna write the same variable again, can I simply use the variable from the first function in the second one WITHOUT redefining it in the second function?
Someting like:
class example{
public function a($foo){
$foo2 = $foo + 1
return $foo2;
}
public function b($foo2){
echo "result: " . $foo2;
}
}
It's simple, you can use a property $foo2 and access it from both methods:
class example{
private $foo2;
public function a($foo){
$this->foo2 = $foo + 1
return $this->foo2;
}
public function b(){
echo "result: " . $this->foo2;
}
}
$obj = new example();
$obj->a(5);
$obj->b(); // result: 6
This question already has answers here:
How to get name of calling function/method in PHP? [duplicate]
(10 answers)
Closed 7 years ago.
Is there any way (dirty is fine, this is only for debugging) to get the __FUNCTION__ (function name) from where a function was called from?
example;
function a() {
//code
return b();
}
function b() {
return __PARENT_FUNCTION__
}
echo a();
This should yield a as the function name that was originally called for this output
Take a look at debug_backtrace
function a() {
//code
return b();
}
function b() {
return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]["function"];
}
echo a();
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 8 years ago.
I have the following (summarised) code
define pauls_code($a, $b)
{
$c = $a + $b;
echo $a;
echo $b;
}
pauls_code(1,2);
echo $c; // how do I get this to print outside of the function? I have tried everything. I am hoping to see 123
make your function like this and return the $c
function pauls_code($a, $b){
$c = $a + $b;
return $c;
}
echo pauls_code(1,2);
To declare a function in php you simply put function pauls_code($param1, $param2) not define
You can not access $c outside of the function, read more about scope: http://php.net/manual/en/language.variables.scope.php
This question already has answers here:
How do I get the function name inside a function in PHP?
(4 answers)
Closed 8 years ago.
Is there a function that can return the name of the current function a program is executing?
Yes, you can get the function's name with the magic constant __FUNCTION__
class foo
{
function print_func()
{
echo __FUNCTION__;
}
function print_method()
{
echo __METHOD__;
}
}
$obj = new foo();
$obj->print_func(); // Returns: print_func
$obj->print_method(); // Returns: foo::print_method
Maybe via debug_backtrace http://www.php.net/manual/en/function.debug-backtrace.php