ID: 21745
User updated by: [EMAIL PROTECTED]
Reported By: [EMAIL PROTECTED]
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: windows95
PHP Version: 4.3.0
New Comment:
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
*/
Previous Comments:
------------------------------------------------------------------------
[2003-01-19 06:10:20] [EMAIL PROTECTED]
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 05:54:40] [EMAIL PROTECTED]
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 05:19:00] [EMAIL PROTECTED]
oops
It might be related that you have functions defined in functions. Can
you try without this?
Derick
------------------------------------------------------------------------
[2003-01-19 05:16:03] [EMAIL PROTECTED]
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:14:29] [EMAIL PROTECTED]
// 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
*/
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/21745
--
Edit this bug report at http://bugs.php.net/?id=21745&edit=1