manga
manga

Reputation: 105

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

Upvotes: 0

Views: 50

Answers (1)

Rizier123
Rizier123

Reputation: 59681

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');

Upvotes: 5

Related Questions