PHP - Wrong datatype for second argument

In summary, the conversation is about a PHP class called errorchecker being used to check for certain strings in an array and print out another string if found. However, there is a warning message being displayed due to a wrong datatype being used for the second argument in the in_array() function. The issue is resolved by making the array global within the function.
  • #1
jgg
40
0
PHP - "Wrong datatype for second argument"

So I build this class in my library file (assume that I have all the PHP tags and stuff)...

PHP:
class errorchecker {

var $error;

function errorsearch($error) {

if (in_array($error, $pb)){
	echo '<div class="error">'.$error.'</div>';
	}
}

}

Where '$pb' is an array I have created, that gets assigned a new string given a certain set of conditions. Thus, I wanted to use this object to check for certain strings in an array, and if it's there print out another string.


Then on the main PHP page, I create an instance of the object and attempt to use it as such...

PHP:
<?php 
			$one = new errorchecker;		
			$one -> errorsearch('String I want to be searched for');
			?>


but it doesn't work, and I get slammed with

"Warning: in_array() [function.in-array]: Wrong datatype for second argument in directory on line xxxx"

Which points to my function errorsearch above. What am I doing wrong (it also does this if I just use a function)? This is my first time writing in PHP, so apologies if I missed something stupid.
 
Technology news on Phys.org
  • #2


Try doing vardump($pb) inside the errorsearch function... is you array defined in there? Otherwise, do you maybe need to define it within your class or make it global inside the function?
 
  • #3


I solved my own problem shortly after posting this...

Try doing vardump($pb) inside the errorsearch function... is you array defined in there? Otherwise, do you maybe need to define it within your class or make it global inside the function?

I made it global and that worked for me.
 

FAQ: PHP - Wrong datatype for second argument

1. What does the error "Wrong datatype for second argument" in PHP mean?

This error means that the data type of the second argument passed in a PHP function or method is incorrect or incompatible with the function's expected data type.

2. How can I fix the "Wrong datatype for second argument" error in my PHP code?

To fix this error, you will need to ensure that the data type of the second argument matches the expected data type. If necessary, you can use type conversion functions to convert the data type before passing it in as the second argument.

3. Which data types are commonly involved in the "Wrong datatype for second argument" error in PHP?

The most common data types involved in this error are strings, integers, and arrays. It can also occur with other data types such as booleans, objects, and null values.

4. Can this error occur with built-in PHP functions or only with custom functions?

This error can occur with both built-in PHP functions and custom functions. It is important to always check the expected data type for any function or method that you are using to avoid this error.

5. Is there a way to prevent the "Wrong datatype for second argument" error from happening in my PHP code?

Yes, you can prevent this error by carefully checking the expected data type for any function or method that you are using and ensuring that the data type of your arguments matches. You can also use type hinting in your custom functions to specify the expected data type for each argument.

Similar threads

Replies
7
Views
5K
Replies
2
Views
1K
Replies
4
Views
6K
Replies
5
Views
2K
Replies
6
Views
6K
Replies
3
Views
2K
Replies
4
Views
4K
Back
Top