Re: Boolean type forced on string assignment inside if statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Based on what I learned. I create this simple sample that can occurs
in a real world application.
This simulate a system that needs to send a mail when a flag ($mail)
is true, the system need to
check if the category is passed with the flag to know the type of mail to send.
Here is the results.
<?php
 $mail = true;
 $mail_types = array(0,1,2,3,4,5,6,7,8,9,'A','B');
 $print_it = '';
 foreach($mail_types as $mail_type){
	echo "For $mail_type:<br>";
	 if($mail & $mail_type){
		$print_it = 'email sent.<br>';
	 } else {
		$print_it = 'Mail type not defined!';
	 }
	
	 if($mail && $mail_type){
		$print_it = ('email sent.<br>' != $print_it) ? 'Only && email
sent.<br>' : NULL;
	 } else {
		$print_it =  ('email sent.<br>' == $print_it) ? 'Only & email
sent.<br>' : NULL;
	 }
	
	 echo $print_it;
 }
?>


2013/1/3 Andreas Perstinger <andipersti@xxxxxxxxx>:
> Volmar Machado <qi.volmar@xxxxxxxxx> wrote:
>>When the one of the operators were 2, the cases with "<------------"
>>returns 2 otherwise returns 0 (Or 1 when any operator is 1). And if
>>the operators are 1 and 2, return 0 too. Its curious for me.
>
> & is the bitwise and operator. You have to look at the binary
> representation of the numbers to see what is happening:
> 2 decimal is 0010 binary
> 1 decimal is 0001 binary
>
> 2 & 1 == 0010 & 0001 == 0000 == 0
>
> In your other examples you had
> 2 & 3 == 0010 & 0011 == 0010 == 2
> and
> 4 & 3 == 0100 & 0011 == 0000 == 0
>
> Does this help?
>
> Bye, Andreas
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux