I am receiving the following warning:
Warning: reset() expects parameter 1 to be array, null given in /data/9/1/115/118/1767118/user/1910351/htdocs/aw/home/wp-content/themes/awthemesnew/library/sidebars.php on line 183
Keep in mind I am editing a wordpress .php file. Here is the full code on line 183:
function theme_print_sidebar($name, $places) {
$style = theme_get_option('theme_sidebars_style_' . $name);
$place_count = count($places);
if ($name != 'footer' && $place_count < 2) {
theme_print_widgets(reset($places), $style);
return;
}
?>
Help. Thanks!
The value of $places is apparently null by the time you call reset.
Your code is saying "only call reset when the value of $place_count is less than 2".
You're setting the value of $place_count via the statement:
$place_count = count($places);
We can infer that when count() is called on a null variable it returns 0. Since 0 is less than 2, the following statement is executed:
theme_print_widgets(reset($places), $style);
However, at this point the fact that $places is null is causing an error. I would be curious to know under what circumstances $places is null. Once you've got the answer to that you can decide how to handle that case.
I figured it out. I had erased a command in the footer and when the code when looking for it, since it was gone, it gave me a array warning. Thanks for the help.
Related
i have an issue with a line for several days.
Here is my code :
$files = array();
$count = count(array_filter($_FILES['fichier']['name']));
echo $count;
for($i=0;$i<$count;$i++){
array_push($files, $_FILES['fichier']['tmp_name'][$i]);
}
This code is in a form, but when i submit my form, i have this error message :
Warning: array_filter() expects parameter 1 to be array, null given
in...
This part of code works in FR version, but when i tried to copy/paste in EN version, it doesn't work.
I've tried everything, nothing happens.
Thanks.
EDIT :
var_dump(array_filter($_FILES['fichier']['name']));
return me this :
NULL
when i'm trying to upload 1 or many files.
I think when you see this error you can use this:
<td>{{ is_array($client->phone) ? implode($client->phone, '-') : $client->phone }}</td>
The array_filter function in your source needs an array as parameter, and you use a string. You should ensure that the parameter is an array. If you check for that, you should use the help of is_array.
You have to check first that $_FILES coming as an array or not using is_array()
if(is_array($_FILES['fichier']['name'])){
$count = count(array_filter($_FILES['fichier']['name']));
} else {
$count = 0;
}
echo $count;
I have this bit of code:
if( array_key_exists('customtextcolor',$atts) ){
// If in array, then check if none, if not none, add to CSS classes
if ( 'none' !== $atts['customtextcolor'] ) {
$custstyles[] = $atts['customtextcolor'];
}
}
Which is on line 90, and my php.log shows an error on line 90.
Apr 2 08:46:16 #####.net ool www: PHP Warning: array_key_exists() expects parameter 2 to be array, string given in /var/www/####/wp-content/plugins/Fusion-Builder-Custom-Text-For-HP/fusion-builder-custom-text-for-hp.php on line 90
Which I thought was strange, because I was sure it is was an array, because the code works as expected. I also used echo gettype($atts); and it returns an array. When I var_dump($atts); it renders the full array.
Any idea what php.log is showing this error message?
If $atts is a string, why does gettype() identify it as an array? And why does var_dump() render it as an array?
I've looked at the other array_key_exists() questions on StackOverflow, but they aren't the same issue as far as I can tell.
Change the if condition as below:
if(is_array($atts) && array_key_exists('customtextcolor', $atts))
This will check if $atts is an array at first then will check the array_key_exist operation.
Just write the following line of code:
array_key_exists('customtextcolor', (array) $atts);
This will cast the $atts-variable into an array if it's not already an array.
You can also just check if there is an array with a value per:
isset($atts['customtextcolor']);
I am running into a "Only variables should be passed by reference" error, because on the code I am using there is a line that does not put the explode() result into a variable. As required when using strict PHP standards.
However because the explode() function is used in a While loop I can't think of a appropriate solution.
My code looks like
function user_exists($username) {
rewind($this->fp);
while(!feof($this->fp) && trim($lusername = array_shift(explode(":",$line = rtrim(fgets($this->fp)))))) {
if($lusername == $username)
return 1;
}
return 0;
}
Any suggestions on how to solve this?
I think maybe you need to sit back and break your code apart a bit and take a look at what is happening.
First, condition is while !feof($this->fp)
From the manual:
feof — Tests for end-of-file on a file pointer
One thing you will notice here is that feof() is only a test which returns true or false. It does not advance the pointer position while looping over, so while using this function, somewhere else in your while loop there needs to be something that advances the pointer or else you will have an infinite loop.
Second condition is:
trim($lusername = array_shift(explode(":",$line = rtrim(fgets($this->fp)))))
First function from left to right is trim(), which returns a string. From our handy dandy comparison table we see that when doing if ((String) $var) it evaluates to false if and only if the string is empty ("") or the number zero as a string ("0"), otherwise it returns true. Personally I tend to really hate using if ((String) $var) (first because it's slightly unclear to newbies unless you know your comparison table well and second because 99% of the time people are doing that they are actually checking for string length, in which case I would want it to return true for the string "0"). So assuming that you don't need it to return false for "0" we could change this to strlen($var) > 0 and then manipulate the variable within the loop. That should greatly simplify things here.
So now we have:
while (!feof($this->fp) && strlen($var) > 0) { /*...*/ }
This will loop over until either we are at the end of the file or $var is an empty line. Everything else can be offloaded into the body of the while loop, so it is much easier to break apart.
So this is what we have now:
$line = rtrim(fgets($this->fp));
$lusername = array_shift(explode(":",$line)));
Uh-oh! There's that "nasty" error:
Strict Standards: Only variables should be passed by reference in /path/to/file.php on line x.
So we can see from here, the part producing the error is not explode(), but array_shift(). See also: Strict Standards: Only variables should be passed by reference
What this means is that since array_shift() modifies the array, it requires it to be by reference. Since you are not passing an actual variable but instead the result of a function, PHP is unable to modify it. It's similar to doing something like function($var) = 3;. Of course you can't do that. Instead you need to save the value to a temporary variable. So now we have:
$line = rtrim(fgets($this->fp));
$split = explode(":",$line);
$lusername = array_shift($split);
Woo hoo! No more warning message.
So putting this together, we now have:
while (!feof($this->fp) && strlen($lusername) > 0) {
$line = rtrim(fgets($this->fp));
$split = explode(":",$line);
$lusername = array_shift($split);
if($lusername == $username) {
return 1;
}
}
Also, as mentioned earlier, the fgets() will advance the pointer, which allows the !feof($this->fp) part in the while statement to vary.
I'm new in php and having problem with a piece of my code if the key given exist. The code of error are below and i don't have a idea what is causing it. Please help.
"Warning: array_key_exists() expects parameter 2 to be array, boolean given in"
The line
if ( is_array(array_key_exists('sizes',wp_get_attachment_metadata($attach_id)) == false ){}
As ArSeN already pointed out, is_array(array_key_exists( doesn't make much sense. I guess you were trying: a) is not array or b) lacks a specific key.
Using a temporary variable you can do something like
if ( !is_array($meta=wp_get_attachment_metadata($attach_id)) || !array_key_exists('sizes', $meta) ) {
// ....
}
Or, in case you try to access the meta data more often (or simply as a coding style):
$meta=wp_get_attachment_metadata($attach_id);
if ( !is_array($meta) || !array_key_exists('sizes', $meta) ) {
// ....
}
You could drop a ternary into that thing to uglify the hell out of it.
if ( is_array(array_key_exists('sizes',wp_get_attachment_metadata($attach_id)) == false) ?: array() ){}
This way if the attachment metadata does not exist, it will provide an empty array, which in turn will produce a false from array_key_exists, which is similar to the behave of the desired code.
I am getting this error in a PHP class...
Fatal error: Can't use method return
value in write context in
C:\webserver\htdocs\friendproject2\includes\classes\User.class.php
on line 35
Here is the troubled part.
if(isset($this->session->get('user_id')) && $this->session->get('user_id') != ''){
//run code
}
This code is in my contrustor, is a value is not already set for $this->session->get('user_id') then it will return false instead of a Number. So as you can see I was hoping to check if this value is a number or not or not even set.
Any help with fixing appreciated.
You can't use isset for the result of a function. Consider the following code instead:
if( $this->session->get('user_id') ){
//run code
}
isset() only works with variables as
passing anything else will result in a
parse error. For checking if constants
are set use the defined() function.
From the PHP Manual.
You can't use isset on a function. However, since false, 0, and '' all equate to a falsey statement, write your test this way:
if( $id = $this->sessions->get('user_id') ){
// Will only run if $id does not equal '', False, or 0
}
That way you have run your test and assigned the variable in one step.