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

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

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.

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}