access to variables in nested functions in php - php

suppose i have two nested function like this :
$a = 1;
$b = 2;
function test(){
$b = 20;
function Sum()
{
$b = $GLOBALS['a'] + $b;
}
}
test();
Sum();
echo $b;
now i want in function Sum() access to $b variable declared in function test();
How do you do?

Wild-Guessing-mode:
Your function Sum() would "normaly" take two parameters/operands like
function Sum($a, $b) {
return $a+$b;
}
echo Sum(1, 20);
Now you have the function Test() and you want it to return a function fn that takes only one parameter and then calls Sum($a, $b) with one "pre-defined" parameter and the one passed to fn.
That's called either currying or partial application (depending on what exactly you implement) and you can do something like that with lambda functions/closures since php 5.3
<?php
function Sum($a, $b) {
return $a + $b;
}
function foo($a) {
return function($b) use ($a) {
return Sum($a, $b);
};
}
$fn = foo(1) // -> Sum(1, $b);
$fn = foo(2) // -> Sum(2, $b);
echo $fn(47);

Why not use this?
$a = 1;
$b = 2;
function test(){
$b = Sum(20);
}
function Sum($value)
{
$value = $GLOBALS['a'] + $value;
return $value;
}
test();
// Sum(); // Why do you need this here??
echo $b;
Edit: Better without globals
$a = 1;
$b = 2;
function Sum($value1, $value2)
{
return $value1 + $value2;
}
$b = 20; // you could call Sum($a, 20); instead
$b = Sum($a, $b);
echo $b;

According to the Context best way is to pass the variable to the function like this
Sum($b)
But if you are looking for an alternative then you can use closures
but REMEMBER PHP<5.3 does not support closures
You can do
$a = 1;
$b = 2;
function test() {
$b = 20;
function Sum() use($b)
{
$b = $GLOBALS['a'] + $b;
}
}
test();
Sum();
echo $b;

Related

Laravel: accessing variable declared outside of a function

