PHP Help

DCT Jared Smith jared at dctkc.com
Thu May 30 13:19:51 CDT 2002


Jose, try this:

In your 'FOR' loop, you have an 'IF' statement which
contains two commands, 'print' and 'return'.

>function checkId( $id )
>{
>  global $text;
>
>   for($i=1; $i<5; $i++ )
>   {
>    if( $text[$i]==$id )
>     print( 'Compared...' );
>     return true;
>   }
>   return false;
>
>}

put curly brackets around those two statements like this:

function checkId ($id) {
    global $text;
    for ($i=1; $i<5; $i++) {
        if ($text[$i]==$id) {
            print('Compared...');
            return true;
        }
    }
    return false;
}

I did that to your code, and the print statement works.

Why? In short, 'if' statements work the way you used it
if you are using one consequence, like:

if ($this) dothat();

otherwise, you need to put brackets, like:

if ($this) { dothat(); anddothatalso(); }

I have seen PHP render two different results depending
on this structure, so to be safe, I always enclose 'if'
consequences in curly brackets, even when not needed:

if ($this) { dothat(); }

==========================================================
Another note: I agree with Dustin Decker regarding your 
liberal use of global variables.

For instance, while I can see the value of making $text
a global variable, $path does not need to be. Simply
make your get_file() function return a path, and
call it thusly:

include(get_file($currentPath++));

In a small script like this, it's not a big deal, but in
a larger script, managing the status of a whole bunch
of global variables can make debugging a bear.

-Jared




More information about the Kclug mailing list