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
Related
This question already has answers here:
Does PHP allow named parameters so that optional arguments can be omitted from function calls?
(17 answers)
can php function parameters be passed in different order as defined in declaration
(9 answers)
Closed 4 years ago.
The issue is, I have a function with 3 parameters. All three parameters have a default value. But is it possible to call the function with only one of the parameters like so:
function check($a=1, $b=2, $c=3){
echo 'a : ' . $a;
echo 'b : ' . $b;
echo 'c : ' . $c;
}
$res = check($c=5);
And expect the following result:
a : 1
b : 2
c : 5
I have tried using this kind of code but what happens is that $c=5 is passed to the first parameter. In this case to $a
No you can't do that in this way. PHP excpects the values in the order you defined in the function.
However it is possible to pass data to your function as an array.
$res = check(array('a' => $defaulta, ..., 'c' => 5))
Where you have your default values in somethin like $defaulta/b/c. Or just write them hardcoded.
Hope this helps!
This question already has answers here:
Only variables should be passed by reference
(12 answers)
Closed 5 years ago.
usort($qus_with_ans, "qus_sort");
$ary_val = max(array_column($qus_with_ans, 'updated_at'));
$ary_key = array_search($ary_val, array_column($qus_with_ans, 'updated_at'));
$k = $qus_with_ans[$ary_key];
$curnt_sub_id = $k['subject_id'];
$curnt_sub_name = $k['s_name'];
$last_question_key = end(array_keys($qus_with_ans));
We have a error Strict standards: Only variables should be passed by reference on last line of code i can't understand why error comes Please fix my issue
line no. 138 are $last_question_key = end(array_keys($qus_with_ans));
You can't use function return in end function and should convert it to the variable.
$keys=array_keys($qus_with_ans);
$last_question_key = end($keys);
However, use arrya_pop which is pushing-out last element from the array
$last_question_key = array_pop(array_keys($qus_with_ans));
If you anyway don't have intension to use keys elsewhere from performance point of view.
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);
}
This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
Closed 8 years ago.
I was wondering if it's possible to do something like:
for ($d=0; $d<20; $d++) {
$productname.$d = $_POST['productname'.$d];
$link.$d = $_POST['link'.$d];
$color.$d = $_POST['color'.$d];
$size.$d = $_POST['size'.$d];
$otherinfo.$d = $_POST['otherinfo'.$d];
$no.$d = $_POST['no'.$d];
$other.$d = $_POST['other'.$d];
}
since the code above doesn't work.
What am I doing wrong? Any help is appreciated.
Your $productname.$d statement has no sense.
Use $productname[$d] instead. It is array approach and it is much more preferable.
P.S.: If you really want so many different variables you can use variable variables (pseudocode is below):
$varName = 'productname'.$d;
$$varName = $_POST['productname'.$d];;
This question already has answers here:
Passing arrays as url parameter
(11 answers)
Closed 9 years ago.
$deposit=$_POST['amountdeposit'];
$arr= array();
for($i=0;$i<10;$i++)
{
if($arr[$i]=='\0')
{ $arr[$i]= array("$deposit");
}
break;
}
$page= "step2.php?arr=$arr";
header("Location:$page");
?>
what i want to do is each time there's a change in $deposit , this value is stored in $arr[$i] and then it is passed in a url so that i could use GET on that step2.php page.
What I see is just arr=array instead of values :/ please help me.
You want http_query_string. It will do exactly what you want.
A couple of other comments have recommended http_query_string, however I would use serialize along with urlencode.
Replace:
$page= "step2.php?arr=$arr";
with:
$page= "step2.php?arr=" . urlencode(serialize($arr));
Then when you get to step2.php, unserialize(urldecode($_GET['arr'])) will contain your array as you originally built it.