<?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.
<?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.
Simply print them -
echo "<a href='" . $url[0] . "'>" . $url[0] . "</a>";
Or
echo "<a href='$url[0]'>$url[0]</a>";
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.