floor() in Php

Wednesday, November 5, 2008 23:27
Posted in category PHP, Programming

floor() is the opposite of Ceil(). It rounds down a floating-point number to the next-lowest integer, no matter what the value of the floating-point number is.

Integer on success; FALSE on error

Example #1 floor() example

< ?php
echo floor(4.3); // 4
echo floor(9.999); // 9
echo floor(-3.14); // -4
?>

ceil() in php

Wednesday, November 5, 2008 23:16
Posted in category PHP, Programming

The ceil() function Returns an integer that is the next larger than the float used as the argument. In essence, it rounds up a floating-point number to an integer, no matter what the value of the floating-point integer is. It doesn’t round down.

Example#1 ceil() example

< ?php
echo ceil(4.3); // 5
echo ceil(9.999); // 10
echo ceil(-3.14); // -3
?>

Returns

Integer on success; FALSE on error

multiple catch block in php

Saturday, November 1, 2008 9:56
Posted in category PHP

Some time there is a need to catch more than one exception..In such a situation, we can associate more than one catch statement with a try, When an exception is thrown, the exception handlers are searched in order for an appropriate match. The first handler that yields a match is executed. After executing the handler, the control goes to the first statement after the last catch block for that try. When no match is found, the program is terminated.

$val = { 0, 2, 13, 0, 5, 6, 7, 8 };
for($i=0; $i < $val.length; $i++) {

try {
$k=$val[$i]/$i;

echo $k;

echo $val[$i+1];

}
catch (DivideByZeroException) {
// catch the exception
echo (“Can’t divide by Zero!”);
}
catch (IndexOutOfRangeException) {
// catch the exception
echo (“No matching element found.”);
}

}

When the try block does not throw any exception and when it completes normal execution, the control passes to the first statement after the last catch handler associated with that try bock.

we can have try block in catch block also... we can use nested try blocks..

throw Exception in php

Friday, October 31, 2008 9:52
Posted in category PHP, Programming
 try {
   doExchange();
   throw new Exception("D'oh!");
   echo "hai";
  }
catch (Exception $except) {
    logger.error("doExchange failed", e);
}

In that example, PHP enters the “try” block and starts executing code. When it hits the line “throw new Exception”, it will stop executing the “try” block, and jump to the “catch” block. Here it checks each exception option against the list in “catch” and executes the appropriate code. Once PHP has left the “try” block, it will not return to it.

The $except variable after the Exception class is there because PHP actually hands you an instance of that Exception class, set up with information about the exception you’ve just experienced. As all exceptions extend from the base class Exception

try and catch block in php

Thursday, October 30, 2008 8:47
Posted in category PHP, Programming

Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.
Try block is used to try for the code to execute if any error like file unable to open etc.. It will stop the execution and it will throw exception like file not found exception.. We can catch this exception in catch block.. If try will not throw any exception catch block never execute.

try {

//open a file which is not existing

$row = $this->db->fetchRow( $sql );

return $row;

}

catch( Exception $e ) {

// does some custom error handling

mysqlErrorCheck( $e, __LINE__, __FILE__ );

}