DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Use XOR in PHP

Use XOR in PHP

John Esposito user avatar by
John Esposito
·
Nov. 05, 11 · Web Dev Zone · Interview
Like (0)
Save
Tweet
5.19K Views

Join the DZone community and get the full member experience.

Join For Free

Back in logic 101 you probably learned that English 'or' means, sadly, two different things: inclusive ('spinach or romaine is okay') and exclusive ('to be or not to be').

In circuits 101 (or cs 101, or whatever), you might have been impressed by how easy it is to construct basic logic gates with transistors.

Sometimes, though -- as programming becomes more and more abstract from machine code, to some developers' chagrin (Linus' too) -- developers with a deadline slip comfortably into a language, fall in love with the subset of its features most useful for a few but ultra-common web applications, and find themselves stuck in an active coding vocabulary quite a bit smaller than the set of instructions they passively understand. (True of natural languages too, unsurprisingly.)

So if you code, and code in PHP, then you probably understand XOR, and know that XOR is an operator in PHP. But I didn't, because I learned PHP from existing code, not a book -- and existing code doesn't seem to contain many XORs.

If this looks familiar:

$dir1 = 'download_A';
$dir2 = 'download_B';
 
if((is_dir($dir1) && !is_dir($dir2)) || (!is_dir($dir1) && is_dir($dir2))) {
    echo "Only one Directory exists!";
}

then you might want to check out this post, which shows you how to simplify this code using XOR, and reminds you that there's never a good reason not to.

By the way: post a comment here if you like low-level code tips like this -- or if you don't. If you're a regular DZone reader, and could use a weekly coding tip right here on this site, I have some more ready for you.

PHP

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Refactor Switch to a One-Liner
  • 12 Modern CSS Techniques For Older CSS Problems
  • MongoDB vs. DynamoDB Head-to-Head: Which Should You Choose?
  • Top ALM Tools and Solutions Providers

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo