How to use assert () in PHP

* {box dimensions: edge of the box; } body {margin: 0;} * {box-sizing: border-box;} body {margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;}

In this article, I’ll teach you the assert() function in PHP. I’ll teach you exactly what it is, how it works, and the right context to use it in your code. Along the way, I’ll use simple sample code snippets that will help you understand the concept of assertion even better.

Let’s start!

Which is assert() Function in PHP?

assert() is a function statement used to verify that a condition is always true. PHP uses this function as a debugging tool to trigger errors whenever a certain defined condition is not met.

The assert() Syntax

assert() takes two arguments: the assertion and an error message.

The first argument is the condition that must be met for the rest of the code to continue running. The second argument is normally an error message that is triggered when the condition (which is the first argument) is not met.

The assert() the function evaluates the first argument—$assertion—Which is a string containing an expression to evaluate, a bool or an expression. The program continues to run smoothly if this is true, which means that the asserted condition has been completely satisfied. Otherwise, it throws an exception.

The $assertion Expression

The assertion can be a string containing an expression, a Boolean value or, from PHP 7, an expression. This is done and its result is used to indicate success or failure of the assertion.

The exception or description message

In PHP 5, this was a string describing the error that occurred. If this argument is undefined, then a default description is essentially the same as the source code for the invocation of assert() instead it is expected.

Example of how to use assert() in PHP

Now, let me show you a simple code that it uses assert().

The code snippet above defines a function to return the first letter of a string. If the string contains no letters, however, the function will throw an exception.

Our localhost server would display this:

How to use assert()

PHP uses assert() as a native debugging tool. In other words, assert() it is used to check parts of the code making sure they meet some required criteria or a defined condition. As we saw in our example above.

Using the assert() tool, you can make sure that the internal data that is passed into your application is of the correct type. For example, let’s consider an example where assert() validates a variable type.

In the following example I will define a function and pass it two variables. I will then use assert to check if these variables have a float value. assert() it should throw an exception when a different variable type is passed instead.

When I called the function, I passed in the values ​​for $x And $y 1.00 and 6 respectively.

Running this code would return the following:

The output of the code indicates that the exception $y must be a float value was thrown because the value 6 passed to $y parameter is not of type float but is instead of integer type.

Let’s consider another example where a default exception is thrown due to our exception not being defined.

I added a condition that sets the value of the $ x parameter to 0. So we return the division of $ x by $ y. We already expect a default ZeroDivisionError exception since, by default, it is not possible to divide an integer by zero.

Run the code and you should see this now:

The first line in the output throws an exception because the condition is not met. The value of $ x does not have to be zero yet, this is what has been defined here.

The second line in the output displays a default exception message of ZeroDivisionError because we divided 1 with a value of 0, contrary to the rules of mathematics.

This is basically all you need to know assert() and its functionality.

Disabling assertions

As you can see, assertions can be a useful tool for debugging our code. Assertions help you spot errors early and alert you if something unexpected is happening.

It is important to note, however, that the use of assert() it is mainly during the development phase. Do not use assert() to detect and report errors for the production application, as assertion errors can be turned off with a setting php.ini.

Disable assertions for a production system, use the zend.assertions directive in php.ini.


You now know how to declare assertions in your code to detect errors and aid in debugging. As you have seen, it is also very easy to implement.

Good coding!

Source link

By LocalBizWebsiteDesign

Leave a Reply

Your email address will not be published. Required fields are marked *