PHP语法错误,在我的Yii2上运行php7.2时出现意外的文件结尾



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

在我的电脑中,它运行正常,但当要托管时显示错误。。

<?= $this->render('_form', ['model' => $model]) ?>

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

这里是完整的代码

<?php
use yiihelpersHtml;
/* @var $this yiiwebView */
/* @var $model appmodelsMahasiswa */
$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,
yiiwebView::POS_END,
'in-x-handler'
);?>
</div>
2. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 257– yiibaseView::renderPhpFile('/home/u7796600/public_html/views...', ['model' => appmodelsMahasiswa])
3. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 156– yiibaseView::renderFile('/home/u7796600/public_html/views...', ['model' => appmodelsMahasiswa], null)
4. in /home/u7796600/public_html/views/mahasiswa/create.php at line 16– yiibaseView::render('_form', ['model' => appmodelsMahasiswa])
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– yiibaseView::renderPhpFile('/home/u7796600/public_html/views...', ['model' => appmodelsMahasiswa])
7. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/View.php at line 156– yiibaseView::renderFile('/home/u7796600/public_html/views...', ['model' => appmodelsMahasiswa], appcontrollersMahasiswaController)
8. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Controller.php at line 409– yiibaseView::render('create', ['model' => appmodelsMahasiswa], appcontrollersMahasiswaController)
9. in /home/u7796600/public_html/controllers/MahasiswaController.php at line 106– yiibaseController::render('create', ['model' => appmodelsMahasiswa])
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. appcontrollersMahasiswaController::actionCreate()
11. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/InlineAction.php at line 57– call_user_func_array([appcontrollersMahasiswaController, 'actionCreate'], [])
12. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Controller.php at line 180– yiibaseInlineAction::runWithParams([])
13. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Module.php at line 528– yiibaseController::runAction('create', [])
14. in /home/u7796600/public_html/vendor/yiisoft/yii2/web/Application.php at line 103– yiibaseModule::runAction('mahasiswa/create', [])
15. in /home/u7796600/public_html/vendor/yiisoft/yii2/base/Application.php at line 386– yiiwebApplication::handleRequest(yiiwebRequest)
16. in /home/u7796600/public_html/index.php at line 12– yiibaseApplication::run()
6789101112 
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/config/web.php';

(new yiiwebApplication($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 yiihelpersHtml;
use yiiwidgetsActiveForm;
use yiihelpersArrayHelper;
use appmodelsProdi;
use kartikdateDatePicker;
use kartikfileFileInput;
use yiihelpersUrl;
/* @var $this yiiwebView */
/* @var $model appmodelsMahasiswa */
/* @var $form yiiwidgetsActiveForm */
?>
<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,
yiiwebView::POS_END,
'in-x-handler'
);?>
</div>

您在_form.php中的heredoc声明是错误的。<<<和开头的标识符之间不应该有空格。用于结束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,
yiiwebView::POS_END,
'in-x-handler'
);?>

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

如果您在windows机器上开发脚本,而不是将其上传到linux/unix服务器,那么另一件可能会导致问题的事情是行末尾的字符。heredoc的末尾必须在前面和后面加上当前系统正在使用的行尾字符。Windows使用rn作为行的末尾,但linux/unix系统仅使用n字符。因此,如果你已经在windows机器上开发了脚本,那么你的heredocs的结尾看起来像:

JS;

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

r
JS;r

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

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

最新更新