0

I'm essentially converting this php code into a string assigned to a var so I can return it's value within a function:

<?php if ($add_cta=='yes' ){?>
   <a class="button" href="<?php echo strip_tags(trim($a_href)); ?>">
     <?php echo strip_tags(trim($a_title)); ?>
   </a>
<?php } ?>

I have converted the above into the following:

$html = '

($add_cta == "Yes" ? .
    ' < a class = "button" href = "'.strip_tags(trim($a_href)).
    '" > '.strip_tags(trim($a_title)).
    ' < /a>'. : "")
';

return $html;

But getting unexpected '.' errors on the line ($add_cta == "Yes" ? .'

But that is required to concat the string and php, right? where am I going wrong

Freddy
  • 683
  • 4
  • 35
  • 114
  • 1
    you open it with a single quote? PHP doesn't execute inside single quotes - use double quotes or, as I prefer, concatenation – treyBake Jul 09 '19 at 09:42
  • Are you sure you want to use such complicated code if you are struggling with it so hard? Shouldn't your code be more readable to enhance maintainability? – Nico Haase Jul 09 '19 at 09:46
  • @NicoHaase - I agree, I want it to be simplified. But what's the alternative? I need the markup to be stored as a variable so I can return its value after – Freddy Jul 09 '19 at 09:49
  • 2
    This is a perfect example where using an `if`-statement drastically improves the readability of your code. Just because you _can_ use ternary, it doesn't mean it's a good idea. Readability is _very_ important. – M. Eriksson Jul 09 '19 at 09:54
  • @Machavity this is not a duplicate of the question you linked to. It's helpful for understanding, but even with double quotes it's not a solution. The statement inside the string `($add_cta == "Yes" ? ...` will not be executed inside any quotes. – mixable Jul 09 '19 at 11:29
  • @mixable Yes, I know there's typos as well, but both are close reasons. Typo questions shouldn't be answered per the [help/on-topic] – Machavity Jul 09 '19 at 12:10

3 Answers3

1

You have to correct the usage of your single quotes. Especially the first and the last single quotes are not necessary. PHP does not execute any code inside the single quotes. You can use double quotes, but this will only print variables and makes things more complicated in combination with HTML. The following code uses the correct single quotes:

$html = ($add_cta == "Yes" ? .
    '<a class="button" href="'.strip_tags(trim($a_href)).'">'.
    strip_tags(trim($a_title)).
    '</a>'. : '');
return $html;

Or just use an if statement:

$html = '';
if ($add_cta == "Yes")
{
    $href = strip_tags(trim($a_href));
    $title = strip_tags(trim($a_title));
    $html .= ' <a class="button" href="'.$href.'">'.$title.'</a>';
}
return $html;
mixable
  • 1,068
  • 2
  • 12
  • 42
0

try this. You have done some concatenation mistakes which I have fixed

$a_href = "stackoverflow.com";
$a_title = 'Anything';
$html = 

($add_cta == "Yes" ? 
    ' < a class = "button" href = "'.strip_tags(trim($a_href)) .
    '" > '.strip_tags(trim($a_title)) .
    ' < /a>' : "")
;

echo $html;
Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
0

I think the most simple/readable way to do this is by using a separate template which returns the rendered link.

link-template.php

<?php

return '<a href="' . strip_tags(trim($a_href)) . '">' . strip_tags(trim($a_title)) . '</a>';

The method/function you want to use this template on:

return $add_cta === 'Yes' ? include 'link-template.php' : '';

What you should consider would be to define $a_href and $a_title before including the template

Mihai Matei
  • 24,166
  • 5
  • 32
  • 50