18

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>';
VVV
  • 417
  • 2
  • 6
  • 10
  • 1
    why would you want to write unreadable code like that anyway? Layout the code properly and you will likely fix the error in the process. Start by using `sprintf` – Gordon Oct 26 '12 at 15:28
  • 3
    Have no idea why this question was closed as having anything to do with geography, time or an unusual situation. The idea of the ternary operator x?y:z is used commonly, in many situations, and I found this page basically looking to find out if PHP offered this feature. Fortunately this page still existed (despite being closed), and was able to resolve my query. – dvaey Oct 03 '17 at 02:02

4 Answers4

54

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' ).'.';
Sony Mathew
  • 2,929
  • 2
  • 22
  • 29
  • 5
    I think the readability of that last line is approximately equal to the result of `(1 > 2 ? ( 'x' > 'flowers' ? 0 : 3 ) : ( $j > 42 ? ceil(pi()) : $kittens = true ) -1)` – Mike Aug 02 '13 at 01:32
  • 2
    -1; what you're groping for in you first few sentences is the distinction between a *statement* and an *expression*, but instead you use the made-up terms "self standing statement" and "complete statement" which don't really explain why an `if` construct can't be concatenated onto a string. – Mark Amery Mar 25 '17 at 16:52
  • I did not use it exactly that way but upvote for giving me an idea – hocikto Oct 28 '18 at 17:53
8

if..else is a statement and cannot be used inside an expression. What you want is the "ternary" ?: operator: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • The ternary operator is nice, but it can make code less readable. Use it where it makes sense, and if someone else is going to be reading your code you might want to consider not using it at all. – user428517 Oct 26 '12 at 15:28
4

Use a shorthand if statement using ternary operators ?: -

$display = 'start ' . (($row['type']=="battle")? 'showB' : 'showA') . ' end ';

See "Ternary Operators" on http://php.net/manual/en/language.operators.comparison.php

doublesharp
  • 26,888
  • 6
  • 52
  • 73
  • A ternary isn't a "shorthand if statement", it's an *expression*. – Mark Amery Mar 25 '17 at 16:53
  • The good with this is that you can save time and resources in storing string in variables and then concatenate into your other strings. It goes with the flow of the existing string. – Thanasis Mar 24 '19 at 08:37
2

if is a statement. One cannot put statements inside an expression.

$str = 'foo';
if (cond)
{
  $str .= 'bar';
};
$str .= 'baz';
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358