-1

I have a custom function in functions.php that returns HTMl and some data. I need to pass some of it through a conditional to check for it's existence otherwise don't print it.

What I have looks like this:

$webinarDate = get_field('webinar_date');

// more code between here

$filteredresourcesHtml .= '
            <div class="cell small-12 medium-4 large-3 equal-height">
                <a class="asset-block ' . $post_type . '" href="' . $resource_url . '">
                  <i class="icon-' . $post_type . '"></i>
                  <p class="content-type">' . ucfirst($post_type_display) . '</p>
                  <h4>' . $shortTitle . '</h4>
                  <h5>' . ($webinarDate ? date("F d, Y", strtotime($webinarDate))) . '</h5>
                </a>
            </div>';

The important line is this:

<h5>' . ($webinar ? date("F d, Y", strtotime($webinarDate))) . '</h5>

The line above gives me:

Fatal error: Uncaught Error: syntax error, unexpected ')'

if statement in the middle of concatenation?

PHP if... else alternative syntax throwing "unexpected ':' in..." error message

How to concatenate if statement inside echo in php?

All of these are saying to use a ternary operator, which I am doing however it's not clear how specifically to format this, and the answers above don't provide clarity on how to leverage with the strtotime function and outputting a date.

Concatenation of multiple ternary operator in PHP? unexpected ')'

This suggests you need an else so I tried:

<h5>' . ($webinarDate ? (date("F d, Y", strtotime($webinarDate)) : ('')) . '</h5>

But this gives me

Fatal error: Uncaught Error: syntax error, unexpected ':'

How do you use a ternary operator in string concatenation with strtotime? What specifcally is the syntax for this?

The closest I currently have it is:

<h5>' . (($webinarDate) ? (date("F d, Y", strtotime($webinarDate))) . '</h5>

But on the whole statement:

$filteredresourcesHtml .= '
            <div class="cell small-12 medium-4 large-3 equal-height">
                <a class="asset-block ' . $post_type . '" href="' . $resource_url . '">
                  <i class="icon-' . $post_type . '"></i>
                  <p class="content-type">' . ucfirst($post_type_display) . '</p>
                  <h4>' . $shortTitle . '</h4>
                  <h5>' . (($webinarDate) ? (date("F d, Y", strtotime($webinarDate))) . '</h5>
                </a>
            </div>';

I get an error on the last div that says

Fatal error: Uncaught Error: syntax error, unexpected ';'

kawnah
  • 3,204
  • 8
  • 53
  • 103
  • 1
    Might be me, but my guess is that you're simply missing the "false" portion of the if statement and you messed up the brackets on your posted attempt. Try it like this; `
    ' . ($webinar ? date("F d, Y", strtotime($webinarDate)) : "" ) . '
    ` . Note the number of opening and closing brackets and compare with what you tried.
    – Alain Doe Jun 01 '21 at 20:44
  • ah ya that was it, I was getting lost in the weeds of all the parenthesis etc – kawnah Jun 01 '21 at 20:47
  • 1
    The structure of the ternary if statement is as follows: `condition ? valueWhenTrue : valueWhenFalse`. The three operands must be expressions, and the parentheses must be balanced for each of the expressions. – MC Emperor Jun 01 '21 at 20:49

2 Answers2

0

Hello the best for you is to get the value of your ternary condition in a variable and then to concaternate it in the string you want

$filteredresourcesHtml = condition ? $firstValue: $defaulValue
khazim
  • 11
  • 4
0

Looks like you're missing the false portion of the "shorthand if" statement and you made a little typo in your attempt to fix it.

<h5>' . ($webinarDate ? date("F d, Y", strtotime($webinarDate)) : "" ) . '</h5>

This should work.

The syntax for a "shorthand if" is;

Condition ? True : False

My previous comment as an answer so that this question can neatly be removed from the unanswered list. Although it's not the most informative of answers.

Alain Doe
  • 532
  • 3
  • 7