I want to have a variable that may be used inside and outside of the functions. On PHP manual I found the following example:
http://php.net/manual/ro/language.variables.scope.php
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
Manual says:
The above script will output 3.
But my laravel output for this code (in public show function inside a contreller) is 2.
How to make this work as needed?
Try this Code
class TestController extends Controller {
private $search;
public function __construct() {
$this->search = 1;
}
public function global () {
echo $this->search;
}
I solved it by doing so:
*/
public function show(Plan $plan)
{
global $a;
global $b;
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b.'<br>';
So, the idea is to use global on every function.

i want to send parameters to function in an array

i am trying to use this code:
$arr = [
function ($a) {
return $a + $a;
},
function ($b) {
return $b * $b;
},
function ($c,$cc) {
return $c % $cc - $c;
},
function ($d) {
return $d + 4 / $d;
}
];
how can i pass parameters to this functions?
i have already learned this statment for function with no arguments. for example:
function () { echo 'somethings'; } that's mean this function in an array don't have incoming arguments.
$test = rand (0,1);
echo $myarr[$test]();
In addition
can i change (rand) statment with other statment in the above code snippet?
You can do the following:
Your array:
$arr = [
function ($a) {
return $a + $a;
},
function ($b) {
return $b * $b;
},
function ($c,$cc) {
return $c % $cc - $c;
},
function ($d) {
return $d + 4 / $d;
}
];
Your rand
$test = rand(0,1);
---- note ----
you can change your rand() to any other statement provided that it an existing index of the $arr array. in order to know if $test value is an index which exists in the $arr array, you can do
if (isset($arr[$test])) {}
---- end note ---
to call the functions
$returnedValue = call_user_func($arr[$test], 10);
echo $returnedValue;

Access a variable inside a class from outside the class

I have a php class and variable as follows:
class Test(){
function fun(){
$a= 0;
$b = 5;
$sum = $b+c;
return sum;
}
}
I can access the $sum from outside as follows:
$obj = new Test();
echo $obj->fun();
How can I access the value of $b from outside class?
For this scenario you can use Object properties,
Define a public variable inside your class,
class Test(){
public $b;
function fun(){
$a= 0;
$this->b = 5;
$sum = $this->b+c;
return $sum;
}
}
$obj = new Test();
$b = $obj->b; // here null
echo $obj->fun();
$b = $obj->b; // here 5
You need to make $b as a public datamember to be able to access it from outside
class Test() {
public $b = 5;
public function fun(){
$a= 0;
$sum = $this->b + c;
return sum;
}
}
Now you have access to $b by doing this
$obj = new Test();
echo $obj->b;

Global variable inside class and function PHP

I'm having trouble with reading variable defined in one function in another function.
I have:
global $a;
class test{
function aa($somevar){
switch ($myvar){
case 'value':
global $a;
$a = 15;
break;
}
}
function bb(){
global $a;
echo $a;
}
}
$foo = new test();
$ccc = $foo->bb();
var_dump($ccc);
I get dump result NULL.
Thanx
At no point in your code do you assign a value to $a.
The only assignment to $a is in the test->aa method, which uses inconsistent variables and therefore even if called will never assign to $a.
You never run test->aa() to assign a value to a.
$foo = new test();
$foo->aa();
$ccc = $foo->bb();
In this case $ccc will still be null because you are echoing $a in $foo->bb() instead of returning it.
function bb() {
global $a;
return $a;
}
I would also stay away from globals and pass the variable $a on construct of the class. For example:
class test {
public $a;
function __construct($a = null) {
// pass initial var to $a if you want
$this->a = $a;
}
function aa($somevar) {
// reassign $a
$this->a = $somevar;
}
}
$foo = new test();
$foo->aa(5);
// or just $foo = new test(5);
var_dump($foo->a);
The variable $a should be a property inside the class
Here is code try this..
<?php
global $a;
class test{
function aa($somevar){
switch ($somevar){
case 'value':
global $a;
$a = 15;
break;
}
}
function bb(){
global $a;
echo $a;
return $a;
}
}
$foo = new test();
$foo->aa('value');
$ccc = $foo->bb();
var_dump($ccc);
?>
Try this :
class test
{
public $a;
function aa($somevar)
{
switch ($myvar)
{
case 'value':
$this->a = 15;
break;
}
}
function bb()
{
return $this->a;
}
}
$foo = new test();
$ccc = $foo->bb();
var_dump($ccc);
UPDATED:
<?php
class test
{
var $a;
function aa($somevar)
{
switch ($somevar)
{
case 'value':
$this->a = 15;
break;
}
}
function bb()
{
return $this->a;
}
}
$foo = new test();
$foo->aa('value');
$ccc = $foo->bb();
var_dump($ccc);
?>

PHP Namespaces global keyword in function

<?php
namespace Top
{
$a = "Robert";
$b = "Richard";
$c = "Maurice";
function get_a()
{
global $a;
return $a;
}
function get_b()
{
global $b;
return $b;
}
function get_c()
{
global $c;
return $c;
}
echo namespace\Middle\get_a();
echo namespace\Middle\Bottom\get_c();
echo namespace\get_b();
}
namespace Top\Middle
{
$a = "Dauraun";
$b = "Khalid ";
$c = "Humberto";
function get_a()
{
global $a;
return $a;
}
function get_b()
{
global $b;
return $b;
}
function get_c()
{
global $c;
return $c;
}
}
namespace Top\Middle\Bottom
{
$a = "Terry";
$b = "Jesse";
$c = "Chris";
function get_a()
{
global $a;
return $a;
}
function get_b()
{
global $b;
return $b;
}
function get_c()
{
global $c;
return $c;
}
}
?>
So in the above code snippet I am trying to display the correct variable content using a function using the global keyword with the corresponding namespace yet, the desired result is not happening. The returned variable content is that of the namespace where the echo statement is used and not from the specified namespace. The output being 'RobertMauriceRichard.' Can someone please explain? Perhaps it's a misunderstanding on my part of the 'global' keyword inside a function that is in a namespace?
Because only 4 types of code are affected by namespace: classes, interfaces, functions, constants.
So your $a, $b, $c and echo statement are available - and actually the same - across the whole file.
By the time you call namespace\Middle\get_a();, $a is still "Robert", so "Robert" is returned.
Try to put the echo group into different namespace, and you'll observe different result:
namespace Top\Middle
{
/*...*/
echo \Top\Middle\get_a();
echo \Top\Middle\Bottom\get_c();
echo \Top\get_b();
}
/* outputs "DauraunHumbertoKhalid" */
namespace Top\Middle\Bottom
{
/*...*/
echo \Top\Middle\get_a();
echo \Top\Middle\Bottom\get_c();
echo \Top\get_b();
}
/* outputs "TerryChrisJesse" */

Categories