I am pretty new to php world. I wrote the following:
<html>
<head>
<title>It joins simple1 and prac1 program together</title>
</head>
<body>
<?php
if($_POST['user'])
{
print "hello,";
print $_POST['user'];
}
else{
print <<<_HTML_
<form method="post" action="$_server[PHP_SELF]">
Your name:<input type="text" name="user">
</br>
<input type="submit" value="hello">
</form>
_HTML_;
}
?>
</body>
</html> ---- line 23
Getting Error message:
Parse error: syntax error, unexpected $end in C:\wamp\www\php_practice\simple2.php on line 23
I have removed all html tags and just kept php tags it worked:
<?php
// Print a greeting if the form was submitted
if ($_POST['user']) {
print "Hello, ";
// Print what was submitted in the form parameter called 'user'
print $_POST['user'];
print "!";
} else {
// Otherwise, print the form
print <<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
Your Name: <input type="text" name="user">
<br/>
<input type="submit" value="Say Hello">
</form>
_HTML_;
}
?>
Output : Giving proper output but with an warning
Notice: Undefined index: user in C:\wamp\www\php_practice\test.php on line 3
-
Why it is not working with the previous case? What is going wrong?
-
How to remove or silent the warning message in the second code. It looks bad in the browser.
The cause of your parse error:
The closing of a
HEREDOCstatement must occur at the beginning of a line with no whitespace before or after. You have your_HTMLindented to the same level as the rest of your code, but it must occur at the very first character position of the line.The cause of your
undefined indexwarning:To test if
$_POST['user']is set, useisset(). That will take care of yourundefined indexerror.Update: The cause of the
undefined variable _servernotice:Inside a HEREDOC or double quoted string, you will need to wrap complex variables (arrays, objects) in
{}. Also, place quotes aroundPHP_SELF.