Coding Academy 2015杂志 - PHP意外的T_String

时间:2015-03-17 16:08:13

标签: php

我目前正在2015年Coding Academy杂志上学习PHP。教程一直顺利,直到我遇到以下错误:

  

解析错误:语法错误,意外' check_for_race' (T_STRING)in   第13行/home/u345668349/public_html/warddesign2/f1calendar.php

到目前为止,完整的HTML / PHP代码如下:

<?php

$race_data = FALSE;

$races = array( 
'australia' => array('title' => 'australian', 'location' => 'Melbourne',        
'date' => '13/3/2015'),
'malaysia' => array('title' => 'Malaysia','location' => 'Kuala Lumpur',     
'date' => '27/3/2015'),
'china' => array('title' => 'Chinese',  'location' => 'Shanghai', 'date'     
=> '10/4/2015'),
'bahrain' => array('title' => 'Bahrain',    'location' => 'Sakhir',    
'date' => '17/4/2015'),
'spain' => array('title' => 'Spanish', 'location' => 'catalunya', 'date'    
=> '08/5/2015'),
);

if(isset($_POST['location']) check_for_race($_POST['location']);

function check_for_race($location){
global $races, $race_data;

$location = filter_input(INPUT_POST, 'location', FILTER_SANITIZE_STIRNG,       
FILTER_FLAG_STRIP_LOW);

if(isset($races[$location]))
$race_data = $races[$location];
else $race_data = 'No matching races found';
return; 
}

?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<form method="post">
<fieldset>
<label for="location">Choose a race:</label>
<select name="location">
<?php foreach($races as $location => $race): ?>
<option value="<?php echo $location; ?>"><?php echo $location;?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="View">
</fieldset>
</form>

</body>

</html>

它似乎不喜欢第13行,如下所示:

if(isset($_POST['location']) check_for_race($_POST['location']);

如果有人可以提供帮助,我们将不胜感激。我已经多次阅读过这本书和代码,甚至改变了它的顺序,但似乎没有任何效果。

1 个答案:

答案 0 :(得分:3)

你错过了一个右括号:

if(isset($_POST['location']) check_for_race($_POST['location']);

应该是:

if(isset($_POST['location'])) check_for_race($_POST['location']);
                    //      ^---- notice this

养成在if()语句和循环中使用大括号的习惯可能会更好。它使这样的调试变得如此简单:

if( isset($_POST['location']) )
{
    check_for_race( $_POST['location'] );
}