pTutorial

New way to learn PHP

PHP 5 Logical Error

5. Logical Error

The logical or semantic error is a fault in program design like the order of instruction. They may cause a problem to respond incorrectly to the user or crash completely.

If there is any logical error in code, the PHP script will compile and run successfully and the computer will not generate any error message. But does not do the right thing.

Logical Error

  • PHP code that does not print last message. One reason may be that the print counter started from 0 not 1.
  • An infinite loop was entered.

Example of logical error

<?php
echo "Table
"; for($i=0;$i<10;$i++) { echo 2*$i."
"; } ?>

Solution of above problem

<?php

Echo "table";
For($i = 0; $i <= 10; i++)
{
Echo"$i";
}  

?>

6. Environmental Error

Environmental Error is a result of factors external to the program and often go undetected by test case designed to catch syntax, semantic and logical error.

These error crop up only when external entities such as files, network connection, input data assumes a form or behave in a member unexpected by the program.

The only way to prevent Environmental Errors is to ensure that PHP program interaction with all external entities are valid.

Example

<?php

$host= "localhost";
$user ="root";
@mysql_connect($host,$user,"") or die("failed to connect");
mysql_select_db("test") or die("failed to select");
echo "Connection established";

?>

This script might work fine, but will fail in a production environment if the proper configuration PHP MySql was not done.

7. Runtime Error

Runtime error refers to the error that occur during execution of the code, which are not usually Programming error, but caused by factors outside PHP script itself, such as database calls or disk or network operation.

Runtime logical error

  • The fopen function falls, because the file passed to it does not exit.
  • The mysql_connect() function falls, because the username specified was accidently deleted by the MySQL administrator.

Example

<?php

fopen("data.txt","r");

?>

The above code block opening a non-existent file. An error message is displayed as shown in the below.


QUOTES OF THE DAY

The heart of melody can never be put down.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER