I have got a parse error inside my php script, involving T_String error on line 22
Parse error: syntax error, unexpected 'toys' (T_STRING) in C:\xampp\htdocs\registrationform.php on line 22
<?php
$toys = array('digicam' => 'Digital Camera',
'mp3' => 'MP3 Player', 'wlan' => 'Wireless LAN');
$errors = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors = validate();
}
function validate() {
$errors = array();
if (empty($_POST['name'])) {
$errors['name'] = 'You must enter your name.';
}
if (!is_numeric($_POST['age'])) {
$errors['age'] = 'You must enter a valid age.";
}
if (empty($_POST['toys'])) {
$errors['toys'] = 'You must choose at least one toy.';
}
return $errors;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
<style type="text/css">
label.error {
color: red;
}
</style>
</head>
<body>
<h1>Registration Form</h1>
<p>Please fill out the form below to register for our site. Fields
with bold labels are required.</p>
<?php if (!empty($errors)) { ?>
<ul>
<?php foreach (array_values($errors) as $error) { ?>
<li><?= $error ?></li>
<?php } ?>
</ul>
<?php } ?>
<form method="post" action="<?=$_SERVER['PHP_SELF'] ?>">
<p>
<?php if (array_key_exists('name', $errors)) { ?>
<label for="name" class="error"><b>Name:</b></label>
<?php } else { ?>
<label for="name"><b>Name:</b></label>
<?php } ?>
<br />
<input name="name" value="<?= strip_tags($_POST['name']) ?>" /></p>
<p>
<?php if (array_key_exists('age', $errors)) { ?>
<label for="age" class="error"><b>Age:</b></label>
<?php } else { ?>
<label for="age"><b>Age:</b></label>
<?php } ?>
<br />
<input name="age" value="<?= strip_tags($_POST['age']) ?>"/></p>
<p>
<?php if (array_key_exists('toys', $errors)) { ?>
<label class="error"><b>Toys:</b></label>
<?php } else { ?>
<label><b>Toys:<b></label>
<?php } ?>
<br />
<?php foreach ($toys as $key => $value) { ?>
<label><input type="checkbox" name="toys[]"
<?php if (array_key_exists('toys', $_POST) && in_array($key,
$_POST['toys'])) { echo 'checked="checked" '; } ?>
value="<?= $key ?>" /> <?= value ?></label><br />
<?php } ?>
</p>
<p>
<input type="submit" value="register" /></p>
</form>
</body>
</html>
Line 22 is one with "if (empty($_POST['toys'])) {".
I copied this out of "Sams Teach Yourself Web Publishing with HTML and CSS in One Hour a Day".