即時通訊方式存在一些問題,目前我的用戶錯誤mensage。我用兩個Goto指令「解決」了問題。請看看代碼:GOTO是一種很好的做法嗎? (在這個PHP的特殊情況?)
<?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是可能發生的代碼(至少,每一個說)最壞的想,我會想聽聽你對這個意見,如果這真的是最糟糕的想法,那麼我的代碼如何使用它們呢?謝謝!
我會說答案是「不」。 –
請參閱Steve McConnell的Code Complete中的http://www.stevemcconnell.com/ccgoto.htm。當你有時間時閱讀整本書。 –
抱歉是粗魯的,但使用'goto'是你的問題中最少的。最明顯的問題是,代碼首先是完全不可讀的。 –