Logic Error

A logic error (or logical error) is a mistake in the source code of a program that results in a mistake or unexpected behaviour. This type of runtime error produces the wrong output, or it can also cause a program crash when it is running.

Many different types of programming mistakes can lead to logic errors. For example, we are assigning a value to an incorrect variable that can cause a lot of unexpected program errors. Where there is a need to add two numbers, multiplying them can also result in reverse results.

While minor typos that do not produce syntax errors can cause logic errors, PHP code is described below. For example, an if statement can cause a logic error because it should have a double equal sign (==) instead of a single equal sign (=), which causes a logic error.

Incorrect: if ($ i = 1) {…}

Correct: if ($ i == 1) {…}

In PHP language, “==” means “is equal to,” while “=” means “to become”. In this case, the false if statement always returns TRUE since assigning one returns the variable $ I a TRUE value. While in a correct code, the if statement only returns TRUE if $ i is equal to 1.

Since the syntax of this wrong code will be accepted here, it will not cause any syntax error, and the code will easily compile successfully. Here logic error can be seen during runtime. Since logic errors are often hidden in the source code itself, it is pretty challenging to find them and to debug along compared to Syntax Errors.

See also  GIF (Graphics Interchange Format)

Leave a Comment