PHP 语法错误,在 php 7.2 上运行的 Yii2 上出现意外的文件结尾

Hal*_*laz 1 php yii2

我很抱歉这个问题再次出现,但是,我真的不知道到底发生了什么。

\n

在我的电脑上它运行正常,但是当进入托管时显示错误..

\n
<?= $this->render('_form', ['model' => $model]) ?>\n
Run Code Online (Sandbox Code Playgroud)\n

上面的行显示错误语法错误,php 7.2 中文件意外结束

\n

这里是完整的代码

\n
<?php\n\nuse yii\\helpers\\Html;\n\n/* @var $this yii\\web\\View */\n/* @var $model app\\models\\Mahasiswa */\n\n$this->title = 'Bua Data Mahasiswa';\n$this->params['breadcrumbs'][] = ['label' => 'Mahasiswas', 'url' => ['index']];\n$this->params['breadcrumbs'][] = $this->title;\n?>\n<div class="mahasiswa-create" style="margin-left: 100px;margin-right: 100px;">\n\n    <h1><?= Html::encode($this->title) ?></h1>\n    <hr>\n    <?= $this->render('_form', ['model' => $model]) ?>\n\n</div>\n
Run Code Online (Sandbox Code Playgroud)\n

我真的不知道这里发生了什么..

\n

请你们帮助我

\n

太感谢了...

\n

编辑 :

\n

这里是完整的踪迹

