Jorge
Jorge

Reputation: 5676

php function returning a string with a new line?

In my first 3 days in coding my homework. I successfully did it. But when I got to school, my code have a problem. I have an ajax form and some php functions.

Here is an example.

$i = 1
if($i == 1) {
   echo "yes";
}
else {
   echo "no";
}

The answer in my sample is true. But in my case it's always false. And I notice the ajax requests that the returned message was

    // new line
    // new line
yes
    // new line

That's why it's being false. What is the cause of this? I didn't put any new line in my code.

Upvotes: 0

Views: 133

Answers (3)

Ibu
Ibu

Reputation: 43810

if your code doesn't include any html, simply open:

<?php 

you don't need to close it. This will help you eliminate any whitespace or new lines

Upvotes: 1

Starx
Starx

Reputation: 78971

If you want the easy way out of it, try this

ob_clean();
echo "yes";
exit;

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 400962

You must have newlines somewhere :

  • either at the beginning and end of your script
  • or at the beginning and/or end of some file it includes

Note : make sure you don't have anything outside the <?php ... ?> tags : it would get sent to the browser.

Upvotes: 4

Related Questions