php - Laravel 语法错误,意外 'and' (T_LOGICAL_AND),期望 ')'

标签 php laravel-4

我当前的项目使用 Laravel4。我的 Web 应用程序中有一个搜索功能。这是我的搜索功能代码,位于我的一个controller.php 文件中。

public function postBloodSearch(){

        $blood_group = Input::get('blood_group');
        $location = Input::get('location');

        if(empty($blood_group and $location)){

            return Redirect::route('donors')
                    ->with('global','You have to enter both Blood Group and Location');
        }

        $result = Donor::where('blood_group','LIKE','%'.$blood_group.'%')->where('location','LIKE','%'.$location.'%')->get();
        return View::make('bloodDonation.blood_array')->with('result',$result);


}

你可以看到,在我的代码中,我使用了内置于 empty 函数中的 php,而实际上我面临的是 出现以下错误

syntax error, unexpected 'and' (T_LOGICAL_AND), expecting ')'

我尝试了以下代码来解决这个问题(只需将 and 替换为 && )

if(empty($blood_group && $location)){.........}

现在抛出以下错误

syntax error, unexpected '&&' (T_LOGICAL_AND), expecting ')'

更新:相同的代码在我的本地服务器(Xampp 服务器)中运行得非常好。但是在我的实时服务器(我使用 Cpanel)中,它抛出了这些错误。

最佳答案

而不是

if(empty($blood_group and $location)){

尝试

if(empty($blood_group) and empty($location)){

在上面的调用中,您将 bool 值传递给 empty function 。错误原因提到on php docs作为

Note: Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

关于php - Laravel 语法错误,意外 'and' (T_LOGICAL_AND),期望 ')',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26081826/

相关文章:

php - 关系中的 Laravel 命名空间让我感到困惑

php - 拉维尔 4 : No hint path defined

laravel - 我可以在 Laravel 4 eloquent 中使用驼峰命名法吗?

php - Apache Web 服务器配置给出 404 错误消息

php - 根据每个 id 的自定义公式,仅使用 1 个输入字段更改 MySQL 价格?

php - 数据表警告 : table id={id} - Invalid JSON response

php - 如何列出一棵树的所有部分树

php - Laravel 身份验证抛出奇怪的错误

javascript - JQuery 变量没有转移到 JavaScript

php - (2/2) QueryException SQLSTATE[HY000] [1049] 未知数据库 'homestead' (SQL : select count(*) as aggregate from `users` where `email` =