简体   繁体   中英

An echo with has an ternary operator which echo's a span

please see code below, i have been trying to figure this out for an hour now and have gave up and come here! The code is outputting an error a the end of the echo and it doesn't give any reason, i have narrowed it down to an issue with the ternary operator, it is my first time using one so im unsure of where the issue is and have tried all sorts... Have i got the format of the ternary wrong?

The code checks for a value, if the value exists it fills the first cell with a tick glyph and the value of the variable is echo'd into the second cell.

echo "<table id='tbl' class='defecttable'>
    <tr>
        <th>Trailer:</th>
      <td>*Trailer*</td>  
        <th>Vehicle Mileage:</th>
        <td>*vehicle mileage*</td>        
    </tr>
    <tr>
        <th>Checks To Be Made</th>
        <th>Checked</th>
        <th>Reportable Defect?</th>
        <th>Defect Description</th>
    </tr>
    <tr>
        <th>Fuel/Oil Leaks:</th>
        <td><span class='glyphicon glyphicon-ok-circle'></span></td>  
        <td>".((isset($defect[fuel]) ? '<span class=glyphicon glyphicon-ok-circle"></span>': '')."</td>
        <td>".((isset($defect[fuel]) ? $defect[fuel]: '')."</td>       
    </tr>";

Very close, but a couple things to look at.

  • There is an extra ( before each of your isset s.
  • Do you mean to use $defect['fuel'] instead of just $defect[fuel] ? If you don't surround fuel with single or double quotes, it is assumed to be a constant.

Result:

echo "<table id='tbl' class='defecttable'>
    <tr>
        <th>Trailer:</th>
      <td>*Trailer*</td>  
        <th>Vehicle Mileage:</th>
        <td>*vehicle mileage*</td>        
    </tr>
    <tr>
        <th>Checks To Be Made</th>
        <th>Checked</th>
        <th>Reportable Defect?</th>
        <th>Defect Description</th>
    </tr>
    <tr>
        <th>Fuel/Oil Leaks:</th>
        <td><span class='glyphicon glyphicon-ok-circle'></span></td>  
        <td>".(isset($defect['fuel']) ? '<span class=glyphicon glyphicon-ok-circle"></span>': '')."</td>
        <td>".(isset($defect['fuel']) ? $defect['fuel']: '')."</td>       
    </tr>";

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