简体   繁体   中英

Ternary Operator concatenate $variable

i feel lost with this thing.

<?php
$check = '1';

function showOptions($value, $dbh) {
    $dbh1 = $dbh;
    $value1 = $value;

    echo '<div class"myclass">'.$value1.'</div>';
}

 $options = showOptions ("Hello World!", 'db');
 $tabs = ($check != '2' ? '<div id="tabs-5" class="panel">'.$options.'</div>' : '');
 echo $tabs;
?>

as result is get:

<div class"myclass">Hello World!</div>
<div id="tabs-5" class="panel"></div>

instead of:

<div id="tabs-5" class="panel"><div class"myclass">Hello World!</div></div>

how do i concatenate it correctly ?

thx

You have to change your echo statement in the function to a return statement like this:

(Otherwise you don't return anything and in the variable $options nothing get's saved)

function showOptions($value, $dbh) {
    $dbh1 = $dbh;
    $value1 = $value;

    return '<div class"myclass">'.$value1.'</div>';
  //^^^^^^ See here return instead of echo
}

 $options = showOptions ("Hello World!", 'db');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM