Search code examples
phpconcatenationternary

Can I store html in a variable if the html includes a concatenated isset() ternary expression?


I don't even know how to ask this clearly so I apologize for any confusion.

Basically I'm trying to store html in hoverHTML whereby if $img['socialfb'] contains a value it will concatenate the <li> element that displays the correct social icon and if not, it moves on. This would eventually include other social icon link items.

$hoverHTML = '<div class="thumbnail-overlay">
                <h2><a href="'.$img['link'].'"> '.$img['data']->post_title.'</a></h2>
                    <ul class="socials">'
                        (isset($img['socialfb']) ? .'<li><a href="'.$img['socialfb'].'"><i class="fab fa-facebook"></i></li>'. : .''.)                          
                   '</ul></div>';
return $hoverHTML;

Is this even possible? I'm still very new to php but trying to mod an existing plugin.

Here is the full function:

function getHover( $img ){
        $hoverHTML = '';
        if(!$this->hover) return $hoverHTML;
        if($this->hover == 1){
            $hoverHTML .= $this->titleHover;
            if( $this->linkIcon || $this->zoomIcon ){
                $hoverHTML .= '<div class="rbsIcons">';
                if($this->linkIcon && $img['link']) $hoverHTML .= '<a href="@LINK@" '.($img['typelink']?'target="_blank"':'').' title="@TITLE@">'.$this->linkIcon.'</a>';
                if($this->zoomIcon) $hoverHTML .= $this->zoomIcon;
                $hoverHTML .= '</div>';
            }
            $hoverHTML .= $this->descHover;
        }

        if($this->templateHover) $hoverHTML = $this->templateHover; 

        if($hoverHTML){             
            $hoverHTML =  str_replace( 
                array('@TITLE@','@CAPTION@','@DESC@', '@LINK@', '@SOCIALFB@'), 
                array( 
                    $img['data']->post_title,
                    $img['data']->post_excerpt,
                    $img['data']->post_content,
                    $img['link'],
                    $img['socialfb'],
                ), 
                $hoverHTML
            );
        }
        $hoverHTML = '<div class="thumbnail-overlay">
                        <h2><a href="'.$img['link'].'"> '.$img['data']->post_title.'</a></h2>
                        <ul class="socials">'
                            (isset($img['socialfb']) ? .'<li><a href="'.$img['socialfb'].'"><i class="fab fa-facebook"></i></li>'. : .''.)                          
                        '</ul>

                      </div>';
        return $hoverHTML;
    }

Solution

  • So yes you can, but you have syntax error change $hoverHTML to this:

    $hoverHTML = '<div class="thumbnail-overlay">
                <h2><a href="'.$img['link'].'"> '.$img['data']->post_title.'</a></h2>
                    <ul class="socials">'
                        .(isset($img['socialfb']) ? '<li><a href="'.$img['socialfb'].'"><i class="fab fa-facebook"></i></li>' : '').                          
                   '</ul></div>';
    return $hoverHTML;