我是php世界的新手.我写了以下内容:
<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
Run Code Online (Sandbox Code Playgroud)
获取错误消息:
Parse error: syntax error, unexpected $end in C:\wamp\www\php_practice\simple2.php on line 23
Run Code Online (Sandbox Code Playgroud)
我删除了所有的html标签,只保留了它的php标签:
<?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_;
}
?>
Run Code Online (Sandbox Code Playgroud)
输出:给出正确的输出但有警告
Notice: Undefined index: user in C:\wamp\www\php_practice\test.php on line 3
Run Code Online (Sandbox Code Playgroud)
为什么它不适用于以前的案例?出了什么问题?
如何在第二个代码中删除或静默警告消息.它在浏览器中看起来很糟糕.
HEREDOC语句的结束必须发生在行之前或之后没有空格的行的开头.您将_HTML缩进到与其余代码相同的级别,但它必须出现在该行的第一个字符位置.
_HTML_;
// Should be
_HTML_;
Run Code Online (Sandbox Code Playgroud)
undefined index警告的原因:要测试是否$_POST['user']已设置,请使用isset().这将照顾你的undefined index错误.
if(isset($_POST['user']))
Run Code Online (Sandbox Code Playgroud)
undefined variable _server通知原因:在HEREDOC或双引号字符串中,您需要包含复杂变量(数组,对象){}.另外,附上引号PHP_SELF.
<form method="post" action="{$_SERVER['PHP_SELF']}">
Run Code Online (Sandbox Code Playgroud)