0

I am trying to figure out the order in which PHP check types/object during runtime. So I can become a better coder. Thanks

Which type do php check first. Does it validate if a object is Boolean first? null? array?

which faster:

if (ID != null)

if (ID !== false)

if (ID == 0)

1 Answer 1

3

These are micro-optimizations. Your time would be spent better optimizing something else.

I'm not sure if there are any comparisons on this, but check out http://www.phpbench.com.

Also, you may take a look at http://php.net/manual/en/types.comparisons.php and http://php.net/manual/en/language.operators.precedence.php, although they do not give you benchmarks.

Again, your time would likely be spent better focusing on optimizing something else.

Sign up to request clarification or add additional context in comments.

3 Comments

I very much agree with the comment about this being a micro-optimization. I would also like to add that in terms of order of operation, I is unclear what you mean. Each action would take place in the order called in the script. The three conditionals you present each to different things. I think the most important thing to note is loose (i.e. == and !=) versus strict (=== & !==) comparisons can present totally different behavior (and in some cases unexpected behavior to those new to PHP). You should really study the difference there - it is much more important than which is faster.
@Mike, I know in python the compiler always check if the object is type bool first. For example, if None: is a slower than if False: my question is do php do the samething?
@simshaun I want micro-optimization my code. If I know which way is better, I will adopt it into my coding practices. As a result, I can write optimized code all the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.