Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Use XOR in PHP

DZone's Guide to

Use XOR in PHP

· Web Dev Zone
Free Resource

Try RAD Studio for FREE!  It’s the fastest way to develop cross-platform Native Apps with flexible Cloud services and broad IoT connectivity. Start Your Trial Today!

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.

Get Your Apps to Customers 5X Faster with RAD Studio. Brought to you in partnership with Embarcadero.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}