PHP - Wrong datatype for second argument

  • Context: PHP 
  • Thread starter Thread starter jgg
  • Start date Start date
  • Tags Tags
    Argument Php
Click For Summary
SUMMARY

The discussion centers on a PHP error encountered when using the in_array() function, specifically the "Wrong datatype for second argument" warning. The issue arises from the variable $pb not being defined within the scope of the errorsearch method in the errorchecker class. The solution involves declaring $pb as a global variable within the method, allowing it to be accessed correctly. This resolution highlights the importance of variable scope in PHP programming.

PREREQUISITES
  • Understanding of PHP variable scope and global variables
  • Familiarity with PHP classes and methods
  • Knowledge of the in_array() function in PHP
  • Basic debugging techniques in PHP, such as using var_dump()
NEXT STEPS
  • Research PHP variable scope and how to use global variables effectively
  • Learn about PHP error handling and debugging best practices
  • Explore the use of arrays in PHP and related functions like array_push() and array_merge()
  • Study object-oriented programming principles in PHP, focusing on class properties and methods
USEFUL FOR

PHP developers, especially beginners, who are troubleshooting variable scope issues and seeking to improve their understanding of error handling in PHP applications.

jgg
Messages
40
Reaction score
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


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 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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K