php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21745 in_array not function as intended...
Submitted: 2003-01-19 04:53 UTC Modified: 2003-01-19 09:22 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: jimsonchang at yahoo dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.0 OS: windows95
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jimsonchang at yahoo dot com
New email:
PHP Version: OS:

 

 [2003-01-19 04:53 UTC] jimsonchang at yahoo dot com
function a(){

	$fruit[0] = 'apple';
	$fruit[1] = 'orange';
	
	function aa(){
		global $fruit;
		
		(array) $fruit;
		echo in_array('apple', $fruit) ? 'ok' : 'fail';
	}
	
	function ab(){
		global $fruit;
		if(in_array('apple', array('apple', 'orange'))){
			echo 'ok';
		} else {
			echo 'fail';
		}
	}
}


a();
aa(); // echo fail and Warning: in_array() [function.in-array]: Wrong datatype for second argument
ab(); // echo ok

/*
wondering ? bug : bogus;


May Allah bless all of us!.

sincerely,
Jimson Chang
*/

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-19 05:14 UTC] jimsonchang at yahoo dot com
// functions within functions scope
function a(){

	$fruit[0] = 'apple';
	$fruit[1] = 'orange';
	
	function aa(){
		global $fruit;
		
		(array) $fruit;
		echo in_array('apple', $fruit) ? 'ok' : 'fail';
	}
	
	function ab(){
		global $fruit;
		if(in_array('apple', array('apple', 'orange'))){
			echo 'ok';
		} else {
			echo 'fail';
		}
	}
}


a();
aa(); // echo fail and Warning: in_array() [function.in-array]: Wrong datatype for second argument
ab(); // echo ok

// global scope

$country = array('japan', 'korea');

echo in_array('japan', array('japan', 'korea')) ? 'japan - ok' : 'japan - fail'; // ok
echo '<br>';

echo in_array('japan', $country) ? 'japan -ok' : 'japan - fail'; // ok
echo '<br>';


// function scope

function t(){
	$country = array('Malaysia', 'Singapore');
	echo in_array('Malaysia', $country) ? 'Malaysia -ok' : 'Malaysia - fail';
}
t(); // ok


