Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 79847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:12:36+00:00 2026-05-10T21:12:36+00:00

Is it faster to do the following: if ($var != ‘test1’ && $var !=

  • 0

Is it faster to do the following:

 if ($var != 'test1' && $var != 'test2' && $var != 'test3' && $var != 'test4') { ... } 

Or:

 if (!in_array($var, array('test1', 'test2', 'test3', 'test4') { ... } 

Is there a number of values at which point it’s faster to do one or the other?

(In this case, the array used in the second option doesn’t alreay exist.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:12:36+00:00Added an answer on May 10, 2026 at 9:12 pm

    i’d strongly suggest just using in_array(), any speed difference would be negligible, but the readability of testing each variable separately is horrible.

    just for fun here’s a test i ran:

    $array = array('test1', 'test2', 'test3', 'test4'); $var = 'test'; $iterations = 1000000;  $start = microtime(true); for($i = 0; $i < $iterations; ++$i) {     if ($var != 'test1' && $var != 'test2' && $var != 'test3' && $var != 'test4') {} } $end = microtime(true);  print "Time1: ". ($end - $start)."<br />";  $start2 = microtime(true); for($i = 0; $i < $iterations; ++$i) {     if (!in_array($var, $array) ) {} } $end2 = microtime(true);  print "Time2: ".($end2 - $start2)."<br />";  // Time1: 1.12536692619 // Time2: 1.57462596893 

    slightly trivial note to watch for, if $var is not set, method 1 takes much longer (depending on how many conditions you test)

    Update for newer PHP versions:

    Martijn: I’ved extended the array to five elements, and look for test3, as sort of an average case.

    PHP5.6

    Time1: 0.20484399795532 Time2: 0.29854393005371 

    PHP7.1

    Time1: 0.064045906066895 Time2: 0.056781053543091 

    PHP7.4

    Time1: 0.048759937286377 Time2: 0.049691915512085 

    PHP8.0

    Time1: 0.045055150985718 Time2: 0.049431085586548 

    Conclusion: The original test wasnt the best test, and also: In php7+ is has become a matter of preference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that it's faster to do the following: var $header = $(#header); $header.css({color:#ff0000});
Is there any way I could optimize the following script to run faster? foreach
I have the following text: <i><b>It is noticeably faster.</b></i> <i><b>They take less disk space.</i>
I designed the following test: var arrayLength=5000; object[] objArray=new object[arrayLength]; for(var x=0;x<arrayLength;x++) { objArray[x]=new
I have the following command find /var -type f -exec grep param1 {} \;
I have three condition to compare. Which one is more faster between the following
Given the following javascript (jquery) $(#username).keyup(function () { selected.username = $(#username).val(); var url =
Do you think jquery could help me get the following script work faster? Thanks!
I have the following two code blocks. Code block 1 var checkboxes = $(div.c1
In other words, which of the following would be faster, if any? List<MyClass> myList;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.