Why can't I modify the array in the first function? [duplicate] - php

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)
php: cannot modify array in function?
(2 answers)
Closed 1 year ago.
I want to modify the array in the first function using the second function but it doesn't return the result where it should(first Function).
function first(){
$results = [
'programs' => [],
'events' => [],
];
second('program', $results);
print_r($results); // Don't get results here
}
function second($type,$arr){
$type .= 's';
if(array_key_exists($type,$arr)){
array_push($arr[$type],'title');
}
print_r($arr); // Get results here
}
first();

Related

Why isn't this variable our of context? [duplicate]

This question already has answers here:
Variable scope difference between PHP and C: block scope is not exactly the same?
(1 answer)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 1 year ago.
I'm learning PHP and I encounter this piece of code:
$args = [
"msg" => "hello world!"
];
foreach ($args as $key => $value) {
$$key = $value;
}
echo $msg; // prints hello world!
Why can I acces $msg if it was created inside the for loop? Shouldn't it be out of context?

PHP Undefined variable: array [duplicate]

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)
php function variable scope
(3 answers)
Closed 2 years ago.
I need to check whether selected city ( $_GET['city'] ) behaves in $cities array or not. Right now i'm getting errors "Undefined variable: cities" and "Invalid argument supplied for foreach()". If i check $cities with is_array it returns true. How can i fix my code?
$cities = array ("London" => "name/name2/name3",
"Paris" => "name/name3/name4",
"Moscow" => "name/name5/name6",
"Paraguay" => "name/name4/name5");
function CityIsCorrect() { //IN WORK
if (empty($_GET['city'])) {return false;}
foreach ($cities as $citycheck){
if(($_GET['city'])==$citycheck) {return true;}
else {return false;}
}
}
To directly answer your question – add this line to your function:
global $cities
By default, all variables within a function are "local." If you want to refer to a global function (a variable declared outside of any function-block), you must use global.

How to use the variable inside some function? [duplicate]

This question already has answers here:
How to pass a variable inside a function?
(1 answer)
How to access array element inside another array [duplicate]
(1 answer)
Closed 5 years ago.
I have an API's Function :
$transaction=$tran[4];
function coinpayments_api_call($cmd, $req = array(),$transaction) {
curl_init($transaction);
}
$transaction variable not passing into function coinpayments_api_call.
function not taking values from out side.
I also make $transaction varible GLOBAL ,but still same problem ,
Please Help
Make your code like this
$transaction=$tran[4];
function coinpayments_api_call($transaction,$cmd, $req = array(),$txnid) {
curl_init($transaction);
}

Only variables should be passed by reference in php [duplicate]

This question already has answers here:
Only variables should be passed by reference
(12 answers)
Closed 7 years ago.
I have the following code :
$_SESSION['aParties'] = time();
$aParties[] = $_SESSION['aParties'];
error_log(print_r($aParties,true), 3, "/var/tmp/error.log");
$first = reset(ksort($aParties));
The array aParties is like this :
Array
(
[0] => 1433841062
)
But I get the error :
'Only variables should be passed by reference in the method ksort'
Help me please! Thx in advance
You need to do it as
ksort($aParties);
$first = reset($aParties);
Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference.
Check Docs

Notice: Undefined variable: count line 53 [duplicate]

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 9 years ago.
$count = 0;
$interpreter->addObserver(function(array $row) use (&$temperature) {
$count+=1;
if ($count < 3) <----- not liking this
{
return;
}
$temperature[] = array(
'column1' => $row[16],
'column2' => $row[18],
);
});
I am assuming it is a scope issue and I am unable to access the count from outside however I do need to count the rows in loop....thoughts?
You could refer to the global by adding the following as the first line of your function:
global $count;
However, does it need to be global? You might create a static variable, which will retain its value between your method calls:
static $count = 0;

Categories