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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bartvdCommented:
if $bg is equal to #eeeeee, $bg becomes equal to #ffffff, if not, $bg becomes equal to #eeeeee
0
bartvdCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.