Modify php default function parameter [duplicate] - php

This question already has answers here:
Only Variables can be passed by reference error
(2 answers)
Closed 5 years ago.
I have a function yaz_wait() which looks like this
mixed yaz_wait ([ array &$options ] ) and as parameters, it has options as you can see in the linked documentation.
One of the options is timeout value which I want to use and edit from its default 15 seconds to some other value.
I have tried
yaz_wait(array("timeout" => 30));
but I get Fatal error: Only variables can be passed by reference...
I am not sure how exactly should I insert this parameter into this function since I have never met with such parameter type (haven't been working with php a lot).

When you have a function with & parameter in a function this means it will return a reference to the variable instead of the value.
In other words, you need to pass a variable that the function will attempt to change(or do whatever with it). Since you aren't passing a variable, you get a fatal error.
Try changing your code to:
$some_arr = array("timeout" => 30);
yaz_wait($some_arr);

Related

PHP count replacement [duplicate]

This question already has answers here:
PHP count JSON array
(4 answers)
Closed 4 years ago.
I have some code that works fine on servers running below PHP 7, but on PHP 7 I get a warning that I need to get rid of. I need to fix the code to get rid of the warning, I can not just hide the warnings.
My issue is with the count() function. Here is the warning I am getting and the little bit of code it is referring to. The array has the possibility of having many elements, some with values and others with blank values. It is also possible that the array will be empty. I assume that when the array is empty, that is when the warning is triggered. So I am looking for a way to tell if the array has 1 or more elements, with and without blank values. As long as there is one key then the if statement should be true.
PHP Warning: count(): Parameter must be an array or an object that implements Countable
$tb_operator_meta_json = get_post_meta($tableid, 'tb_operator_meta', true);
$tb_operator_meta = json_decode($tb_operator_meta_json, true);
$tb_operator_meta = wp_unslash($tb_operator_meta);
if (count($tb_operator_meta) > 0 && $tb_operator_meta != null) {
I don't know why this was marked as a duplicate. If you read my post it is clearly not the same as the other post.
As of PHP 7.2.0
count() will now yield a warning on invalid countable
types passed to the array_or_countable parameter.
http://php.net/manual/en/function.count.php
check the array is_array() before counting.
check if it is an array or not null.
use is_array($var);
or
use (!empty($var))

"Cannot pass parameter 2 by reference" Error in PHP [duplicate]

This question already has answers here:
PHP error: "Cannot pass parameter 2 by reference"
(2 answers)
How to resolve 'cannot pass parameter by reference' error in PHP? [duplicate]
(2 answers)
Closed 5 years ago.
When I run this code for updating my likedOne column and making it empty ("")....
$sql11 = $conn->prepare("UPDATE UserData SET likedOne=? WHERE username=?");
$sql11->bind_param('ss',"",$Username);
$Username = "netsgets";
$sql11->execute();
I get this error....
1 Fatal error: Cannot pass parameter 2 by reference in /xxx/xxx/xxx/test.php on line 36.
The line is....
$sql11->bind_param('ss',"",$Username);
What's wrong?
You need to use a variable , bind_param takes only variable not values directly.
$likedone ="";
$sql11->bind_param('ss',$likedone,$Username);
bind_param(...) expects its param parameters as references. See. That means it might change them (e.g. when they refer to a result of the query). Whenever you pass something as a reference you have to give it a name (sloppy rule of thumb, but makes it easier to explain). In a way this just tells PHP that you care about the potential modification (again simplified). You can use $emptyString = ""; $sql11->bind_param('ss',$emptyString ,$Username);
sorry for my previous answers
you could fix it up by changing
$sql11->bind_param('ss',"",$Username);
to
$variable ="";
$sql11->bind_param('ss',$variable,$Username);
because bind_param works better with variables

Return a value if a PHP GET doesn't exist? [duplicate]

This question already has answers here:
Check if url contains parameters [duplicate]
(3 answers)
How to verify if $_GET exists?
(7 answers)
Closed 9 years ago.
I have lots of PHP statements along the lines of:
$getvalue = $_GET['valueiwant'];
In some scenarios not all variables are available. So, let's say 'valueiwant' doesn't exist in the URL string, how can I return a value based on the fact it doesn't exist?
For example if 'valueiwant' can't be found set $getvalue to -1
Currently it appears the value defaults to 0 and I need to be equal less than 0 if it doesn't exist.
Any ideas?
thanks
I always use
$getvalue=isset($_GET['valueiwant'])?$_GET['valueiwant']:-1;
Use of the isset() function checks if the offset exists, and returns a boolean value indicating it's existence.
This means that you can structure an if statement around the output.

Do I have to always pass all arguments to PHP function? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - Calling functions with multiple variables
function test($var1=null, $var2=null, $var3=null){
//smart stuff goes here
}
Do I have to every time call the function passing all variables?
test(null, $var2, null);
I'd like to pass only $var2 because all the other variables have default values... Is it even possible?
In JavaScript we can pass an object to the function, is there something similar in PHP?
You only have to pass the arguments up to and including the last argument you do not wish to use the default value for. In your example, you could do this:
test(null, $var2);
The last argument can be omitted since the default value is satisfactory. But the first one must be included so PHP knows that you are setting the value for the second parameter.
Until PHP offers named parameters like Python does, this is how it has to work.

Unlimited arguments for PHP function? [duplicate]

This question already has answers here:
PHP function with unlimited number of parameters
(8 answers)
Closed 2 years ago.
In php how would you create a function that could take an unlimited number of parameters: myFunc($p1, $p2, $p3, $p4, $p5...);
My next question is: how would you pass them into another function something like
function myFunc($params){
anotherFunc($params);
}
but the anotherFunc would receive them as if it was called using anotherFunc($p1, $p2, $p3, $p4, $p5...)
call_user_func_array('anotherFunc', func_get_args());
func_get_args returns an array containing all arguments passed to the function it was called from, and call_user_func_array calls a given function, passing it an array of arguments.
Previously you should have used func_get_args(), but in a new php 5.6 (currently in beta), you can use ... operator instead of using .
So for example you can write :
function myFunc(...$el){
var_dump($el);
}
and $el will have all the elements you will pass.
Is there a reason why you couldn't use 1 function argument and pass all the info through as an array?
Check out the documentation for variable-length argument lists for PHP.
The second part of your question refers to variable functions:
...if a variable name has parentheses appended to it, PHP will look for a function with the
same name as whatever the variable evaluates to, and will attempt to execute it.

Categories