StudioTime
StudioTime

Reputation: 23959

Which way to write this code is most the efficient?

If I am inside a ton of PHP code, instead of coming out of PHP I usually write code which contains variables like so:

echo "This is how I use a ".$variable." inside a string";

but is it more efficient to actually get out of PHP like so:

?>

Should I instead use the <? echo $variable; ?> like this

<? // then back into PHP

Throughout a page there will be lots of instances of code like this as I fill pages with dynamic content, or is this too much of a generalisation?

Upvotes: 5

Views: 176

Answers (6)

Destiny Makuyana
Destiny Makuyana

Reputation: 93

Another way is to use printf() as follows:

// your variable
$my_variable = "Stack Overflow";

printf("Real programmers graduate on %s", $my_variable);

// this prints 'Real programmers graduate on Stack Overflow, 

Plus has the added bonus of not needing to concatenate frequently. However it might be worth finding out how it performs as compared to echo statement

Upvotes: 0

Robbie McAlister
Robbie McAlister

Reputation: 61

In most cases, it is recommended to not use the short tags for php. In any case, if you're inside double quotes, you don't even have to use the concat dots to output a variable. You can just do this:

echo "This is how I use a $variable inside a string";

or this:

echo "This is how I use a {$variable} inside a string";

The second example allows expressions (array elements and object properties) to be delimited by the curly braces. You can use them in the first example too but the greediness of the variable parsing sometimes causes problems. Using the curly braces stops that.

Upvotes: 0

Dan Simon
Dan Simon

Reputation: 13117

Using echo appears to be slightly faster. I made this simple benchmark script:

<?php
$variable = "hello world";
$num = 10000;
$start1 = microtime(true);
for ($i = 0; $i<$num;$i++) {    
    echo "test " . $variable . " test\n";    
}
$time1 = microtime(true) - $start1;
$start2 = microtime(true);
for ($i = 0; $i<$num;$i++) {
    ?>test <?php echo $variable;?> test
<?
}
$time2 = microtime(true) - $start2;
echo "\n$time1\n$time2\n";

The echo loop was consistently about 25% faster.

In reality this difference is so minor it wouldn't have any impact on overall performance unless you were literally doing millions of such output statements. I would still recommend using echo just because it's more straightforward and easier to read.

Upvotes: 1

Yes just use <?php ..... ?> between your non-PHP (HTML) wherever you need. You can even have a do-while , for loops inside them.

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227190

I only suggest breaking out of php tags when echoing HTML, not just a string.

This is fine for just a string:

// You don't need to concat, double quotes interpolate variables
echo "This is how I use a $variable inside a string";

But with HTML, personally, I like to do this:

<?php //... ?>
<div>
    <span>This is how I use a <?=$variable?> inside HTML</span>
</div>
<?php //... ?>

Upvotes: 5

Demian Brecht
Demian Brecht

Reputation: 21368

echo 'This is how I use a'.$variable.' inside a string'; is probably the most efficient here..

In your first case, you're using ", which causes the entire string to be evaluated for inline variables.

In your second case, I'd imagine that the context switch can be a little more expensive for the interpreter than in my sample.

Having said that, you're looking at a negligible difference either way.

Upvotes: 0

Related Questions