How do I read this: $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');

I know it changes the background color. I'm trying to wrap my head around how it's read. I see $bg=='#eeeeee'  means $bg equals '#eeeeee' . Then the ? mark. What is it all saying?

What's it telling the script to do to have it alternate background colors? Break it down, please.
QUESTOMNIAsked:
Who is Participating?
 
bartvdConnect With a Mentor Commented:
So in fact it is exactly the same as:
if($bg=='#eeeeee')
$bg = '#ffffff';
else
$bg = '#eeeeee';

It is just a shorter way to write it.
0
 
bartvdConnect With a Mentor Commented:
if $bg is equal to #eeeeee, $bg becomes equal to #ffffff, if not, $bg becomes equal to #eeeeee
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
QUESTOMNIAuthor Commented:
bartvd is that what the ? mark is indicating.
if($bg=='#eeeeee')
$bg = '#ffffff';

You say
else
$bg = '#eeeeee';

Isn't it establishing that
$bg equals '#eeeeee'
from the start.
0
 
QUESTOMNIAuthor Commented:
I got it.

Ternary Operator
if this(condition) ? value true : value false
if ($bg=='#eeeeee' ? $bg = '#ffffff'; : else $bg = '#eeeeee';)

So $bg=='#eeeeee' changes to $bg = '#ffffff' when the script is run.
Therefore it becomes $bg='#eeeeee' next run. Alternating colors.
0
 
QUESTOMNIAuthor Commented:
DaveBaldwin gave a URL to a bad example.
bartvd took the time to relate directly to what I needed explained. I actually looked up Ternary before I took time to read and of what DaveBaldwin submitted. Could have got equal credit if he relate directly to what I submitted. I was already frustrated with trying to google the answer. First glance, it looked like more of the same. It's a good effort and worth a 100 points since it had what I looked up and helped me understand better.

Thanks guys.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.