PHP 语法错误,我的 Yii2 运行 php 7.2 上的文件意外结束
PHP syntax error, unexpected end of file on my Yii2 run on php 7.2
我很抱歉这个问题又出现了,但是,我真的不知道到底发生了什么..
在我的 PC 中它 运行 正常但是当去托管时显示错误..
<?= $this->render('_form', ['model' => $model]) ?>
上一行显示错误语法错误,php 7.2
中的文件意外结束
这里是完整代码
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model app\models\Mahasiswa */
$this->title = 'Bua Data Mahasiswa';
$this->params['breadcrumbs'][] = ['label' => 'Mahasiswas', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="mahasiswa-create" style="margin-left: 100px;margin-right: 100px;">
<h1><?= Html::encode($this->title) ?></h1>
<hr>
<?= $this->render('_form', ['model' => $model]) ?>
</div>
我真的不知道这里发生了什么..
请大家帮帮我
非常感谢...
编辑:
此处完整跟踪
ParseError
syntax error, unexpected end of file
1. in /home/u7796600/public_html/views/mahasiswa/_form.phpat line 152
143144145146147148149150151 });
JS;
$this->registerJs($script,
yii\web\View::POS_END,
'in-x-handler'
);?>
</div>
2. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 257– yii\base\View::renderPhpFile('/home/u7796600/public_html/views...', ['model' => app\models\Mahasiswa])
3. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 156– yii\base\View::renderFile('/home/u7796600/public_html/views...', ['model' => app\models\Mahasiswa], null)
4. in /home/u7796600/public_html/views/mahasiswa/create.php at line 16– yii\base\View::render('_form', ['model' => app\models\Mahasiswa])
101112131415161718$this->params['breadcrumbs'][] = $this->title;
?>
<div class="mahasiswa-create" style="margin-left: 100px;margin-right: 100px;">
<h1><?= Html::encode($this->title) ?></h1>
<hr>
<?= $this->render('_form', ['model' => $model]) ?>
</div>
5. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 348– require('/home/u7796600/public_html/views...')
6. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 257– yii\base\View::renderPhpFile('/home/u7796600/public_html/views...', ['model' => app\models\Mahasiswa])
7. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 156– yii\base\View::renderFile('/home/u7796600/public_html/views...', ['model' => app\models\Mahasiswa], app\controllers\MahasiswaController)
8. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Controller.php at line 409– yii\base\View::render('create', ['model' => app\models\Mahasiswa], app\controllers\MahasiswaController)
9. in /home/u7796600/public_html/controllers/MahasiswaController.php at line 106– yii\base\Controller::render('create', ['model' => app\models\Mahasiswa])
100101102103104105106107108109110111112 $model->addError('foto', "Foto tidak boleh kosong !");
}
}
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Mahasiswa model.
* If update is successful, the browser will be redirected to the 'view' page.
10. app\controllers\MahasiswaController::actionCreate()
11. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/InlineAction.php at line 57– call_user_func_array([app\controllers\MahasiswaController, 'actionCreate'], [])
12. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Controller.php at line 180– yii\base\InlineAction::runWithParams([])
13. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Module.php at line 528– yii\base\Controller::runAction('create', [])
14. in /home/u7796600/public_html/vendor/yiisoft/yii2/web/Application.php at line 103– yii\base\Module::runAction('mahasiswa/create', [])
15. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Application.php at line 386– yii\web\Application::handleRequest(yii\web\Request)
16. in /home/u7796600/public_html/index.php at line 12– yii\base\Application::run()
6789101112
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/config/web.php';
(new yii\web\Application($config))->run();
$_COOKIE = [
'_csrf' => '88604fb700879268611516c1ee39de9cdab11ea7f454885b6bf3328fb9d26e97a:2:{i:0;s:5:"_csrf";i:1;s:32:"bNGjdI0AfMr2cromPF1xm1f7RthBqbIf";}',
'_identity-admin-simpresensi' => '["admin","jKDGomSH1sCM3KRg2zooF97hI6MbsNHN",2592000]',
'PHPSESSID' => '021088dcd0c061b02965e21ab8d87fcb',
];
$_SESSION = [
'__flash' => [],
'admin' => 'admin',
];
我的_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Prodi;
use kartik\date\DatePicker;
use kartik\file\FileInput;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $model app\models\Mahasiswa */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="mahasiswa-form">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ; ?>
<?php
$img = empty($model->foto) ? '':\Yii::$app->request->BaseUrl.'/images/' . $model->gambar;
echo FileInput::widget([
'model' => $model,
'attribute' => 'foto',
'options'=>[
'accept'=>'image/*',
'multiple'=>false
],
'pluginOptions' => [
'initialPreview'=>empty($model->foto) ? '':'<img width="158" src="'.$img.'" class="file-preview-image">',
'initialCaption'=>"Foto",
'initialPreviewShowDelete' => false,
'overwriteInitial' => true,
'showPreview' => true,
'showRemove' => false,
'showUpload' => false,
'maxFileSize'=>2800
],
'pluginEvents' => [
'fileuploaded'=>"function(event, data, previewId, index){
console.log(data);
}",
]
]);
?>
<br>
<?= $form->field($model, 'nim')->textInput() ?>
<?=$form->field($model, 'id_prodi')
->dropDownList(
ArrayHelper::map(Prodi::find()->all(), 'id', 'nama'),
['prompt' => 'Pilih Prodi']
)?>
<?=$form->field($model, 'tahun_masuk')
->dropDownList(
[
'2010' => '2010',
'2011' => '2011',
'2012' => '2012',
'2013' => '2013',
'2014' => '2014',
'2015' => '2015',
'2016' => '2016',
'2017' => '2017',
'2018' => '2018',
'2019' => '2019',
'2020' => '2020',
'2021' => '2021',
],
['prompt' => 'Pilih Tahun Masuk']
)?>
<?= $form->field($model, 'nama')->textInput(['maxlength' => true]) ?>
<?=$form->field($model, 'jenis_kelamin')
->dropDownList(
[
'Laki-laki' => 'Laki-laki',
'Perempuan' => 'Perempuan'
],
['prompt' => 'Pilih Jenis Kelamin']
)?>
<?= $form->field($model, 'tempat_lahir')->textArea(['maxlength' => true]) ?>
<?= $form->field($model, 'tanggal_lahir')->widget(DatePicker::classname(), [
'options' => ['placeholder' => 'Pilih Tanggal Lahir ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'dd-MM-yyyy',
'autoclose'=>true,
]
]) ?>
<?=$form->field($model, 'agama')
->dropDownList(
[
'Islam' => 'Islam',
'Kristen' => 'Kristen',
'Katolik' => 'Katolik',
'Hindu' => 'Hindu',
'Budha' => 'Budha',
'Konghuchu' => 'Konghuchu',
'Kepercayaan' => 'Kepercayaan',
],
['prompt' => 'Pilih Agama']
)?>
<?= $form->field($model, 'alamat_rumah')->textArea(['maxlength' => true]) ?>
<?= $form->field($model, 'domisili')->textArea(['maxlength' => true]) ?>
<?=$form->field($model, 'akses')
->dropDownList(
[
0 => 'Tidak',
1 => 'Ya',
],
['prompt' => 'Pilih Akses Aplikasi'],
)?>
<div id='psd'>
<?= $form->field($model, 'password')->passwordInput(['maxlength' => true]) ?>
</div>
<div class="form-group">
<?= Html::submitButton('Simpan', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
<?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'
);?>
</div>
您在 _form.php
中的 heredoc 声明是错误的。 <<<
和开头的标识符之间不应有 space。 heredoc 结尾的行只能包含标识符和分号,这意味着 heredoc 末尾的标识符 不能缩进 .
因为您缩进了标识符,所以解析器无法将其识别为 heredoc 声明的结尾,并将文件的其余部分解析为字符串。然后当它结束时它抱怨它没有在文件结束之前找到 heredoc 的结尾。
_form.php
中的 heredoc 声明应如下所示:
<?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'
);?>
您可以找到一些 heredoc 声明的示例 in php documentation。
如果您在 windows 机器上开发脚本而不是将其上传到 linux/unix 服务器,则可能导致问题的另一件事是行尾字符。 heredoc 的结尾必须前后跟有当前系统正在使用的行尾字符。 Windows 使用 \r\n
作为行尾,但 linux/unix 系统仅使用 \n
字符。
因此,如果您在 windows 机器上开发了脚本,那么您的 heredocs 的结尾如下所示:
JS;
但在 linux/unix 系统中只有 \n
被视为换行,而 \r
留在字符串中。这打破了在 heredoc 末尾只能出现标识符和分号的规则。
\r
JS;\r
如果您通过 FTP 将脚本上传到服务器,请确保对 php 文件使用 text
传输。 text
传输将为您转换行尾。
如果你正在使用像 git 这样的版本控制系统,应该有一些设置允许你在你的开发机器上提交或拉到你的服务器时转换行尾。
或者您可以将您的编辑器设置为使用 unix 风格的行尾保存文件。
注意:
heredoc 的此要求在 php 7.2.x 之前有效。正如 rob006 在评论中指出的那样,php 7.3 带来了更灵活的 heredoc 语法,允许缩进并且不需要 ;
和结束标识符后的换行符。但缩进仍有其特殊含义,不能超过heredoc字符串中任何一行的标识。 More details about heredoc/nowdoc changes.
我很抱歉这个问题又出现了,但是,我真的不知道到底发生了什么..
在我的 PC 中它 运行 正常但是当去托管时显示错误..
<?= $this->render('_form', ['model' => $model]) ?>
上一行显示错误语法错误,php 7.2
中的文件意外结束这里是完整代码
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model app\models\Mahasiswa */
$this->title = 'Bua Data Mahasiswa';
$this->params['breadcrumbs'][] = ['label' => 'Mahasiswas', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="mahasiswa-create" style="margin-left: 100px;margin-right: 100px;">
<h1><?= Html::encode($this->title) ?></h1>
<hr>
<?= $this->render('_form', ['model' => $model]) ?>
</div>
我真的不知道这里发生了什么..
请大家帮帮我
非常感谢...
编辑:
此处完整跟踪
ParseError
syntax error, unexpected end of file
1. in /home/u7796600/public_html/views/mahasiswa/_form.phpat line 152
143144145146147148149150151 });
JS;
$this->registerJs($script,
yii\web\View::POS_END,
'in-x-handler'
);?>
</div>
2. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 257– yii\base\View::renderPhpFile('/home/u7796600/public_html/views...', ['model' => app\models\Mahasiswa])
3. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 156– yii\base\View::renderFile('/home/u7796600/public_html/views...', ['model' => app\models\Mahasiswa], null)
4. in /home/u7796600/public_html/views/mahasiswa/create.php at line 16– yii\base\View::render('_form', ['model' => app\models\Mahasiswa])
101112131415161718$this->params['breadcrumbs'][] = $this->title;
?>
<div class="mahasiswa-create" style="margin-left: 100px;margin-right: 100px;">
<h1><?= Html::encode($this->title) ?></h1>
<hr>
<?= $this->render('_form', ['model' => $model]) ?>
</div>
5. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 348– require('/home/u7796600/public_html/views...')
6. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 257– yii\base\View::renderPhpFile('/home/u7796600/public_html/views...', ['model' => app\models\Mahasiswa])
7. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 156– yii\base\View::renderFile('/home/u7796600/public_html/views...', ['model' => app\models\Mahasiswa], app\controllers\MahasiswaController)
8. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Controller.php at line 409– yii\base\View::render('create', ['model' => app\models\Mahasiswa], app\controllers\MahasiswaController)
9. in /home/u7796600/public_html/controllers/MahasiswaController.php at line 106– yii\base\Controller::render('create', ['model' => app\models\Mahasiswa])
100101102103104105106107108109110111112 $model->addError('foto', "Foto tidak boleh kosong !");
}
}
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Mahasiswa model.
* If update is successful, the browser will be redirected to the 'view' page.
10. app\controllers\MahasiswaController::actionCreate()
11. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/InlineAction.php at line 57– call_user_func_array([app\controllers\MahasiswaController, 'actionCreate'], [])
12. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Controller.php at line 180– yii\base\InlineAction::runWithParams([])
13. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Module.php at line 528– yii\base\Controller::runAction('create', [])
14. in /home/u7796600/public_html/vendor/yiisoft/yii2/web/Application.php at line 103– yii\base\Module::runAction('mahasiswa/create', [])
15. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Application.php at line 386– yii\web\Application::handleRequest(yii\web\Request)
16. in /home/u7796600/public_html/index.php at line 12– yii\base\Application::run()
6789101112
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/config/web.php';
(new yii\web\Application($config))->run();
$_COOKIE = [
'_csrf' => '88604fb700879268611516c1ee39de9cdab11ea7f454885b6bf3328fb9d26e97a:2:{i:0;s:5:"_csrf";i:1;s:32:"bNGjdI0AfMr2cromPF1xm1f7RthBqbIf";}',
'_identity-admin-simpresensi' => '["admin","jKDGomSH1sCM3KRg2zooF97hI6MbsNHN",2592000]',
'PHPSESSID' => '021088dcd0c061b02965e21ab8d87fcb',
];
$_SESSION = [
'__flash' => [],
'admin' => 'admin',
];
我的_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Prodi;
use kartik\date\DatePicker;
use kartik\file\FileInput;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $model app\models\Mahasiswa */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="mahasiswa-form">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ; ?>
<?php
$img = empty($model->foto) ? '':\Yii::$app->request->BaseUrl.'/images/' . $model->gambar;
echo FileInput::widget([
'model' => $model,
'attribute' => 'foto',
'options'=>[
'accept'=>'image/*',
'multiple'=>false
],
'pluginOptions' => [
'initialPreview'=>empty($model->foto) ? '':'<img width="158" src="'.$img.'" class="file-preview-image">',
'initialCaption'=>"Foto",
'initialPreviewShowDelete' => false,
'overwriteInitial' => true,
'showPreview' => true,
'showRemove' => false,
'showUpload' => false,
'maxFileSize'=>2800
],
'pluginEvents' => [
'fileuploaded'=>"function(event, data, previewId, index){
console.log(data);
}",
]
]);
?>
<br>
<?= $form->field($model, 'nim')->textInput() ?>
<?=$form->field($model, 'id_prodi')
->dropDownList(
ArrayHelper::map(Prodi::find()->all(), 'id', 'nama'),
['prompt' => 'Pilih Prodi']
)?>
<?=$form->field($model, 'tahun_masuk')
->dropDownList(
[
'2010' => '2010',
'2011' => '2011',
'2012' => '2012',
'2013' => '2013',
'2014' => '2014',
'2015' => '2015',
'2016' => '2016',
'2017' => '2017',
'2018' => '2018',
'2019' => '2019',
'2020' => '2020',
'2021' => '2021',
],
['prompt' => 'Pilih Tahun Masuk']
)?>
<?= $form->field($model, 'nama')->textInput(['maxlength' => true]) ?>
<?=$form->field($model, 'jenis_kelamin')
->dropDownList(
[
'Laki-laki' => 'Laki-laki',
'Perempuan' => 'Perempuan'
],
['prompt' => 'Pilih Jenis Kelamin']
)?>
<?= $form->field($model, 'tempat_lahir')->textArea(['maxlength' => true]) ?>
<?= $form->field($model, 'tanggal_lahir')->widget(DatePicker::classname(), [
'options' => ['placeholder' => 'Pilih Tanggal Lahir ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'dd-MM-yyyy',
'autoclose'=>true,
]
]) ?>
<?=$form->field($model, 'agama')
->dropDownList(
[
'Islam' => 'Islam',
'Kristen' => 'Kristen',
'Katolik' => 'Katolik',
'Hindu' => 'Hindu',
'Budha' => 'Budha',
'Konghuchu' => 'Konghuchu',
'Kepercayaan' => 'Kepercayaan',
],
['prompt' => 'Pilih Agama']
)?>
<?= $form->field($model, 'alamat_rumah')->textArea(['maxlength' => true]) ?>
<?= $form->field($model, 'domisili')->textArea(['maxlength' => true]) ?>
<?=$form->field($model, 'akses')
->dropDownList(
[
0 => 'Tidak',
1 => 'Ya',
],
['prompt' => 'Pilih Akses Aplikasi'],
)?>
<div id='psd'>
<?= $form->field($model, 'password')->passwordInput(['maxlength' => true]) ?>
</div>
<div class="form-group">
<?= Html::submitButton('Simpan', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
<?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'
);?>
</div>
您在 _form.php
中的 heredoc 声明是错误的。 <<<
和开头的标识符之间不应有 space。 heredoc 结尾的行只能包含标识符和分号,这意味着 heredoc 末尾的标识符 不能缩进 .
因为您缩进了标识符,所以解析器无法将其识别为 heredoc 声明的结尾,并将文件的其余部分解析为字符串。然后当它结束时它抱怨它没有在文件结束之前找到 heredoc 的结尾。
_form.php
中的 heredoc 声明应如下所示:
<?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'
);?>
您可以找到一些 heredoc 声明的示例 in php documentation。
如果您在 windows 机器上开发脚本而不是将其上传到 linux/unix 服务器,则可能导致问题的另一件事是行尾字符。 heredoc 的结尾必须前后跟有当前系统正在使用的行尾字符。 Windows 使用 \r\n
作为行尾,但 linux/unix 系统仅使用 \n
字符。
因此,如果您在 windows 机器上开发了脚本,那么您的 heredocs 的结尾如下所示:
JS;
但在 linux/unix 系统中只有 \n
被视为换行,而 \r
留在字符串中。这打破了在 heredoc 末尾只能出现标识符和分号的规则。
\r
JS;\r
如果您通过 FTP 将脚本上传到服务器,请确保对 php 文件使用 text
传输。 text
传输将为您转换行尾。
如果你正在使用像 git 这样的版本控制系统,应该有一些设置允许你在你的开发机器上提交或拉到你的服务器时转换行尾。
或者您可以将您的编辑器设置为使用 unix 风格的行尾保存文件。
注意:
heredoc 的此要求在 php 7.2.x 之前有效。正如 rob006 在评论中指出的那样,php 7.3 带来了更灵活的 heredoc 语法,允许缩进并且不需要 ;
和结束标识符后的换行符。但缩进仍有其特殊含义,不能超过heredoc字符串中任何一行的标识。 More details about heredoc/nowdoc changes.