0

How can you add this line of code in a

<a href='/<?=$value["content_url"];?>' class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true"><?=$value["content_title"];?><span class="caret"></span></a>

so eventually you get something like this:

if (1 == 1) { 
    echo "<a href='/<?=$value["content_url"];?>' 
    class="dropdown-toggle" data-toggle="dropdown" role="button" 
    aria-haspopup="true" aria-expanded="true"> <?=$value["content_title"];?>
    <span class="caret"></span></a>";
} else { 
    echo "Nothing to see!" 
}

Because you have multiple punctuations it will end the echo to quick..

0

5 Answers 5

1

Format your string properly. Updated string.

if (1 == 1) { 
    echo "<a href=\"$value[content_url]\"  
    class=\"dropdown-toggle\" data-toggle=\"dropdown\" role=\"button\"  
    aria-haspopup=\"true\" aria-expanded=\"true\">$value[content_title] 
    <span class=\"caret\"></span></a>";
} else { 
    echo "Nothing to see!"; 
}

Inside double quoted string the php variables don't need to be quoted. Escape quotes by a backslash.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This worked for me. Did not know that you could remove the punctuations in the variables.
1

if you want to actually print the PHP-code, you just have to escale your quotes

echo "<a href='/<?=$value[\"content_url\"];?>' class=\"dropdown-toggle\" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"true\"><?=$value[\"content_title\"];?><span class=\"caret\"></span></a>";

if instead you want to print the values, you do not need the tags at all, but just have to concatenate the variables.

echo "<a href='/".
$value["content_url"].
"' class=\"dropdown-toggle\" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"true\">".
$value["content_title"].
"<span class=\"caret\"></span></a>";

3 Comments

Bad example. At least run the code on your end first before suggesting.
oops, sorry. my bad. won't happen again
Mistakes happen... Big world.. bad thing is someone rated your answer up and not yet mine.. lolz
0
if you want to print or echo php code than use concatenate    

if (1 == 1) { 
        echo "<a href='".$value["content_url"]."' class='dropdown-toggle' data-toggle='dropdown' role='button' 
        aria-haspopup='true' aria-expanded='true'> '".$value["content_title"]."' <span class='caret'></span></a>";
    } else { 
        echo "Nothing to see!" 
    }

Comments

0

You can use print_f() function:

print_f("<a href='%s' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-haspopup='true' aria-expanded='true'><span class='caret'>%s</span></a>",
  $value['content_url'],
  $value['content_title']
);

or echo string in "string {var}":

echo "
   <a href='{$value['content_url']}' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-haspopup='true' aria-expanded='true'><span class='caret'>{$value['content_title']}</span></a>
" ;

or use concritenate operand . 'string 1'.'string 2'

echo '
   <a href="'.$value['content_url'].'" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true"><span class="caret">'.$value['content_title'].'</span></a>
' ;

but best way use template for example

index.php

function getContentUrl() {
   return $value['content_url'];
}

function getTitle() {
   return $value['content_title'];
}

include './template.phtml';

and template.phtml

<a href='<?php echo getContentUrl() ?>'><?php echo getContentTitle() ?></a>

Comments

0
<a href='<?php  echo  $value["content_url"]; ?>' class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">
<?php echo $value["content_title"]; ?>
   <span class="caret"></span> 
</a>

try this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.