-4

I am currently working to improve a PHP timetable. A statement being used by the timetable is:

echo $array2['username'] ? $array2['username'] : '-';

I understand that echo is displaying the information but why is there is a question mark? also what is the purpose of the colon?

I'm trying to advancde the above line to include PHP if statements and also a href tag so I need to know what the above statement is doing in order to break it up so I can recode appropriately.

Andrew
  • 359
  • 2
  • 6
  • 20
  • 1
    Look for PHP ternary statement. –  Jul 27 '14 at 10:18
  • 1
    possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Clive Jul 27 '14 at 10:18
  • its a ternary statement read this http://davidwalsh.name/php-shorthand-if-else-ternary-operators – Azeem Hassni Jul 27 '14 at 10:25

1 Answers1

1

Those are ternary operators. it is equivalent to an if-else statement.

The statement above is trying to see if the index username is present or set. if true, it uses the value of index username. else, it returns an empty string.

Reference: http://en.wikipedia.org/wiki/%3F:

Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22