/*
wondering ? bug : bogus;

looks like in_array() can't function well in
functions within functions scope..



May Allah bless all of us!.

sincerely,
Jimson Chang
*/
 [2003-01-19 05:16 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
 [2003-01-19 05:19 UTC] derick@php.net
oops

It might be related that you have functions defined in functions. Can you try without this?

Derick
 [2003-01-19 05:54 UTC] moriyoshi@php.net
This problem turned out to be a problem of the scripting engine, rather than of in_array(), that "global" declarations don't work in nested functions.


 [2003-01-19 06:10 UTC] moriyoshi@php.net
Sorry, my assumption wasn't right at all.

The error is because $fruit is not initialised in the global
scope.

Try the following:

<?php
function a(){
	global $fruit;
	$fruit[0] = 'apple';
	$fruit[1] = 'orange';

	function aa(){
		global $fruit;
		
		(array) $fruit;
		echo in_array('apple', $fruit) ? 'ok' : 'fail';
	}
	
	function ab(){
		global $fruit;
		if(in_array('apple', array('apple', 'orange'))){
			echo 'ok';
		} else {
			echo 'fail';
		}
	}
}

a();
aa();
ab();
?>

 [2003-01-19 07:06 UTC] jimsonchang at yahoo dot com
function a($param=null){

	$fruit[0] = 'apple';
	$fruit[1] = 'orange';
	
	switch($param){
		case	'R':
			return $fruit;
			break;
			
		default:
			function aa(){
				$fruit = a('R');
				echo in_array('apple', $fruit) ? 'ok' : 'fail';
			}

			function ab(){
				$fruit = a('R');
				if(in_array('apple', array('apple', 'orange'))){
					echo 'ok';
				} else {
					echo 'fail';
				}
			}
	}
}


a();
aa();
ab();
//aa(); // echo ok
//ab(); // echo ok

/*
finally, it works like the way i want... :)
wondering ? bug : bogus <= should change to 'less experience';
cause i using php less than 3 years... :) sorry.

anyway, i learn that 

file a.php
==========
function a(){
	
	$a = 'php';
	
	function ab(){
		echo $a; // doesn't work
	}
}

i understand that global apple is the $GLOBALS['apple'] and
i thought by using the global here, it would maybe means the
scope of function a(). anyway, i am wrong about that..

you might be wondering why i created a function like this?
em... this is to prevent the use of vars in the $GLOBALS scope
why, because i am writing a secure application.

if a people include a.php, then using the $GLOBALS, he can find out
what value in the vars.

anyway, i also think how to prevent people to include the a.php
or calling a()... emm... maybe u people can help me next time...

bogus = [adj] derog pretended; intentionally false: <= from dictionary.

so u see, i am not intended to fool ur people...
so the status shouldn't be bogus...
i am learning and ur people are teaching... :)

May Allah bless all of us!.

sincerely,
Jimson Chang
*/
 [2003-01-19 07:29 UTC] moriyoshi@php.net
Well, it's just one of the current limitations of PHP, so it is possible that some solution for the issue will be announced in the future. Anyway I think you'd better off doing it object-oriented-wise in this case.

And as for the term "bogus", whilst its usage in this bug database is often argued, please note that we don't always mean to be harsh on every report marked so.

Thanks

Moriyoshi


 [2003-01-19 09:18 UTC] jimsonchang at yahoo dot com
/*
i try oop before, but here are the reasons.
*/
class a{
	var $country;
	
	function a(){
		$this->country = array('Malaysia', 'Singapore', 'Japan');
	}
	
	function aa(){
		// after some ...process here..., return Malaysia
		return $this->country[0];
	}
}

/*
$c = new a();
print_r($GLOBALS);

fail because, it treats class in the global scope.
maybe u say it is because it is constructor;
[c] => a Object
(
	[country] => Array
	(
	  [0] => Malaysia
	  [1] => Singapore
	  [2] => Japan
	)
)
*/

// ok, try another
class b{
	function bb(){
		$this->money = array('ringgit', 'dollar', 'yen');
	}
}

/*
the result will be the same, because it appears in global scope
*/

// this time, we use function
function a(){
	return new a();
}

function aa(){
	$a = new a();
	return $a->aa();
}

// OR
function c($param=null){
	$a = new a();

	switch($param){
		case 'aa':
			return $a->aa();
		//.... case ... more for every object method... :(
	}
}
/*
this works, because it is inside the function.
hence, in order to secure that object, we need to write
a wrapper using function for that object... or just
use it inside the function.

let say now we want to run the aa() method of a object. how?
write a new function or using switch()!

so after much consideration
i think i rather write in functions within functions.
and implement my own OOP... :)

a function as a data storage for all function
where function needs to verify themselves before getting
or setting data using the storage function.
*/
 [2003-01-19 09:21 UTC] jimsonchang at yahoo dot com
<?php
// ===================
// copyright statements
// he he he :) :)
// ===================
/*
function a($param=null){
	$fd = fopen('thefilename.php', 'rb');
	$content = fread($fd, 93);
	fclose($fd);
	
	if(md5($content) == 'c38627d91179b94fdd75faa36c8796df'){
		function aa(){
			echo 'aa() ok';
		}
	} else {
		function aa(){
			echo 'who tell u to modify copyright?<br>';
			echo 'i will report u to the police';
		}
	}
}
a();
aa();
*/
?>

this may not relevant, but since i am trying to secure my application... hence maybe ur people can try too!

this idea will prevent people [lots of people out there] to
erase the copyright.. and the creator information :)
 [2003-01-19 09:22 UTC] jimsonchang at yahoo dot com
filename = a.php
================
<?php
// ===================
// copyright statements
// he he he :) :)
// ===================
/*
function a($param=null){
	$fd = fopen('a.php', 'rb');
	$content = fread($fd, 93);
	fclose($fd);
	
	if(md5($content) == 'c38627d91179b94fdd75faa36c8796df'){
		function aa(){
			echo 'aa() ok';
		}
	} else {
		function aa(){
			echo 'who tell u to modify copyright?<br>';
			echo 'i will report u to the police';
		}
	}
}
a();
aa();
*/
?>
just to make it clear...
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Aug 16 16:00:03 2025 UTC