DefinitionIt's a function of https://en.wikipedia.org/wiki/Assertion_(software_development) . It exists in almost all languages. Some have more sophisticated and alternative mechanisms.Assertion - Proposition which is thought to be true http://www.priberam.pt/dlpo/asser%C3%A7%C3%A3o .So it's a resource to prove if something is true. It is not a decision mechanism (question) as it is a if. It's just a statement.When to useWe use it when we expect it to always be true. In one if We hope that both things will happen. No assert a false result indicates problems. It's a documentation mechanism, not the algorithm itself. His only goal is to say whether or not something matches the expectation that the programmer wrote there.In PHP I do not see people using. The type of application (doesn't need maximum robustness, after all are scripts, right?) and the kind of typical PHP programmer does not care much about it.We don't use it. if because this code is not part of the application, it is part of the "test" of the application. The goal is another. In production she "does not exist". In development helps identify situations where the code is not behaving as expected.This function should be used only for formal tests or for any debugging tests. So she can be turned off. Then in production it should not run, and it will not cost (in PHP technically it has because it will need to be interpreted, but not executed, but that cost is/should be negligible). Obviously some tools can be used to automate the verification process.Mechanism is different from resultThat's what I said in one https://pt.stackoverflow.com/a/165499/101 . Programmers need to dissociate the idea of a Boolean expression and if walk together. In this case the Boolean expression serves to say for the programmer if the code is ok, but it is the used mechanism that does it. The Boolean expression only generates a result, what will be done with it is the subject of the programmer. Can be used in:a calculation,in a flow control instruction that makes a decision,just print,or if you just want to know if that is correct,etc.Validity and quality of useThe quality of the help she gives to test is as good as the programmer's ability to identify what can go wrong. Naïve programmers tend to find themselves filled with assert() or other similar things the code will be bulletproof (as occurs with the exception). Relapso programmers don't care about tests of any kind. Pragmatic programmers know when to use every thing.This is obtained by studying each resource, seeing use in good third-party codes and using your own experience. It is normal to have difficulty using at first not only the assert()almost anything in programming. Therefore it is always interesting to have quality mentors to go showing in each case whether it is doing right or not.Tests and contractsNote that this has nothing to do with https://pt.stackoverflow.com/q/19177/101 and other existing tests outside the code. But asserts They are usually used within them.It can be seen as a https://en.wikipedia.org/wiki/Design_by_contract poor. Therefore it is very common to be used to evaluate function parameters, results that they return, or if the state of an object came out of a valid situation at some point. In some languages it is part of the language and helps the compiler produce better codes. In a dynamically typed language is more important yet and type verification is one of the greatest uses.An example would be to check if the received parameter is within a range of values that is expected. Remembering that differs from if because in this case if a number is out of the track means that there was a programming error, it was not an expected situation. If formal tests are done properly in every application, you may find a problem using that function before sending it to production.AbuseSome people use as code control in production. This can be considered an abuse, even though it works. In general the execution is interrupted, as an exception, after all if a assert is false, there is a programming error that needs to be fixed. It is possible even to throw an exception optionally.When you do this you cannot disable your use, then assert passed to be used in place of if, mixed mechanisms, used the test where it should be the logic of the algorithm.Reading supplement https://pt.stackoverflow.com/q/128991/101 https://pt.stackoverflow.com/q/127118/101 https://pt.stackoverflow.com/q/85323/101 https://pt.stackoverflow.com/q/94/101