Bug #73166 [Opn->Nab]: Function ruturns instruction in finally

From: Date: Sun, 25 Sep 2016 02:04:30 +0000
Subject: Bug #73166 [Opn->Nab]: Function ruturns instruction in finally
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-204247@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73166&edit=1 ID: 73166 Updated by: requinix@php.net Reported by: shoaib dot bazmi at hotmail dot co dot uk Summary: Function ruturns instruction in finally -Status: Open +Status: Not a bug Type: Bug -Package: PHP Language Specification +Package: Scripting Engine problem Operating System: Windows 10 PHP Version: 5.6.26 Block user comment: N Private report: N New Comment: Your code has a mistake (catch needs a class name) and your actual output is incorrect (correct output is 4). https://3v4l.org/kkpGu This is not a bug. Code in a finally block will always execute as soon as execution leaves the try/catch block. The function will initially try to return ++$x (2 and $x=2) but the finally will execute and it will return $x+2 (4) instead. It's like writing $return = ++$x; // try { return ++$x; } $return = $x + 2; // finally { return $x + 2; } return $return; > It is prohibitted in .Net > return statement should be a syntax error if used in finally block It is allowed in Java, Python, and Ruby, and they all behave the same way that PHP does. Previous Comments: ------------------------------------------------------------------------ [2016-09-25 01:15:10] shoaib dot bazmi at hotmail dot co dot uk Description: ------------ While using try catch and finally, the function returns the instruction which is mentioned in finally block. It is prohibitted in .Net, gives syntax error and logically return statement should be a syntax error if used in finally block. I wrote a function which returns and increment by 1 in argument variable and increment by 2 in finally block. Whenever i the function it returs variable incremented by 2 i.e finally block instruction. Test script: --------------- function incrementVar($x) { try{ return ++$x; }catch($e){} finally{ return $x+2; } } echo incrementVar(1); Expected result: ---------------- 2 Actual result: -------------- 3 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73166&edit=1

« previous php.bugs (#204247) next »