在php中匹配大括号可能会令人困惑
有人能告诉我学习如何辨别下面哪些括号是匹配括号的最佳方法吗,例如第一个else语句后面的3个括号。作为一个初学者,这是非常令人困惑的。我目前使用TextWrangler,看到它没有突出显示匹配大括号选项。代码取自headfirstphp&MySQL。我也看到了,所以Gedit文本编辑器可以选择突出显示匹配的大括号,但是有人能告诉我其他的吗。谢谢在php中匹配大括号可能会令人困惑,php,Php,有人能告诉我学习如何辨别下面哪些括号是匹配括号的最佳方法吗,例如第一个else语句后面的3个括号。作为一个初学者,这是非常令人困惑的。我目前使用TextWrangler,看到它没有突出显示匹配大括号选项。代码取自headfirstphp&MySQL。我也看到了,所以Gedit文本编辑器可以选择突出显示匹配的大括号,但是有人能告诉我其他的吗。谢谢 <?php require_once('appvars.php'); require_once('connectvars.php');
<?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>';
}
}
?>
正如其他人所提到的,一个好的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!
}
$screenshot=$\u文件['screenshot']['name'];
$screenshot\u type=$\u文件['screenshot']['type'];
$screenshot\u size=$\u文件['screenshot']['size'];
$parameters\u set=false;
$acceptable_type=false;
$acceptable_size=false;
如果(!empty($name)&&!empty($score)&&!empty($screenshot)){
$parameters\u set=true;
}
如果($screenshot_type=='image/gif'||
$screenshot\u type=='image/jpeg'||
$screenshot_type=='image/pjpeg'||
$screenshot\u type=='image/png'){
$acceptable_type=true;
}
如果($screenshot\u size>0&&$screenshot\u size)使用IDE或至少使用一个常用的文本编辑器,如notepad++。这些程序使您能够折叠代码并直接跳转到匹配的括号中。IDE的优势(如netbeans)它显示了潜在的解析错误。因此,您甚至不必运行文件来查找错误。最好的方法是使用一个体面的IDE或显示匹配大括号的编辑器,或者至少除了IDE的@ProfileTwist建议外,还可以清晰地缩进代码,通过拉出可以很好地外部化的代码来减少函数的大小o单独的函数也有助于提高可读性。关于缩进,你应该为每一个新的嵌套代码级别使用一个制表符或4个空格。2个空格是不够的,而且在业界传统上是不被接受的。经验有帮助,你编写的代码越多,你就越能自然地看到它的工作原理。感谢scunliffe提供的上述代码。作为一个例子内心深处,我能比我从书中抄出来的代码更好地阅读你的代码。我想知道为什么这本书不会像你上面那样简化他们的代码。