Conditional swapping of variables

It’s called a Tertiary Operator. The above code is the equivalent to this:

$onoff='';
if ((date_format($new_date,'w') == 0 || date_format($new_date,'w') == 4)) {
    $onoff='on';
} else {
    $onoff='off';
}
$day_box='<div class="'.$onoff.'">'; //the class now defines all element styling.

ok, so if I was describing this in English, would I be correct in saying the following:
If today is either Sunday or Thursday, apply the class “on” otherwise apply the class “off”

Correct, mostly. The formatting for a ternary (not tertiary :wink: ) operator is:

($condition_test) ? $true_statement : $false_statement

Which is a shorthand for
if ($condition_test) {
$true_statement
} else {
$false_statement
}

Additionally, I had to put it in a wrapping ( ) so that the order of precedence worked correctly, and didnt try and use my test as a concatenated element (Concatenation has a higher precedence than ? or : ). Plus, it just looks neater. (I always wrap () around any concatenated thing that isnt just a variable).

It should be noted that PHP doesnt think I put it “in the middle of a class name”. PHP has no concept of the HTML enclosed in the string having meaning - it’s simply characters in a string. So as far as PHP is concerned:


'&lt;div class="'.((date_format($new_date,'w') == 0 || date_format($new_date,'w') == 4) ? "on" : "off").'"&gt;'

and


'Im a duck'.((date_format($new_date,'w') == 0 || date_format($new_date,'w') == 4) ? "on" : "off").'quack quack'

are equivalent expressions. The BROWSER interprets the output of the script; the PHP engine does not.

So what this actually says from PHP’s perspective is:

If today is either Sunday or Thursday, put the string “on” into this string otherwise put the string “off” in.

The browser then looks at the fully formed output (what you see if you do a View Source) and says “Okay, this element is a member of class on”.

This is an important distinction because PHP classes are not the same things as CSS classes.

Ok thanks, I get it, that’s very powerful.
Just like a kid that can’t get a job without experience, I struggle to find a way to apply my meager knowledge of php. Are there any common intermediate real world exercises you can point me to?

Well the way I’ve always taught myself is to have a project, and then learn what i needed to achieve my project.

Start a personal website for… collections of things. For… a hobby. Set yourself a goal like… “I want to be able to dynamically display a week of events based on a dropdown box.” and start figuring out what needs to happen to make that happen.

Don’t over-stress on learning the shortcuts right away - get the basics down pat, and then work on simplifying working code.

Good advice - thanks for that!

have a ‘Watering Days’ table I wish to reproduce from a government water authority site. I have supplied a screenshot as the site requires local input fields to generate the table I wish to re-create.

Might be best to start a new thread on this as it’s a separate question, rather than drag one up from last year. I can’t see your screenshot and I don’t really see the question either? Can you post some code that you’re having trouble with?