如何在数组中使用 if 语句?
How can I use an if statement inside an array?
我尝试在数组中创建一个 if 语句:
$formBuilder->add('getTimestamp', DateType::class, array(
'widget' => 'single_text',
if($target == "new"){
'data' => new \DateTime(),
}
'attr' => array('class' => 'form-control', 'style' => 'line-height: 20px;'), 'label' => $field['fieldName'],
));
但我收到一条错误消息
(1/1) ParseError
syntax error, unexpected 'if' (T_IF), expecting ')'
问题是数组中的 if
。这是无效的 php 语法。你可以把它移到外面。
$options = [
'widget' => 'single_text',
'attr' => array('class' => 'form-control', 'style' => 'line-height: 20px;'), 'label' => $field['fieldName'],
];
if($target == "new"){
$options['data'] = new \DateTime();
}
$formBuilder->add('getTimestamp', DateType::class, $options);
'data' => $target == 'new' ? new \DateTime() : ''
我尝试在数组中创建一个 if 语句:
$formBuilder->add('getTimestamp', DateType::class, array(
'widget' => 'single_text',
if($target == "new"){
'data' => new \DateTime(),
}
'attr' => array('class' => 'form-control', 'style' => 'line-height: 20px;'), 'label' => $field['fieldName'],
));
但我收到一条错误消息
(1/1) ParseError
syntax error, unexpected 'if' (T_IF), expecting ')'
问题是数组中的 if
。这是无效的 php 语法。你可以把它移到外面。
$options = [
'widget' => 'single_text',
'attr' => array('class' => 'form-control', 'style' => 'line-height: 20px;'), 'label' => $field['fieldName'],
];
if($target == "new"){
$options['data'] = new \DateTime();
}
$formBuilder->add('getTimestamp', DateType::class, $options);
'data' => $target == 'new' ? new \DateTime() : ''