Ternary Operator and String Concatenation Quirk

Ternary operator and string concatenation quirk?


$description = 'Paper: ' . ($paperType == 'bond' ? 'Bond' : 'Other');

Try adding parentheses so the string is concatenated to a string in the right order.

HTL Language, string concatenation and ternary operator

@ token in ternary expression is not a valid token at this place. You can move the @join to end of expression.

 ${properties.hash ? [model1.href,properties.hash] : model1.href @ join='#'}

@join doesn't impacts simple string, leaves as it is, which would the case if properties.hash is false

Why ternary condition not working perfectly for string concatenation

The operator precedence plays a crucial role here. The expression is evaluated as:

(foo.upper() + "_" + "") if bar is 0 else str(bar)

This is because conditional expressions precede addition and subtraction.

Use parenthesis to enforce your desired evaluation order:

foo.upper() + "_" + ("" if bar is 0 else str(bar))

Or, what is probably even better is to decrease complexity by extracting a variable to avoid any possible confusion:

postfix = "" if bar is 0 else str(bar)
print(foo.upper() + "_" + postfix)

Ternary Operator issue

just put () around statement:

$result = 'http://domain.dev/category' . (empty($condition) ? '' : '/' . $categoryId);

so it's treated as operator

How to create string concatenation operator which preserves trailing spaces in char(n)

Use string functions

SELECT concat('A'::char(2), 'B');

String concatenation concat() and + operator usage effectively

There's are difference.

If aStr is null, then aStr.concat(bStr) >> NPEs

but if aStr += bStr will treat the original value of aStr as if it were null.

Also, the concat() method accepts just String instead the + operator which converts the argument to String (with Object.toString()).

So the concat() method is more strict in what it accepts.

Also, if you have lot of String concatenations with concat() or +, I highly recommend to work with mutable StringBuilder object that will increase speed of your code.

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

Very close, but a couple things to look at.

  • There is an extra ( before each of your issets.
  • 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>";


Related Topics



Leave a reply



Submit