Php Error - Unexpected require_once expecting function

匿名 (未验证) 提交于 2019-12-03 09:18:39

问题:

I'm currently trying to fetch the medoo framework so that I can begin easily retrieving data from my MySQL database... and for some reason it doesn't work!

here's the code of my signin.php file

<?php     function loginUserAccount($loginname, $password){     // Include Medoo (configured)     require_once 'medoo.min.php';      // Initialize     $database = new medoo();      $email = $database->get('MM_Users', 'Email', [         'ID' => 1     ]);     return $email;     }     ?> 

And the error:

Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE), expecting function (T_FUNCTION) in /myfiledirectory/example.php on line 4

Any help is greatly appreciated! I'm not quite sure at what's going wrong.

The php file that's running is :

<?php require_once('signin.php'); if(!loginUserAccount('bobby@gmail.com', 'AveryRandomPassword')){      echo 'error'; } else {     echo $email; } ?> 

There's also a difference in the wrapping for require_once... this doesn't make a difference does it? And if so, which one is more advisable to use in a production environment?

回答1:

You can't have require_once inside a class without a function. That's the main reason.

Try putting the require_once in the construct.

to be exact :

class foo  {  require_once('bar.php');  } 

will thro an error.



回答2:

This line:

require_once('signin.php'); 

is inside a class but outside a method, which is not possible in PHP.



回答3:

I might be wrong, but doesn't require_once 'medoo.min.php'; require parentheses? like so: require_once ('medoo.min.php');



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!