\n
ParseError\nsyntax error, unexpected end of file\n1. in /home/u7796600/public_html/views/mahasiswa/_form.phpat line 152\n143144145146147148149150151    });\n \n    JS;\n \n    $this->registerJs($script,\n        yii\\web\\View::POS_END,\n        'in-x-handler'\n    );?>\n</div>\n2. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 257\xe2\x80\x93 yii\\base\\View::renderPhpFile('/home/u7796600/public_html/views...', ['model' => app\\models\\Mahasiswa])\n3. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 156\xe2\x80\x93 yii\\base\\View::renderFile('/home/u7796600/public_html/views...', ['model' => app\\models\\Mahasiswa], null)\n4. in /home/u7796600/public_html/views/mahasiswa/create.php at line 16\xe2\x80\x93 yii\\base\\View::render('_form', ['model' => app\\models\\Mahasiswa])\n101112131415161718$this->params['breadcrumbs'][] = $this->title;\n?>\n<div class="mahasiswa-create" style="margin-left: 100px;margin-right: 100px;">\n \n    <h1><?= Html::encode($this->title) ?></h1>\n    <hr>\n    <?= $this->render('_form', ['model' => $model]) ?>\n \n</div>\n5. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 348\xe2\x80\x93 require('/home/u7796600/public_html/views...')\n6. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 257\xe2\x80\x93 yii\\base\\View::renderPhpFile('/home/u7796600/public_html/views...', ['model' => app\\models\\Mahasiswa])\n7. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 156\xe2\x80\x93 yii\\base\\View::renderFile('/home/u7796600/public_html/views...', ['model' => app\\models\\Mahasiswa], app\\controllers\\MahasiswaController)\n8. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Controller.php at line 409\xe2\x80\x93 yii\\base\\View::render('create', ['model' => app\\models\\Mahasiswa], app\\controllers\\MahasiswaController)\n9. in /home/u7796600/public_html/controllers/MahasiswaController.php at line 106\xe2\x80\x93 yii\\base\\Controller::render('create', ['model' => app\\models\\Mahasiswa])\n100101102103104105106107108109110111112                    $model->addError('foto', "Foto tidak boleh kosong !");\n                }\n            }\n        }\n \n        return $this->render('create', [\n            'model' => $model,\n        ]);\n    }\n \n    /**\n     * Updates an existing Mahasiswa model.\n     * If update is successful, the browser will be redirected to the 'view' page.\n10. app\\controllers\\MahasiswaController::actionCreate()\n11. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/InlineAction.php at line 57\xe2\x80\x93 call_user_func_array([app\\controllers\\MahasiswaController, 'actionCreate'], [])\n12. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Controller.php at line 180\xe2\x80\x93 yii\\base\\InlineAction::runWithParams([])\n13. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Module.php at line 528\xe2\x80\x93 yii\\base\\Controller::runAction('create', [])\n14. in /home/u7796600/public_html/vendor/yiisoft/yii2/web/Application.php at line 103\xe2\x80\x93 yii\\base\\Module::runAction('mahasiswa/create', [])\n15. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Application.php at line 386\xe2\x80\x93 yii\\web\\Application::handleRequest(yii\\web\\Request)\n16. in /home/u7796600/public_html/index.php at line 12\xe2\x80\x93 yii\\base\\Application::run()\n6789101112 \nrequire __DIR__ . '/vendor/autoload.php';\nrequire __DIR__ . '/vendor/yiisoft/yii2/Yii.php';\n \n$config = require __DIR__ . '/config/web.php';\n \n(new yii\\web\\Application($config))->run();\n$_COOKIE = [\n    '_csrf' => '88604fb700879268611516c1ee39de9cdab11ea7f454885b6bf3328fb9d26e97a:2:{i:0;s:5:"_csrf";i:1;s:32:"bNGjdI0AfMr2cromPF1xm1f7RthBqbIf";}',\n    '_identity-admin-simpresensi' => '["admin","jKDGomSH1sCM3KRg2zooF97hI6MbsNHN",2592000]',\n    'PHPSESSID' => '021088dcd0c061b02965e21ab8d87fcb',\n];\n\n$_SESSION = [\n    '__flash' => [],\n    'admin' => 'admin',\n];\n
Run Code Online (Sandbox Code Playgroud)\n

我的_form.php

\n
<?php\n\nuse yii\\helpers\\Html;\nuse yii\\widgets\\ActiveForm;\nuse yii\\helpers\\ArrayHelper;\nuse app\\models\\Prodi;\nuse kartik\\date\\DatePicker;\nuse kartik\\file\\FileInput;\nuse yii\\helpers\\Url;\n\n/* @var $this yii\\web\\View */\n/* @var $model app\\models\\Mahasiswa */\n/* @var $form yii\\widgets\\ActiveForm */\n?>\n\n<div class="mahasiswa-form">\n\n    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ; ?>\n    <?php \n    $img = empty($model->foto) ? '':\\Yii::$app->request->BaseUrl.'/images/' . $model->gambar;\n    echo FileInput::widget([\n        'model' => $model,\n        'attribute' => 'foto',\n        'options'=>[\n            'accept'=>'image/*',\n            'multiple'=>false\n        ],\n        'pluginOptions' => [\n            'initialPreview'=>empty($model->foto) ? '':'<img width="158" src="'.$img.'" class="file-preview-image">',\n            'initialCaption'=>"Foto",  \n            'initialPreviewShowDelete' => false,\n            'overwriteInitial' => true,\n            'showPreview' => true,\n            'showRemove' => false,\n            'showUpload' => false,\n            'maxFileSize'=>2800\n        ],\n        'pluginEvents' => [\n            'fileuploaded'=>"function(event, data, previewId, index){\n                console.log(data);\n            }",\n        ]\n    ]);    \n    ?>\n    <br>\n    <?= $form->field($model, 'nim')->textInput() ?>\n    \n    <?=$form->field($model, 'id_prodi')\n        ->dropDownList(\n        ArrayHelper::map(Prodi::find()->all(), 'id', 'nama'),\n        ['prompt' => 'Pilih Prodi']\n    )?>\n\n\n    <?=$form->field($model, 'tahun_masuk')\n        ->dropDownList(\n        [\n            '2010' => '2010',\n            '2011' => '2011',\n            '2012' => '2012',\n            '2013' => '2013',\n            '2014' => '2014',\n            '2015' => '2015',\n            '2016' => '2016',\n            '2017' => '2017',\n            '2018' => '2018',\n            '2019' => '2019',\n            '2020' => '2020',\n            '2021' => '2021',\n        ],\n        ['prompt' => 'Pilih Tahun Masuk']\n    )?>\n\n    <?= $form->field($model, 'nama')->textInput(['maxlength' => true]) ?>\n\n    <?=$form->field($model, 'jenis_kelamin')\n        ->dropDownList(\n        [\n            'Laki-laki' => 'Laki-laki',\n            'Perempuan' => 'Perempuan'\n        ],\n        ['prompt' => 'Pilih Jenis Kelamin']\n    )?>\n\n    <?= $form->field($model, 'tempat_lahir')->textArea(['maxlength' => true]) ?>\n\n    <?= $form->field($model, 'tanggal_lahir')->widget(DatePicker::classname(), [\n        'options' => ['placeholder' => 'Pilih Tanggal Lahir ...'],\n        'convertFormat' => true,\n        'pluginOptions' => [\n            'format' => 'dd-MM-yyyy',\n            'autoclose'=>true,\n        ]\n    ]) ?>\n\n    <?=$form->field($model, 'agama')\n        ->dropDownList(\n            [\n                'Islam' => 'Islam',\n                'Kristen' => 'Kristen',\n                'Katolik' => 'Katolik',\n                'Hindu' => 'Hindu',\n                'Budha' => 'Budha',\n                'Konghuchu' => 'Konghuchu',\n                'Kepercayaan' => 'Kepercayaan',\n            ],\n        ['prompt' => 'Pilih Agama']\n    )?>\n\n    <?= $form->field($model, 'alamat_rumah')->textArea(['maxlength' => true]) ?>\n\n    <?= $form->field($model, 'domisili')->textArea(['maxlength' => true]) ?>\n\n    <?=$form->field($model, 'akses')\n        ->dropDownList(\n            [\n                0 => 'Tidak',\n                1 => 'Ya',\n            ],\n            ['prompt' => 'Pilih Akses Aplikasi'],\n    )?>\n    <div id='psd'>            \n        <?= $form->field($model, 'password')->passwordInput(['maxlength' => true]) ?>\n    </div>\n\n\n    <div class="form-group">\n        <?= Html::submitButton('Simpan', ['class' => 'btn btn-success']) ?>\n    </div>\n\n    <?php ActiveForm::end(); ?>\n\n    <?php\n    $script = <<< JS\n\n    $('#mahasiswa-akses').on('change', function(){\n        var val = $(this).val();\n        document.getElementById("psd").style.display= val == 1 ? 'block' : 'none';\n    });\n\n    $(function(){        \n        console.log(document.getElementById('mahasiswa-akses').value);\n        document.getElementById("psd").style.display= document.getElementById("mahasiswa-akses").value == 1 ? 'block' : 'none';\n    });\n\n    JS;\n\n    $this->registerJs($script,\n        yii\\web\\View::POS_END,\n        'in-x-handler'\n    );?>\n</div>\n
Run Code Online (Sandbox Code Playgroud)\n

