Home > Blockchain >  PHP inline if not working when condition is string "0"
PHP inline if not working when condition is string "0"

Time:05-15

I have many inline if's like this but only this one not working:

$border = $settings[$key]['border'] ? 'border-width:'.$settings[$key]['border'].'px;' : null;

$settings[$key]['border'] is "0" and gettype($settings[$key]['border']) return string.

I tried convert "0" again to string by adding (string) before $settings[$key]['border'].

    public static function GenerateStylesFromSettings($key, $settings){

            $bg         =   $settings[$key]["background"]           ? 'background-color:'.$settings[$key]["background"].';'     : null;
            $pl         =   $settings[$key]["position"]['left']     ? 'left:'.$settings[$key]["position"]['left'].'%;'          : null;
            $pt         =   $settings[$key]["position"]['top']      ? 'top:'.$settings[$key]["position"]['top'].'%;'            : null;
            $c          =   $settings[$key]['font']["color"]        ? 'color:'.$settings[$key]['font']["color"].';'             : null;
            $f          =   $settings[$key]['font']["family"]       ? 'font-family:"'.$settings[$key]['font']["family"].'";'    : null;
            $s          =   $settings[$key]['font']["size"]         ? 'font-size:'.$settings[$key]['font']["size"].'mm;'        : null;
            $width      =   $settings[$key]['width']                ? 'width:'.$settings[$key]['width'].'mm;'                   : null;
            $height     =   $settings[$key]['height']               ? 'height:'.$settings[$key]['height'].'mm;'                 : null;
            $linehe     =   $settings[$key]['height']               ? 'line-height:'.($settings[$key]['height']-1.5).'mm;'      : null;
            $border     =   (string) $settings[$key]['border']              ? 'border-width:'.$settings[$key]['border'].'px;'           : null;
            $borderc    =   $settings[$key]['border_color']         ? 'border-color:'.$settings[$key]['border_color'].';'       : null;
            $borders    =   $settings[$key]['border_style']         ? 'border-style:'.$settings[$key]['border_style'].';'       : null;
            $visibility =   $settings[$key]['visibility']           ? 'visibility:'.$settings[$key]['visibility'].';'           : null;

            $css        =   ($bg)                                   ? $bg                                                       : null;
            $css        .=  ($pl)                                   ? $pl                                                       : null;
            $css        .=  ($pt)                                   ? $pt                                                       : null;
            $css        .=  ($c)                                    ? $c                                                        : null;
            $css        .=  ($f)                                    ? $f                                                        : null;
            $css        .=  ($s)                                    ? $s                                                        : null;
            $css        .=  ($width)                                ? $width                                                    : null;
            $css        .=  ($height)                               ? $height                                                   : null;
            $css        .=  ($linehe)                               ? $linehe                                                   : null;
            $css        .=  ($border)                               ? $border                                                   : null;
            $css        .=  ($borderc)                              ? $borderc                                                  : null;
            $css        .=  ($borders)                              ? $borders                                                  : null;
            $css        .=  ($visibility)                           ? $visibility                                               : null;

            $styles     =   ($css) ? 'style=\''.$css.'\'' : null;

            return $styles;

}

Not working. Edit: I need make that inline if as true to generate border-width: 0px.

CodePudding user response:

As string '0' is considered false in php, you should use another comparisons like:

null !== $settings[$key]['border'] ? 'value' : null;
// if value is always a string - compare with empty string
'' !==  $settings[$key]['border'] ? 'value' : null;
  • Related