[PHP-BUG] Req #71896 [NEW]: break to end of block if error
[email protected] ("p dot peyremorte at free dot fr")
| Newsgroups | php.standards |
|---|---|
| Message-ID | <[email protected]> |
From: p dot peyremorte at free dot fr
Operating system: all
PHP version: Irrelevant
Package: PHP Language Specification
Bug Type: Feature/Change Request
Bug description:break to end of block if error
Description:
------------
Hi,
I would like to propose to add a new instruction, like :
break_if_empty(expression, command);
Description:
If "expression" is empty(undef or false), then execute "command" and
skip until end of current block = skip until next "}" at same level.
Explanation and example:
Each time we have some linear treatment, i.e. on response to POST
request, errors can occurs that must stop the linear treatment and
return an error message, but not stop all the script.
This produce many nested or successive checks of an "error flag"
Exemple :
if (isset($_POST[...])
{
if (empty($_POST[...])
$error = '... must not be empty';
else
{ init something;
if (!mysql....)
$error = 'Cannot reach database';
else
{
.....;
}
}
if (empty($error))
{
if (someting wrong)
$error = 'wrong params';
}
if (empty($error))
{
next steps...
}
etc ...
}
Such structure can be simplified by using function and "exit" or
"return" but it requires to move the full bloc, and only it, in a
function. All external variable must be passed as parameter or declared
as global, inducing frequent miss or bug.
It can also be simplifyed using "goto", but not so much and most of us
really dislike it :
if (isset($_POST[...])
{
if (empty($_POST[...])
{
$error = '... must not be empty';
goto EndBloc;
}
init something
if (!mysql....)
{
$error = 'Cannot reach database';
goto EndBloc;
}
.....;
if (empty($error))
goto EndBloc;
next steps...
EndBloc:
}
With break_if_empty(), my first exemple would resume to :
if (isset($_POST[...])
{
break_if_empty($_POST[...], $error = '... must not be empty');
init something;
break_if_empty(mysql.... , $error = 'Cannot reach database');
.....;
break_if_empty(someting wrong, $error = 'wrong params');
next steps...
etc ...
}
This is a lot easier to read, to understand and to maintain than first
exemple.
:-)
--
Edit bug report at https://bugs.php.net/bug.php?id=71896&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71896&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71896&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71896&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71896&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71896&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71896&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71896&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71896&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71896&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71896&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71896&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71896&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71896&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71896&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71896&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71896&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71896&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71896&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71896&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71896&r=mysqlcfg