Mic*_*ica 5

你的heredoc声明_form.php是错误的。<<<开头的 和 标识符之间不应有空格。结束heredoc的行只能包含标识符和分号,这意味着heredoc末尾的标识符不能缩进

因为您有缩进的标识符,所以解析器不会将其识别为heredoc声明的结尾,并将文件的其余部分解析为字符串。然后,当它结束时,它会抱怨在文件结束之前没有找到heredoc的结尾。

您的 Heredoc 声明_form.php应如下所示:

    <?php
    $script = <<<JS
    $('#mahasiswa-akses').on('change', function(){
        var val = $(this).val();
        document.getElementById("psd").style.display= val == 1 ? 'block' : 'none';
    });

    $(function(){        
        console.log(document.getElementById('mahasiswa-akses').value);
        document.getElementById("psd").style.display= document.getElementById("mahasiswa-akses").value == 1 ? 'block' : 'none';
    });
JS;

    $this->registerJs($script,
        yii\web\View::POS_END,
        'in-x-handler'
    );?>
Run Code Online (Sandbox Code Playgroud)

您可以在 php 文档中找到一些 Heredoc 声明的示例。

如果您在 Windows 计算机上开发脚本而不是将其上传到 linux/unix 服务器,则另一件事可能会导致问题是行尾字符。Heredoc 的结尾必须位于当前系统正在使用的行结束符之前和之后。Windows 使用\r\n作为行尾,但 linux/unix 系统仅使用\n字符。因此,如果您在 Windows 计算机上开发了脚本,则此处文档的末尾如下所示:

JS;
Run Code Online (Sandbox Code Playgroud)

但在 linux/unix 系统中仅\n被视为换行并\r保留在字符串中。这打破了heredoc末尾只能出现标识符和分号的规则。

\r
JS;\r
Run Code Online (Sandbox Code Playgroud)

如果您通过 FTP 将脚本上传到服务器,请确保text对 php 文件使用传输。传输text将为您转换行尾。如果您使用像 git 这样的版本控制系统,那么应该有一些设置允许您在开发计算机上提交或在服务器上拉取时转换行尾。或者您可以将编辑器设置为使用 Unix 样式行尾保存文件。

注意:heredoc 的此要求在 php 7.2.x 之前有效。正如rob006在评论中指出的那样,php 7.3 带来了更灵活的heredoc 语法,允许缩进并且不需要;在结束标识符之后添加换行符。但缩进仍然有其特殊含义,并且不能比定界符字符串中任何行的缩进长。有关heredoc/nowdoc 更改的更多详细信息

  • *heredoc 末尾的标识符不得缩进* - 自 PHP 7.3 以来已更改:https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes (2认同)