有人能告诉我最好的方法来学习如何分辨下面的匹配大括号,比如第一个else语句之后的3个大括号。作为初学者,这非常令人困惑。我目前使用TextWrangler并且看到它没有突出显示匹配的大括号选项。该代码取自Head First PHP& MySQL的。我也看过,Gedit文本编辑器可以选择突出显示匹配的括号,但有人可以告诉我其他人。谢谢。
<?php
require_once('appvars.php');
require_once('connectvars.php');
if (isset($_POST['submit'])) {
// Grab the score data from the POST
$name = $_POST['name'];
$score = $_POST['score'];
$screenshot = $_FILES['screenshot']['name'];
$screenshot_type = $_FILES['screenshot']['type'];
$screenshot_size = $_FILES['screenshot']['size'];
if (!empty($name) && !empty($score) && !empty($screenshot)) {
if ((($screenshot_type == 'image/gif') || ($screenshot_type == 'image/jpeg') ||
($screenshot_type == 'image/pjpeg') || ($screenshot_type == 'image/png'))
&& ($screenshot_size > 0) && ($screenshot_size <= GW_MAXFILESIZE)) {
if ($_FILES['screenshot']['error'] == 0) {
// Move the file to the target upload folder
$target = GW_UPLOADPATH . $screenshot;
if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {
// Connect to the database
$dbc = mysqli_connect(localhost, root , root , guitarwars);
// Write the data to the database
$query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score',
'$screenshot')";
mysqli_query($dbc, $query);
// Confirm success with the user
echo '<p>Thanks for adding your new high score! It will be reviewed and
added to the high score list as soon as possible.</p>';
echo '<p><strong>Name:</strong> ' . $name . '<br />';
echo '<strong>Score:</strong> ' . $score . '<br />';
echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image"
/></p>';
echo '<p><a href="index.php"><< Back to high scores</a></p>';
// Clear the score data to clear the form
$name = "";
$score = "";
$screenshot = "";
mysqli_close($dbc);
}
else {
echo '<p class="error">Sorry, there was a problem uploading your screen shot
image.</p>';
}
}
}
else {
echo '<p class="error">The screen shot must be a GIF, JPEG, or PNG image file no
greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size.</p>';
}
// Try to delete the temporary screen shot image file
@unlink($_FILES['screenshot']['tmp_name']);
}
else {
echo '<p class="error">Please enter all of the information to add your
high score.</p>';
}
}
?>
答案 0 :(得分:0)
正如其他人所提到的,一个好的IDE会为你解决这个问题做很多事情但是没有什么可以说你不能重构代码以使它更具可读性。
e.g。如果你要将各种条件归结为个人明确的陈述,你(以及任何同事)可能会更容易理解。
例如:
$screenshot = $_FILES['screenshot']['name'];
$screenshot_type = $_FILES['screenshot']['type'];
$screenshot_size = $_FILES['screenshot']['size'];
$parameters_set = false;
$acceptable_type = false;
$acceptable_size = false;
if(!empty($name) && !empty($score) && !empty($screenshot)){
$parameters_set = true;
}
if($screenshot_type == 'image/gif' ||
$screenshot_type == 'image/jpeg' ||
$screenshot_type == 'image/pjpeg' ||
$screenshot_type == 'image/png'){
$acceptable_type = true;
}
if($screenshot_size > 0 && $screenshot_size <= GW_MAXFILESIZE){
$acceptable_size = true;
}
if($parameters_set && $acceptable_type && $acceptable_size){
//do stuff!
}
还有一个潜在的未来好处,如果其中一个“子条件”发生变化,添加逻辑说...需要最小的屏幕截图文件大小)你可以修改单个if语句来检查大小条件而不用必须通过单一,非常复杂的if语句找到自己的方式