简体   繁体   中英

Syntax for echoing link in PHP

I'm trying to string together a URL and then get it to echo out a link

<?php
$url = 'https://www.google.com/#q='.$word;
echo '<a href="'.$url.'" id="link">' 'Google This!' '</a>';  
?>

Pretty sure it is an issue with the quotes however I'm unsure of how to fix them? Thank you!

echo '<a href="'.$url.'" id="link"> Google This! </a>';  
<?php
$url = 'https://www.google.com/#q='.urlencode($word);
echo '<a href="'.htmlentities($url).'" id="link">Google This!</a>';
echo '<a href="'.$url.'" id="link">' . 'Google This!' . '</a>';

要么

echo '<a href="'.$url.'" id="link">Google This!</a>';

I think the most otimized way to do that is :

<?php
$url = 'https://www.google.com/#q='.$word;
echo '<a href="'.$url.'" id="link">Google This!</a>';
?>

You can have more informations about strings at : http://php.net/manual/en/book.strings.php

First of all you have a break in your string without concatenating.

>' 'Google This!' '</

Should be:

<?php
$url = 'https://www.google.com/#q='.urlencode($word);
echo '<a href="'.$url.'" id="link">'.'Google This!'.'</a>';  
?>

Always safe to use "urlencode()" as well. See php documentation here . It makes sure all characters are safe in the URL.

personally all those single and double quotes drive me crazy so whenever possible i make the php variable and then echo out just what is needed in the html

<?php  $url = 'https://www.google.com/#q='.urlencode($word); ?>

<a href="<?php echo $url; ?>" id="link">Google This!</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM