0
<?php
echo "<a href=$url[0]>...</a>";
?>

Thats fine. How to add array value at the dotted place? I want to show '0' as url name which is not fixed. Thanks in advance.

2 Answers2

3

Simply print them -

echo "<a href='" . $url[0] . "'>" . $url[0] . "</a>";

Or

echo "<a href='$url[0]'>$url[0]</a>";
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
2

The "optimisations" given to you here are somewhat incorrect.

Let's take a look at the following code:

<?php
$var = 'a';
$runs = 1000000;

ob_start();
$double = microtime(true);
for($i=0; $i<$runs; $i++)
{
        echo "string{$var}string{$var}string{$var}string{$var}";
}
$double = microtime(true) - $double;

$singleConcat = microtime(true);
for($i=0; $i<$runs; $i++)
{
        echo 'string'.$var.'string'.$var.'string'.$var.'string'.$var;
}
$singleConcat = microtime(true) - $singleConcat;

$singleSequence = microtime(true);
for($i=0; $i<$runs; $i++)
{
        echo 'string', $var, 'string', $var, 'string', $var, 'string', $var;
}
$singleSequence = microtime(true) - $singleSequence;
ob_clean();
?>
<p>Double quotes: <?php echo $double; ?></p>
<p>Single quotes concatenated: <?php echo $singleConcat; ?></p>
<p>Single quotes sequenced: <?php echo $singleSequence; ?></p>

My results are:

Double quotes: 0.26701784133911

Single quotes concatenated: 0.2887818813324

Single quotes sequenced: 0.32703709602356

Now that is a little strange, why would that be? It is especially strange, since the common wisdom is that single quotes are faster.

The reason for this is that PHP is forced to create tons of anonymous objects.

The code 'string'.$var.'string'.$var.'string'.$var.'string'.$var does not mean "take all of these strings and concatenate them together", rather it means "take the first string and concatenate it to second, then take the result and concatenate it to third, etc".

This causes PHP to actually do more work in the process.

<?php
$var = 'a';
$runs = 1000000;

ob_start();
$double = microtime(true);
for($i=0; $i<$runs; $i++)
{
        echo "string{$var}";
}
$double = microtime(true) - $double;

$singleConcat = microtime(true);
for($i=0; $i<$runs; $i++)
{
        echo 'string'.$var;
}
$singleConcat = microtime(true) - $singleConcat;

$singleSequence = microtime(true);
for($i=0; $i<$runs; $i++)
{
        echo 'string', $var;
}
$singleSequence = microtime(true) - $singleSequence;
ob_clean();
?>
<p>Double quotes: <?php echo $double; ?></p>
<p>Single quotes concatenated: <?php echo $singleConcat; ?></p>
<p>Single quotes sequenced: <?php echo $singleSequence; ?></p>

And now the results are what we expect:

Double quotes: 0.10352993011475

Single quotes concatenated: 0.085345029830933

Single quotes sequenced: 0.088818073272705

So the question of optimisation becomes not as simple. If you have many variables within your string, then by all means use double quotes, if it's only once that a variable appears, and it appears in the beginning or the end, then use single quotes+concatenation or single quotes+sequential output.

v010dya
  • 5,296
  • 7
  • 28
  • 48
  • 2
    this is great, but what does it have to do with the question? should be more on [code review](http://codereview.stackexchange.com/) – DevDonkey Jul 09 '15 at 10:35
  • @v010dya Although some time has passed, in my opinion your answer should be also (or could be of better fit) on https://stackoverflow.com/questions/13620/ – edmundo096 Nov 02 '20 at 22:56