What runs faster?
Setting a default value and changing it-
$foo = ""; if($bar) { $foo = "someValue"; }
Or-
if($bar) { $foo = "someValue"; } else { $foo = ""; }
What runs faster?
Setting a default value and changing it-
$foo = ""; if($bar) { $foo = "someValue"; }
Or-
if($bar) { $foo = "someValue"; } else { $foo = ""; }
At a guess, the 2nd one "potentially". First one you're potentially setting 2 values. Second one you're definitely only setting one.
Its also a question though of code clarity. If you've got 2 alternatives (e.g turn left or right) then use the "else" statement. If you've got a 'normal' value vs a flag e.g http or https, then use the first one
EDIT: becose you valorize a variable in base to another one, the isset() statement is mandatory.. so the 'faster one' is the second way becose, as David said, yoo valorize the $foo var just one time.
Also consider the Anton suggestion to use the short if syntax (dont know if it speed up the execution)
P.s: if your goal is to speed up many if like that one, use the ' instead of ", becose the content inside "" is being evalutated by php (in case it contain a variable:
You absolutely should not be bothered with performance of a single if
statement.
Also, consider:
$foo = $bar ? "" : "someValue"