Forum Moderators: coopster

Message Too Old, No Replies

undefined function

rogue function after adding if()

         

WhosAWhata

5:54 am on Apr 10, 2004 (gmt 0)

10+ Year Member



i have a long piece of code that makes a gallery of every picture in a folder, it works well as long as there is at least one picture in the gallery, but when the gallery is empty it returns error messages
i added this code to the begining
$asdfgaa = glob("./{*.jpg,*.JPG,*.Jpg}",GLOB_BRACE);
if($asdfgaa) {
and a
} else {
echo "no pics";
}
like appendix

i thought this would solve the problem, but it causes a Callto undefined function error in the body and i can't understand why, is there something that says a functions cannot be inside an if statement?

brucec

5:52 pm on Apr 10, 2004 (gmt 0)

10+ Year Member



I never heard of the GLOB function, but the problem may be that some internal PHP function is being called.

I would add some File System folder functions and use an

if (!file_exist()) function) {
GLOB...
} else {
echo "no files exist";
}

This way, the GLOB function won't runb and you won't get the error message.

It can get more complicated and sophisticated than this, but you need to do this, at least I believe so.

WhosAWhata

12:57 am on Apr 11, 2004 (gmt 0)

10+ Year Member



all i did was move the function to the top of the file
i suppose you just can't define functions in if() statements

coopster

12:04 am on Apr 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



This code works fine for me. However, it may depend not only on the OS, but also on which version of PHP you are using. There is a User Contributed note in the glob [php.net] manual pages that states...


It's also worth noting that when using the GLOB_BRACE flag in any version of PHP prior to 4.3.4, PHP will crash if no matches are found.

WhosAWhata

12:45 am on Apr 13, 2004 (gmt 0)

10+ Year Member



it has nothing to do with glob...the code was like this

<? 
if (expression) {
//code
function myfunction($var) {
//function code
}

//code
myfunction("this");
} else {
//code
}
?>

i changed to

<? 
function myfunction($var) {
//function code
}

if (expression) {
//code
myfunction("this");
} else {
//code
}
?>

and now it is fine

jatar_k

2:41 am on Apr 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



there really isn't any point to it though

define the function
if true call it
else dont

I am not sure if it is actually improper syntax but it doesn't really make logical sense, pls understand that I mean no offence in any way.

I just don't really see the purpose.

WhosAWhata

5:23 am on Apr 13, 2004 (gmt 0)

10+ Year Member



it really wasn't puroseful,
i was just wondering why...
i started wth a page like this
<?
//code
function myfunction($var) {
//function stuff
}
//code
?>
i realized that there were certain circumstances hat wuld cause the scrit to "behave" badly.

i added an if statemant at the beginning with no thought to the function in the midde of the page

o well, not really important, i suppose it must just be bad syntax

coopster

1:18 pm on Apr 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



In PHP 3, functions must be defined before they are referenced. No such requirement exists in PHP 4. Except when a function is conditionally defined; when a function is defined in a conditional manner, its definition must be processed prior to being called.

It's all right here:
User-defined functions [php.net]

WhosAWhata

9:13 pm on Apr 13, 2004 (gmt 0)

10+ Year Member



hmm...interesting

stevenmusumeche

9:48 pm on Apr 13, 2004 (gmt 0)

10+ Year Member



I've never seen a function defined within program structure. I am about 90% sure that a function can't be defined inside of a function. Anyone know for sure?

jatar_k

9:53 pm on Apr 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Anyone know for sure?

give the link coopster posted a follow and it explains it I believe.