0

I am trying to insert messages to a function

function addMessage($item) {
     if ($result) {
      $message =  '<p class="ok">
                    <span> Item added </span>
                   </p>
                  ';
                  header("Refresh: 2; url=?page=$item");
        }
        else{
            $message = '<p class=not><span>There is an error blah blah</span></p>';
        }
        return $message;

}

When I use it : addMessage('contents') it only returns to second condition. How can I fix this?

  • which IDE are you using? If you can't pay for Zend, then Eclipse is perfectly acceptable, and there are others, such as CodeLobster ... this is so trivial that you ought not need to ask it here (no offence intended) – Mawg says reinstate Monica Apr 22 '10 at 07:32

4 Answers4

5

You are checking $result inside the if but its neither been assigned any value before that nor been declared as global . I think you meant to check $item:

if ($item) {
codaddict
  • 445,704
  • 82
  • 492
  • 529
1

Is $result defined in your script? Use if ($item) instead.

Be very careful that PHP allows the usage of undefined variables.

Xavier Ho
  • 17,011
  • 9
  • 48
  • 52
1

Hi jasmine

Your function always returns the second condition because you haven't assigned a value to $result, eider inside the function or when you call the function (like unicornaddict mentioned by other words).

To get your code working the way you probably want, your function should be like this:

function addMessage($item, $result) {
     if ($result) { // It will return this condition, case $result has any value assigned and is different from FALSE (boolean)
      $message =  '<p class="ok">
                    <span> Item added </span>
                   </p>
                  ';
                  header("Refresh: 2; url=?page=$item");
        }
        else{ // It will return this condition, case $result doesn't has any value assigned or is equal to FALSE (boolean)
            $message = '<p class="not"><span>There is an error blah blah</span></p>';
        }
        return $message;
}

And then you can call the function like you where already calling it, but don't forget to include a variable or a value that should be handled as the $result variable inside the function

addMessage('contents', $result);

Note:

In your $message variable you have <p class=not> and should be <p class="not">.

Remember that header() must be called before any actual output is sent to the browser.

Hope it Helps.

Community
  • 1
  • 1
Fábio Antunes
  • 16,984
  • 18
  • 75
  • 96
0

what they said :-)

Btw, a decent IDE (like Zend) will analyze your code and warn you about things like that.

Such static code analysis is known as "linting", so google for "PHP lint" or see questions like Is there a static code analyzer [like Lint] for PHP files?

But this code sample is so small that I guess you are a beginner (no offence interned - we all had to start somewhere), so do a lot of reading and gather a lot of tools and experience.

For instance, a decent IDE (like Zend or Eclipse PDT) would let you step through your code, line nby line, and examine the value of each variable and then you ought to have seen the problem.

Welcome to PHP and good luck!

Community
  • 1
  • 1
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • and there's always the classic "I don't see what can be going wrong, so I will echo() teh value of $result just before that "if" – Mawg says reinstate Monica Apr 22 '10 at 07:31
  • My problem in php is that I want to separate logic from html. My teacher writes php function with html code. That is wrong and I want to use php in correct way. I have to forget the wrong way. Its not very easy. Thank you mawg : ) –  Apr 22 '10 at 07:40
  • can you post the whole HTML / PHP poage, so that we can get a better understanding? – Mawg says reinstate Monica Apr 22 '10 at 08:11