Reputation: 401
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.
Upvotes: 1
Views: 272
Reputation: 270599
The closing of a HEREDOC
statement must occur at the beginning of a line with no whitespace before or after. You have your _HTML
indented to the same level as the rest of your code, but it must occur at the very first character position of the line.
_HTML_;
// Should be
_HTML_;
undefined index
warning:To test if $_POST['user']
is set, use isset()
. That will take care of your undefined index
error.
if(isset($_POST['user']))
undefined variable _server
notice:Inside a HEREDOC or double quoted string, you will need to wrap complex variables (arrays, objects) in {}
. Also, place quotes around PHP_SELF
.
<form method="post" action="{$_SERVER['PHP_SELF']}">
Upvotes: 7
Reputation: 122
You can suppress errors in PHP with an @ before the function name (won't work in this case) or by setting error_reporting to a diffrent value (http://www.php.net/manual/en/function.error-reporting.php).
but you should fix the source of the warning instead of suppressing it. In this case there are whitespaces before your HTML;
Upvotes: 0