1

I have an array called $blocks that print_r() function on it will result:

Array ( [0] => template.header [1] => error [2] => emails.addEmail )

and here is my code is:

if(count($blocks)) {
    foreach ($blocks as $block) {
        echo $block;
        $parts = explode('.', $block);
        if (count($parts) > 1) {
            list($folder, $file) = $parts;
            $folder = $folder . '/';
            echo BASE . '/blocks/' . $folder . $file . '.php';
        } else {
            echo BASE . '/blocks/' . $file . '.php';
        }
    }
    $parts = null;
}

but it's acting strangely and echo on line 3 gives me this:

template.header
error
error

The third one must be emails.addEmail but it's duplicating the second one.

Can anyone tell me what is wrong with my code?

Update: I removed @ and replaced it with if (count($parts) > 1). Sorry about that guys. But it doesn't have anything to do with my problem! Still need help...

Update 2: I was using $block variable somewhere before this code and it was connected by reference to another thing. The problem got fixed by changing that variable name. Thanks you guys for your help.

8
  • 3
    What happens if you leave out the include statements? Commented Oct 9, 2012 at 11:50
  • @Jack test it .. it works fine without include ... see : codepad.viper-7.com/sRIxMT Commented Oct 9, 2012 at 11:51
  • @Jack wow! i changed include to echo and now it's showing /var/www/newsletter/blocks/template/header.php 3 times! Commented Oct 9, 2012 at 11:52
  • 7
    Please use array_key_exists or isset instead of error suppression "@" operator. Really bad practice. Commented Oct 9, 2012 at 11:53
  • @WayneC Thanks, but it has nothing to do with my problem! Commented Oct 9, 2012 at 11:56

1 Answer 1

2

My first impression was that some (included) file is somehow manipulating either the $blocks array or $block itself. You should avoid referencing variables that way; keep variable changes close to the scope where they're being used.

Other issues I found:

echo BASE . '/blocks/' . $file . '.php';

Should be:

echo BASE . '/blocks/' . $block . '.php';

Because $file only gets set if $parts contains at least two elements.

Also to test if the split has explode() returned two items:

if (isset($parts[1])) { ... }

Or:

if (count($parts) > 1) { ... }

Working example: http://codepad.viper-7.com/6oNhBv

Sign up to request clarification or add additional context in comments.

5 Comments

What do you mean when you say 'You should avoid changing global variables that way.'?
@faridv Have you looked inside those included files and see what they're doing?
They are empty at this time! I mean they are containing bunch of HTML code, not a single line of PHP.
I found it. Your first answer was right. I was using $block somewhere before by reference. That was the problem. edit your answer to you first one so I can mark it as accepted.
@faridv See my code pastie in the updated answer; it works fine there, so perhaps you could see what's different in your own code and fix it accordingly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.