Concatenate Hyperlinks in PHP

Hi all,

probably a basic question if anyone can help me out:

I need to concatenate a link to a string of text and be able to do this with either double or single quotes.

The PHP Code:

[php]<?php
$website1 = “http://www.wikipedia.org”;
$website2 = “http://www.nationalgeographic.com”;

echo “Catch up on all the latest, visit: Wikipedia”; //works

echo “

”;

echo ‘Catch up on all the latest, visit: National Geographic’; //works
?>[/php]

Both the double and single examples work fine, links are spaced for easier reading.

My questions relates to the second statement and the use of single quotes around the .

[php]<?php
echo ‘Catch up on all the latest, visit: National Geographic’; //works
echo “

”;

echo ‘Catch up on all the latest, visit: National Geographic’; //works
echo “

”;

echo ‘Catch up on all the latest, visit: National Geographic’; // Does not work
?>[/php]

Why do examples 1 and 2 only work and which is the best option to go with if any around the ?

i.e. or or it doesn’t matter?

Thanks,

Andy :wink:

Because using double quotes PHP will still parse the content and execute any PHP it finds, it will not when using single quotes.

[php]$name = ‘JimL’;

echo “Thank you $name”; // Thank you JimL
echo ‘Thank you $name’; // Thank you $name[/php]

Not echoing HTML at all. It makes the code easier to read, your IDE will highlight the syntax properly, autocomplete and display errors as it should, etc.

[php]<?php

$website1 = “http://www.wikipedia.org”;
$website2 = “http://www.nationalgeographic.com”;

?>

Catch up on all the latest, visit: Wikipedia



Catch up on all the latest, visit: National Geographic[/php]

Thanks JimL,

So if a variable is declared and initialised with single quotes

[php]$website1 = ‘http://www.nationalgeographic.com’; [/php]

then [php] “.$website1.”[/php] will not work as $website1 will be treated as a literal string.

However, [php]"’.$website1.’"[/php] or [php]’.$website.’ [/php]will work.

Okay, go it, thanks JimL, Andy :wink:

With double quotes PHP will execute code
[php]echo “Lorem ipsum $dolor is amet”;[/php]

With single quotes it’s treated as a literal string and not parsed as code. If you want to concatenate you will have to stop the string and append any variables you want to insert.

[php]echo ‘Lorem ipsum ’ . $dolor . ’ is amet’;[/php]

If variables are hardcoded with the urls you want, why are you using the variables at all? Are they coming from another source or is this just being used as an example?

Hi astonecipher, it’s just being used as an example. Thanks, Andy :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service