TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-13-2008, 10:43 PM   #1 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Arrow Toggling true/false variables

Hi all,

Just a quick tip for you all

I'm sure that we've all used something like the following before to toggle a bool variable:

PHP Code:
if ($myVar == true)
{
    
$myVar false;
}
else
{
    
$myVar true;

Well you'll be please to know that there is a shorter way of doing it

PHP Code:
$myVar ^= true
This does exactly the same as our if() block above - if $myVar is false, it sets it to true. If it is true, it sets it to false.

Hopefully usefull to someone

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
Devels (03-14-2008)
Old 03-13-2008, 11:56 PM   #2 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

I haven't tried this but it seems like the following would work as well. It's a little easier to understand.

PHP Code:
$var = !$var
__________________
Eric
wGEric is offline  
Reply With Quote
Old 03-14-2008, 12:06 AM   #3 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

I've always done it the way Eric showed.
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
Old 03-14-2008, 12:23 AM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

What you're doing there is utilizing the bitwise 'xor' operator. It's a little bit in depth to explain this operation in detail so we'll go with "it just works"... sort of. Since we're only working with one bit (0 or 1) the expression works something like:
PHP Code:
$myVar FALSE// (bool) false
$myVar ^= TRUE// (int) 1
$myVar ^= TRUE// (int) 0
$myVar ^= TRUE// (int) 1 
So, really we're making FALSE into (int) 1, or TRUE into (int) 0 rather than strictly converting between TRUE/FALSE. An alternative (which actually preserves boolean type) would be to go along the lines of:

PHP Code:
$myVar FALSE;   // (bool) false
$myVar = !$myVar// (bool) true
$myVar = !$myVar// (bool) false
$myVar = !$myVar// (bool) true 
As mentioned above, it keeps the boolean type rather than giving integer results and is also, for me at least, more logical (pardon the pun) in that we're saying (if $myVar is originally true) "$myVar equals not $myVar" => "$myVar equals not true" => "$myVar equals false".
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Morishani (03-29-2008)
Old 07-19-2011, 10:15 AM   #5 (permalink)
The Visitor
 
Join Date: Jul 2011
Posts: 2
Thanks: 0
NINTHTJ is on a distinguished road
Default

This is the method I usually use:

Code:
$var = ($var) ? false : true;
pretty quick and easy.
NINTHTJ is offline  
Reply With Quote
Old 07-20-2011, 06:28 AM   #6 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

Handy to know, but why are you replying to a thread that is over 3 years old !
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 07-20-2011, 08:46 AM   #7 (permalink)
The Visitor
 
Join Date: Jul 2011
Posts: 2
Thanks: 0
NINTHTJ is on a distinguished road
Default

Oops... I don't look at post dates that much XD
NINTHTJ is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:32 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design