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))
Related
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);
This question already has answers here:
PHP syntax for dereferencing function result
(22 answers)
Closed 8 years ago.
I was wondering that we have this function in PHP:
mysqli_fetch_row($result);
and this returns a simple array, but using it as an array like
mysqli_fetch_row($result)[0];
to get the first element of the array doesn't work, so you have to create a new variable which will contain the returned array.
Could anyone explain why isn't this working?
Because mysqli_fetch_row is a function that returns a value - it's not an array itself.
Assigning a variable will retrieve the result. Unfortunately that's how it works in earlier versions of PHP.
Gerald's link identifies the functionality it as availble in PHP 5.4, so I guess that you're using an earlier version.
This question already has answers here:
Parentheses altering semantics of function call result
(2 answers)
Closed 8 years ago.
end(array_keys(array(0))) says PHP Strict standards: Only variables should be passed by reference ( http://3v4l.org/CNLVT )
end((array_keys(array(0)))) on the other hand, just works ( http://3v4l.org/168fi ). Why?
The VLD decompiler shows the same opcodes being ran the only difference is in the ext column but I can't find documentation on what that means.
What's likely happening is array_keys is passing the result back by reference. As such, PHP is throwing you a notice that you shouldn't do that.
Wrapping in parenthesis actually changes the reference and forces PHP to evaluate the statement inside first. As such, it removes the reference. One of those weird things that doesn't look like it makes a difference but actually does.
More on the weirdness here http://phpsadness.com/sad/51
This question already has answers here:
Accessing an array element when returning from a function
(3 answers)
Closed 9 years ago.
Say I have a function/method that returns an array, let's call it ArrayReturner(). But I only want the first element, [0]. Right now I'm doing something like...
$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];
Is there a way to do that in one line without the need for the temporary $arrayReturned array?
Try:
$arrayReturned = reset(ArrayReturner());
Depends on PHP's version you use.
If you're using PHP < 5.4, then you cannot get that, like ArrayReturner()[0]. That's only possible in PHP >= 5.4.
If you want your code to be portable, that would work with old and new versions, then you'd better stick with that code:
$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];
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.