访问HTML中的php数组元素

问题描述 投票:0回答:2

我在php中有这个:

'position' => [
			'label' => ['Pic Position', ''],
			'inputType' => 'select',
			#'default' => 'left',
			'options' => [
				'left' => 'Left',
				'right' => 'Right',
			],
		]

现在我想要选择'Left',将一个名为left的类添加到div中!我无法弄清楚PHP的语法!

<div class="floating_text" <?php if ($this->position == 'left') echo 'class="left"' ?>>
		<?= $this->text ?>
	</div>

这显然是错误的!

php html5
2个回答
1
投票

将PHP条件移动到现有的class=中,而不是尝试创建另一个错误的class=实例。

<div class="floating_text <?php echo ($this->position == 'left') ? 'left' : '' ?>">
        <?= $this->text ?>
</div>

0
投票

@kerbholz感谢小费!

<div class="floating_text <?php if ($this->position === 'left') echo left ?>">

或者我也可以这样做:

	<div class="floating_text <?php echo $this->position ?>">
© www.soinside.com 2019 - 2024. All rights reserved.