首页 > 解决方案 > 如何将“px”附加到数组值?

问题描述

我正在开发一个 wordpress 插件,我在使用一些基本的 php 时遇到了一些困难。

<div style="display: flex; align-items:center; justify-content:center; background:{$background_color}; height:{$bar_height}; {$show_hide_border}">

正如你在这里看到的,我有height:{$bar_height}.

这是对数组的调用:

$bar_height = $options['wprcb_api_number_field_barHeight'] ? $options['wprcb_api_number_field_barHeight'] : 'fit-content';

我希望它以这种方式工作:如果wprcb_api_number_field_barHeight存在,则使用此值 AND append px。否则值应该是fit-content

我试着这样写,但它不起作用:

$bar_height = $options['wprcb_api_number_field_barHeight'] ? ($options['wprcb_api_number_field_barHeight'] + 'px') : 'fit-content';

请建议如何以正确的方式编写它。

标签: php

解决方案


这样写:

  $bar_height = $options['wprcb_api_number_field_barHeight'] ? $options['wprcb_api_number_field_barHeight'] . 'px' : 'fit-content';

它应该可以正常工作


推荐阅读