note 125935 added to control-structures.goto
| From: | PHP_is_still_great@php.net | Date: | Wed, 17 Mar 2021 01:09:10 +0000 |
| Subject: | note 125935 added to control-structures.goto | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219237@lists.php.net to get a copy of this message | ||
// goto is STILL a good feature if you know how to use it.
// Just don't use it in loops.
// Example:
$sql = "DELETE FROM sometable WHERE id=?;";
$stmt = $conn->prepare($sql);
if (!$stmt) {
echo "ERR prepare_fail";
goto End;
}
$bind = $stmt->bind_param('i', $id);
if (!$bind) {
echo "ERR bind_fail";
goto End;
}
$exec = $stmt->execute();
if (!$exec) {
echo "ERR exec_fail";
goto End;
}
if (isset($_POST['file'])) {
$file = "../" . $_POST['file'];
if (is_file($file)) { unlink($file); }
}
echo "OK delete_success" ;
End:
$stmt->close();
$conn->close();
exit;
/*
instead of repeating the $stmt->close() and $conn->close(),
we save a few lines by adding a goto and just close everything at the end.
*/
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.246 (proxied: 2001:e68:67f9:b701:598b:fafe:e464:d527)
----
Manual Page -- http://php.net/manual/en/control-structures.goto.php
Edit -- https://master.php.net/note/edit/125935
Del: integrated -- https://master.php.net/note/delete/125935/integrated
Del: useless -- https://master.php.net/note/delete/125935/useless
Del: bad code -- https://master.php.net/note/delete/125935/bad+code
Del: spam -- https://master.php.net/note/delete/125935/spam
Del: non-english -- https://master.php.net/note/delete/125935/non-english
Del: in docs -- https://master.php.net/note/delete/125935/in+docs
Del: other reasons-- https://master.php.net/note/delete/125935
Reject -- https://master.php.net/note/reject/125935
Search -- https://master.php.net/manage/user-notes.php