0

in my case , i need to return value from a ajax page. as i know , there is two way to do it: echo , return.

echo 0;
exit;

or

return 0;
exit;

is there any different between them?

2 Answers 2

3

If I were you, I would use :

echo '0'; 
exit();

Why ? Because you don't want to output an integer (treated as bool in PHP, I assume 0/1). You want a string so AJAX can read it.

Return is for function and stuff, not to end a page.

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

2 Comments

You can echo the integer 0 and it will show up just the same as the string '0'. If you echo false, however, nothing shows up in the output. Still, I suppose it is safest to always echo as a string to ensure that extra data isn't truncated, like echo 1.0, which would just echo '1' and leave off the '.0'.
Yes, true. But it is only a "principle". PHP threat 0 and 1 as false and true respectively. An HTML page must be treated as a giant string and not an integer. This is why I suggest using echo '0'; even if the output is the same;
0

AJAX retrieves the output from another page. echo will output something, but return is for returning a value from a function.

Use echo.

Comments

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.