I noticed that many developers use Math operations as variable values, like:
$time = 60 * 60 * 24 * 7; // 7 days
instead of just assigning the computed value
$time = 604800;
I know it makes the code more easy to understand, but won’t this affect performance?
Do you think it’s a good practice to do this?
In a compiled language there would be no performance penalty as the calculation would be done at compile time. PHP generally is not compiled so there’s probably a small performance penalty. In most cases I would prefer more readable code to higher performance (unless the code becomes a bottleneck). So DON’T worry about performance in this case.
Now if we compare your examples then
is self explanatory even without a comment. If you write
then it’s not obvious, what the value represents. Now, you could add a comment and write
but this is bad for two reasons: