gpt4 book ai didi

php - GOTO 是一个好习惯吗? (在这个 php 特殊情况下?)

转载 作者:可可西里 更新时间:2023-10-31 23:55:07 25 4
gpt4 key购买 nike

我在向用户显示错误消息时遇到了一些问题。我使用两条 Goto 指令“解决”了这个问题。请看一下代码:

<?php require_once("registration/include/membersite_config.php"); ?>
<!DOCTYPE html>
<html lang="en">
<head><?php include_once("parts/head.php"); ?></head>
<body>
<div id="footerfix">
<?php include_once("parts/header.php"); ?>
<div class="container">
<div class="hero-unit">
<?php
if (isset($_GET['i'])) {
unlink("users/thumbs/" . $_SESSION["user_code"] . ".jpg");
header('Location: profile.php?i=mycv');
}
if (isset($_FILES['avatar']['tmp_name'])) {
$file_ext = end(explode('.', $_FILES['avatar']['name']));
if (in_array($file_ext, array('jpg', 'jpeg', 'png', 'gif')) == false) {
echo("<h2>Error!</h2><p>Your profile photo have to be a picture file.</p>");
goto nomore;
}
$src_size = getimagesize($_FILES['avatar']['tmp_name']);
if ($src_size['mime'] == 'image/jpeg') {
$src_img = imagecreatefromjpeg($_FILES['avatar']['tmp_name']);
} elseif ($src_size['mime'] == 'image/png') {
$src_img = imagecreatefrompng($_FILES['avatar']['tmp_name']);
} elseif ($src_size['mime'] == 'image/gif') {
$src_img = imagecreatefromgif($_FILES['avatar']['tmp_name']);
} else {
echo("<h2>Error!</h2><p>Incorrect file format.</p>");
goto nomore;
}
$thumb_w = 150;
if ($src_size[0] <= $thumb_w) {
$thumb = $src_img;
} else {
$new_size[0] = $thumb_w;
$new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_w;
$thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
}
imagejpeg($thumb, "users/thumbs/" . $_SESSION["user_code"] . ".jpg");
//header('Location: profile.php?i=mycv');
echo('<h2>Ready!</h2><p>Your profile picture is updated. <a href="profile.php">Go back</a>.</p>');
nomore:
echo "</div></div>";
include_once("parts/footer.php");
echo "</div></body></html>";
}
?>

我从来不明白为什么 goto 是代码可能发生的最糟糕的想法(至少,每个人都这么说),我想听听你对此的看法,如果那真的是有史以来最糟糕的想法,那么仍然如何使用我的代码而不使用它们?谢谢!

enter image description here

最佳答案

简短回答为什么 GOTO 是一个坏主意:可读性受到影响。考虑一下:

<?php require_once("registration/include/membersite_config.php"); ?>
<!DOCTYPE html>
<html lang="en">
<head><?php include_once("parts/head.php"); ?></head>
<body><div id="footerfix">
<?php include_once("parts/header.php"); ?>
<div class="container">
<div class="hero-unit">
<?php
if(isset($_GET['i'])){ unlink("users/thumbs/".$_SESSION["user_code"].".jpg"); header('Location: profile.php?i=mycv');}
if(isset($_FILES['avatar']['tmp_name'])){
$file_ext = end(explode('.',$_FILES['avatar']['name']));
if(in_array($file_ext,array('jpg','jpeg','png','gif'))==false){
echo("<h2>Error!</h2><p>Your profile photo have to be a picture file.</p>");
}
else {
$src_size=getimagesize($_FILES['avatar']['tmp_name']);
if($src_size['mime']=='image/jpeg') {
$src_img=imagecreatefromjpeg($_FILES['avatar']['tmp_name']);
} elseif($src_size['mime']=='image/png') {
$src_img=imagecreatefrompng($_FILES['avatar']['tmp_name']);
} elseif($src_size['mime']=='image/gif') {
$src_img=imagecreatefromgif($_FILES['avatar']['tmp_name']);
} else {
echo("<h2>Error!</h2><p>Incorrect file format.</p>");
}
if(!empty($src_img)) {
$thumb_w = 150;
if($src_size[0]<=$thumb_w){
$thumb=$src_img;
}else{
$new_size[0] = $thumb_w;
$new_size[1] = ($src_size[1]/$src_size[0])*$thumb_w;
$thumb=imagecreatetruecolor($new_size[0],$new_size[1]);
imagecopyresampled($thumb,$src_img,0,0,0,0,$new_size[0],$new_size[1],$src_size[0],$src_size[1]);
}
imagejpeg($thumb,"users/thumbs/".$_SESSION["user_code"].".jpg");
//header('Location: profile.php?i=mycv');
echo('<h2>Ready!</h2><p>Your profile picture is updated. <a href="profile.php">Go back</a>.</p>');
}
}
}
?>
</div></div>
<?php include_once("parts/footer.php"); ?>
</div>
</body>
</html>

无论如何,您应该考虑将您的模板与逻辑分离(谷歌“MVC”)并至少使用函数进行复杂操作。

关于php - GOTO 是一个好习惯吗? (在这个 php 特殊情况下?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7557372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com