0

Why do people write 'exit' after the ajax function calls end

function ajaxValidate($params)
{
  // echo something
  exit;
}

When the function blocks end the code executions stops at server side so why break the code manually ?

4
  • 1
    it depends on how you have implemented the code. As for example if you are using wordpress's ajax, if you don't exit, the code will return an extra 0. Commented Aug 5, 2014 at 8:30
  • well in php i havent noticed such thing but +1 for telling me about wordpress :) Commented Aug 5, 2014 at 8:32
  • you'll also see lots of exit(json_encode($array_of_data)), that's because you want to make sure your output doesn't contain anything else but the response you expect to get on the client side. Commented Aug 5, 2014 at 8:37
  • 1
    Who is "people"? Haven't seen this kind of madness yet. Commented Aug 5, 2014 at 8:37

1 Answer 1

2

The function will be called via the server-side framework, be it Joomla, Yii or anything else. So after the function returns, the framework continues to run to completion. In a cleanly designed framework, exit statement is not required as you rightly point out.

If the programmer does not understand the framework well and wants to play it safe, or has seen redundant exit values appearing the output, he/she may add the exit statement.

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

2 Comments

nyc answere but i would like to add yii its self uses Yii::app()->end(); in ajax validation but this doesnt mean they dont know anything about their framework. For me the core reason behind using exit is still unclear
Excellent. Yii is a good framework to use. Thanks for pointing out.

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.