Dan
Dan

Reputation: 517

add variable to ternary operator Php

I want to summarize this ternary operator and add a new variable to the result (in a single line)

$names= "Joe";
$channel ="joe";

 $user_alt= $names ? $names : "";
 $user_alt= $user_alt." @".$channel;

EXPECTED RESULT

   <img src=""  alt="Joe @joe" />

Upvotes: 1

Views: 44

Answers (1)

Nick
Nick

Reputation: 147146

I think what you want to do is:

$user_alt= ($names ? $names : "") ." @".$channel;

Note the necessity of the brackets around the ternary operator as : has lower precedence than ..

Upvotes: 1

Related Questions