php 解析错误:语法错误:意外的“{”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7059753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:54:57  来源:igfitidea点击:

Parse Error: syntax error: unexpected '{'

phpsyntax-errorparse-error

提问by snarkyt123

I have this code that processes a user then redirects them to the user homepage.

我有这个处理用户的代码,然后将他们重定向到用户主页。

<?php
    $username = $_POST['username'];
    $password = $_POST['pwd'];

    $file = file_get_contents("userdb.html");
    if(!strpos($file, $username)) {
        echo "Your username was not found in our database. Please go back and try again.";
    } else {
        echo "Redirecting...";
        if (md5($password) == !strpos($file, (md5($password))) {
             echo "Redirecting..."
             header ('Location: ./userhome.php')
        } else {
             print "Whoops! Your password seems to be incorrect. Go back and try again."
        }
    }
?>

And I get the error:

我得到错误:

Parse error: syntax error, unexpected '{' in userprocess.php on line 11

Could someone tell me the problem please? I think it may be the if inside of if statement, but what can I do for an alternative? Thanks.

有人可以告诉我问题吗?我认为这可能是 if 语句中的 if ,但我能做些什么呢?谢谢。

回答by Spudley

Firstly, this line is missing a closing bracket:

首先,这一行缺少一个右括号:

if (md5($password) == !strpos($file, (md5($password))) {

Count the number of (and )-- they need to match.

计算()- 他们需要匹配的数量。

When you fix this, you'll still get errors, because PHP statements need to end with semi-colons.

当您修复此问题时,您仍然会遇到错误,因为 PHP 语句需要以分号结尾。

All of the following lines are missing their semi-colon:

以下所有行都缺少分号:

echo "Redirecting..."
header ('Location: ./userhome.php')
print "Whoops! Your password seems to be incorrect. Go back and try again."

You need to fix them all before you'll be able to run the program without syntax errors.

您需要将它们全部修复,然后才能在没有语法错误的情况下运行程序。

Hope that helps.

希望有帮助。

回答by Shef

Change

改变

if (md5($password) == !strpos($file, (md5($password)))

to

if (md5($password) == !strpos($file, md5($password)))

回答by Marcelo

You are missing a right parenthesis in the line:

您在该行中缺少右括号:

if (md5($password) == !strpos($file, (md5($password))) {

回答by Ernestas Stankevi?ius

<?php
    $username = $_POST['username'];
    $password = $_POST['pwd'];

    $file = file_get_contents("userdb.html");
    if(!strpos($file, $username)) {
        echo "Your username was not found in our database. Please go back and try again.";
    } else {
        echo "Redirecting...";
        if (md5($password) == !strpos($file, md5($password))) {
             echo "Redirecting...";
             header ('Location: ./userhome.php');
        } else {
             print "Whoops! Your password seems to be incorrect. Go back and try again.";
        }
    }
?>