Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement in the middle of concatenation? [closed]

Does this not work? or am I just doing it wrong? Tried multiple variations of it, and can't seem to find any solid info on the subject. any ideas?

    $given_id = 1;
while ($row = mysql_fetch_array($sql))
{
    if ($i < 10){
    $display = '<a href="' . $row['info'] . '" onMouseOver="' . if($row['type']=="battle"){ . 'showB' . } else { . 'showA'() . "><div class="' . $row['type'] . "_alert" . '" style="float:left; margin-left:-22px;" id="' . $given_id . '"></div></a>';
like image 731
VVV Avatar asked Oct 26 '12 15:10

VVV


People also ask

Can you use concatenate in an if statement?

Concatenate If – in pre-Excel 2019The CONCATENATE Function is available but does not take ranges of cells as inputs or allow array operations and so we are required to use a helper column with an IF Function instead. This formula uses the & character to join two values together.

How do I concatenate multiple If in Excel?

Concatenate cells if same value with formulas and filter 1. Select a blank cell besides the second column (here we select cell C2), enter formula =IF(A2<>A1,B2,C1 & "," & B2) into the formula bar, and then press the Enter key. 2. Then select cell C2, and drag the Fill Handle down to cells you need to concatenate.

What are the 2 methods used for string concatenation?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.


1 Answers

if is a self standing statement. It's a like a complete statement. So you can't use it in between concatenetion of strings or so. The better solution is to use the shorthand ternary operatior

    (conditional expression)?(ouput if true):(output if false);

This can be used in concatenation of strings also. Example :

    $i = 1 ;
    $result = 'The given number is'.($i > 1 ? 'greater than one': 'less than one').'. So this is how we cuse ternary inside concatenation of strings';

You can use nested ternary operator also:

    $i = 0 ;
    $j = 1 ;
    $k = 2 ;
    $result = 'Greater One is'. $i > $j ? ( $i > $k ? 'i' : 'k' ) : ( $j > $k ? 'j' :'k' ).'.';
like image 105
Sony Mathew Avatar answered Sep 19 '22 05:09

Sony Mathew