php-general Digest 4 Jan 2013 07:56:05 -0000 Issue 8086
php-general Digest 4 Jan 2013 07:56:05 - Issue 8086 Topics (messages 320004 through 320030): Re: Boolean type forced on string assignment inside if statement 320004 by: Geoff Shang 320005 by: John Iliffe 320006 by: Marc Guay 320007 by: John Iliffe 320008 by: Marc Guay 320009 by: David OBrien 320010 by: Tedd Sperling 320011 by: Marc Guay 320012 by: Tedd Sperling 320013 by: Marc Guay 320014 by: Jim Lucas 320015 by: Marc Guay 320016 by: Volmar Machado 320017 by: Andreas Perstinger 320026 by: Volmar Machado 320028 by: Jim Lucas 320029 by: tamouse mailing lists 320030 by: Sebastian Krebs date problem 320018 by: Marc Fromm 320019 by: Jonathan Sundquist 320020 by: Serge Fonville 320021 by: Ken Robinson 320022 by: Marc Fromm 320023 by: Jonathan Sundquist 320024 by: Marc Fromm 320025 by: Jim Giner 320027 by: Jim Lucas Administrivia: To subscribe to the digest, e-mail: php-general-digest-subscr...@lists.php.net To unsubscribe from the digest, e-mail: php-general-digest-unsubscr...@lists.php.net To post to the list, e-mail: php-gene...@lists.php.net -- ---BeginMessage--- On Thu, 3 Jan 2013, Jim Giner wrote: The only time I use a single '=' symbol in an if statement is when I forget to use two of them! Must be my old school, old languages habits but this style of programming reminds me of the days when we used to pack empty spaces in assembler code with constants or byte-size vars in order to save memory back when memory was the most precious resource one had. The only time I'd consider doing it is if the next thing I was going to do would be check to see if the assignment itself worked. for example: if (file_handle = fopen(foo, r)) { ... } Geoff. ---End Message--- ---BeginMessage--- On Thursday 03 January 2013 10:26:39 Jim Giner wrote: On 1/2/2013 2:02 PM, Marc Guay wrote: Something else that's happening with this, which makes it a Bad Idea (tm) is that when the operator is or, as it is in my real life scenerio, the 2nd variable occasionally doesn't get populated if the first one returns true. if ($a = foo || $b = bar){ echo $a.br /.$b; } Returns foo And even worse, because I have this in a loop, what can happen is that if $b gets populated on one loop, it doesn't get reset for the next one so the data gets seriously bungled. Moral of the story: Don't be so fancy on your first day back after vacation. :) Marc You actually use statements like that in order to populate vars? Whatever happened to simple to understand, easy to maintain coding practices? The only time I use a single '=' symbol in an if statement is when I forget to use two of them! Must be my old school, old languages habits but this style of programming reminds me of the days when we used to pack empty spaces in assembler code with constants or byte-size vars in order to save memory back when memory was the most precious resource one had. I have been watching this discussion with some amusement and I recall the days mentioned by Jim very well indeed! First, did the original poster realize that he was assigning a value to the variable $a in the 'if' statement? Assuming that he did, and this is not just a typo, then remember how the if statement evaluates an OR condition; that is, if the first variable is 'true' then the true path is followed because there is no reason to go further. So the result is EXACTLY what one would expect. $a is true (ie it is not set to a 'false' value, whatever PHP uses for false) and $b is bar because that is what it is set to. Since the evaluation is within a bracket, the interior values ($a, $b) are set BEFORE the if condition is evaluated. I see neither a bug in PHP nor a variance from the expected result here. Regards, John ---End Message--- ---BeginMessage--- First, did the original poster realize that he was assigning a value to the variable $a in the 'if' statement? Hello, Yes, I did, and if you read my responses you can see that I came to the realisations you describe. I don't think that anyone suggested there was a bug. $a is true (ie it is not set to a 'false' value, whatever PHP uses for false) and $b is bar because that is what it is set to. Since the evaluation is within a bracket, the interior values ($a, $b) are set BEFORE the if condition is evaluated. Regarding this I'm a bit confused. In the case of an OR operator, $b is not bar because it follows the true path as you said earlier. Probably just a glitch in the English language. I'll file a bug for that. Marc ---End Message--- ---BeginMessage--- On Thursday 03 January 2013 11:33:22 Marc Guay wrote: First, did the
Re: [PHP] Boolean type forced on string assignment inside if statement
On 1/2/2013 2:02 PM, Marc Guay wrote: Something else that's happening with this, which makes it a Bad Idea (tm) is that when the operator is or, as it is in my real life scenerio, the 2nd variable occasionally doesn't get populated if the first one returns true. if ($a = foo || $b = bar){ echo $a.br /.$b; } Returns foo And even worse, because I have this in a loop, what can happen is that if $b gets populated on one loop, it doesn't get reset for the next one so the data gets seriously bungled. Moral of the story: Don't be so fancy on your first day back after vacation. :) Marc You actually use statements like that in order to populate vars? Whatever happened to simple to understand, easy to maintain coding practices? The only time I use a single '=' symbol in an if statement is when I forget to use two of them! Must be my old school, old languages habits but this style of programming reminds me of the days when we used to pack empty spaces in assembler code with constants or byte-size vars in order to save memory back when memory was the most precious resource one had. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
On Thu, 3 Jan 2013, Jim Giner wrote: The only time I use a single '=' symbol in an if statement is when I forget to use two of them! Must be my old school, old languages habits but this style of programming reminds me of the days when we used to pack empty spaces in assembler code with constants or byte-size vars in order to save memory back when memory was the most precious resource one had. The only time I'd consider doing it is if the next thing I was going to do would be check to see if the assignment itself worked. for example: if (file_handle = fopen(foo, r)) { ... } Geoff. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
On Thursday 03 January 2013 10:26:39 Jim Giner wrote: On 1/2/2013 2:02 PM, Marc Guay wrote: Something else that's happening with this, which makes it a Bad Idea (tm) is that when the operator is or, as it is in my real life scenerio, the 2nd variable occasionally doesn't get populated if the first one returns true. if ($a = foo || $b = bar){ echo $a.br /.$b; } Returns foo And even worse, because I have this in a loop, what can happen is that if $b gets populated on one loop, it doesn't get reset for the next one so the data gets seriously bungled. Moral of the story: Don't be so fancy on your first day back after vacation. :) Marc You actually use statements like that in order to populate vars? Whatever happened to simple to understand, easy to maintain coding practices? The only time I use a single '=' symbol in an if statement is when I forget to use two of them! Must be my old school, old languages habits but this style of programming reminds me of the days when we used to pack empty spaces in assembler code with constants or byte-size vars in order to save memory back when memory was the most precious resource one had. I have been watching this discussion with some amusement and I recall the days mentioned by Jim very well indeed! First, did the original poster realize that he was assigning a value to the variable $a in the 'if' statement? Assuming that he did, and this is not just a typo, then remember how the if statement evaluates an OR condition; that is, if the first variable is 'true' then the true path is followed because there is no reason to go further. So the result is EXACTLY what one would expect. $a is true (ie it is not set to a 'false' value, whatever PHP uses for false) and $b is bar because that is what it is set to. Since the evaluation is within a bracket, the interior values ($a, $b) are set BEFORE the if condition is evaluated. I see neither a bug in PHP nor a variance from the expected result here. Regards, John -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
First, did the original poster realize that he was assigning a value to the variable $a in the 'if' statement? Hello, Yes, I did, and if you read my responses you can see that I came to the realisations you describe. I don't think that anyone suggested there was a bug. $a is true (ie it is not set to a 'false' value, whatever PHP uses for false) and $b is bar because that is what it is set to. Since the evaluation is within a bracket, the interior values ($a, $b) are set BEFORE the if condition is evaluated. Regarding this I'm a bit confused. In the case of an OR operator, $b is not bar because it follows the true path as you said earlier. Probably just a glitch in the English language. I'll file a bug for that. Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
On Thursday 03 January 2013 11:33:22 Marc Guay wrote: First, did the original poster realize that he was assigning a value to the variable $a in the 'if' statement? Hello, Yes, I did, and if you read my responses you can see that I came to the realisations you describe. I don't think that anyone suggested there was a bug. $a is true (ie it is not set to a 'false' value, whatever PHP uses for false) and $b is bar because that is what it is set to. Since the evaluation is within a bracket, the interior values ($a, $b) are set BEFORE the if condition is evaluated. Regarding this I'm a bit confused. In the case of an OR operator, $b is not bar because it follows the true path as you said earlier. Probably just a glitch in the English language. I'll file a bug for that. Marc Hi Marc: I'm not at all sure of that. There are two things happening in parallel here: first the interior of the brackets is evaluated as necessary, in this case the $a is set to foo and the $b is set to bar. Then the exterior part of the statement is evaluated: if ($a. ). It is this last operation that results in the path selection through the code, and in this case $a is true, $b is not evaluated. mind you I'm basing this on my university basic programming course from almost 50 years ago :-) Regards, John -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
On Thu, Jan 3, 2013 at 11:49 AM, Marc Guay marc.g...@gmail.com wrote: Hi John, I just ran this: if (($a = foo) || ($b = bar)){ echo $a.br /.$b; } and it only spat out foo so I'm guessing things have changed. :) Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php From what I understood about || is once it sees a true the whole statement is regarded as true so nothing else following matters so PHP ignores everything in the conditional after it evaluates as true... and once it sees a false the whole statement is regarded as false so nothing else following matters again even the docs say short circuiting is used :) http://php.net/manual/en/language.operators.logical.php
Re: [PHP] Boolean type forced on string assignment inside if statement
On Jan 3, 2013, at 11:49 AM, Marc Guay marc.g...@gmail.com wrote: I just ran this: if (($a = foo) || ($b = bar)){ echo $a.br /.$b; } and it only spat out foo so I'm guessing things have changed. :) Marc Marc et al: I joined late into this conversation, so I may be missing the point, but you want to discus strangeness try this: ?php if (($a = 'foo') | ($b = 'bar'))// -- note the single pipe ( | ) { echo $a br $b; } else { echo 'Neither are populated'; } ? However, the above practice of using one '=' is questionable -- the following is better. ?php $a = 'foo'; $b = 'bar'; if (($a == 'foo') | ($b == 'bar')) { echo $a br $b; } else { echo 'Neither are populated'; } ? Comment out the variables to see how things work. Also change the number of pipes to see how things change. To the more accomplished programmers reading this, here's a question: What's the difference between using one pipe or two in an 'if' statement? :-) Cheers, tedd _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
Hi Tedd, A little searching enlightened me to the fact that in other languages, a single | or operator will cancel the short-circuiting so all of the evaluations are done before proceeding. However, they don't seem to exist in PHP so in your example it behaves the same as ||...? http://php.net/manual/en/language.operators.logical.php Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
On Jan 3, 2013, at 12:09 PM, David OBrien dgobr...@gmail.com wrote: From what I understood about || is once it sees a true the whole statement is regarded as true so nothing else following matters so PHP ignores everything in the conditional after it evaluates as true... and once it sees a false the whole statement is regarded as false so nothing else following matters again You are correct with regard to the double pipe ( || ). The double pipe means simply that if the first expression is true, then the second expression will not be considered. Whereas, a single pipe ( | ) means that both expressions will be evaluated. Now, I am not sure as to where that would mean anything. Can anyone provide an example where using a single pipe would produce different results than using a double pipe? IOW, why is there a difference? Cheers, tedd _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
On 01/03/2013 09:25 AM, Marc Guay wrote: Hi Tedd, A little searching enlightened me to the fact that in other languages, a single | or operator will cancel the short-circuiting so all of the evaluations are done before proceeding. However, they don't seem to exist in PHP so in your example it behaves the same as ||...? http://php.net/manual/en/language.operators.logical.php Marc In PHP | is not a logical operator, it is a bitwise operator. It is used to flip bits in binary data. Not to be used in a logical condition statement. http://php.net/manual/en/language.operators.bitwise.php -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Fwd: [PHP] Boolean type forced on string assignment inside if statement
I received the message below addressed only to me, but I believe the group could benefit. It looks like the single pipe is a bitwise operator so you will get an integer as a result (and probably other weird things to discover when using it on non-numbers). http://php.net/manual/en/language.operators.bitwise.php -- Forwarded message -- From: Volmar Machado qi.vol...@gmail.com Date: 3 January 2013 12:42 Subject: Re: [PHP] Boolean type forced on string assignment inside if statement To: Marc Guay marc.g...@gmail.com My results in a simple test: ?php $a = true; $b = false; // either null, or 0 echo ('($a | $b))' . ($a | $b) . 'br'); //1 echo ('($a || $b))' . ($a || $b) . 'br'); //1 echo ('($b | $a)' . ($b | $a) . 'br'); //1 echo ('($b || $a)' . ($b || $a) . 'br'); //1 echo ('($a | $a)'. ($a | $a) . 'br'); //1 echo ('($a || $a)' . ($a || $a) . 'br'); //1 echo ('($b | $b)' . ($b | $b) . 'br'); //0 echo ('($b || $b)' . ($b || $b) . 'br'); //false(outputs nothing) ? 2013/1/3 Marc Guay marc.g...@gmail.com: Now, I am not sure as to where that would mean anything. Can anyone provide an example where using a single pipe would produce different results than using a double pipe? If PHP had Eager operators (thanks Wikipedia), then your first example would have different output if (($a = 'foo') | ($b = 'bar')) { echo $a br $b; } else { echo 'Neither are populated'; } Would spit out: foo bar rather than just foo No? -- 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
Re: [PHP] Boolean type forced on string assignment inside if statement
2013/1/3 Marc Guay marc.g...@gmail.com: I received the message below addressed only to me, but I believe the group could benefit. It looks like the single pipe is a bitwise operator so you will get an integer as a result (and probably other weird things to discover when using it on non-numbers). http://php.net/manual/en/language.operators.bitwise.php -- Forwarded message -- From: Volmar Machado qi.vol...@gmail.com Date: 3 January 2013 12:42 Subject: Re: [PHP] Boolean type forced on string assignment inside if statement To: Marc Guay marc.g...@gmail.com My results in a simple test: ?php $a = true; $b = false; // either null, or 0 echo ('($a | $b))' . ($a | $b) . 'br'); //1 echo ('($a || $b))' . ($a || $b) . 'br'); //1 echo ('($b | $a)' . ($b | $a) . 'br'); //1 echo ('($b || $a)' . ($b || $a) . 'br'); //1 echo ('($a | $a)'. ($a | $a) . 'br'); //1 echo ('($a || $a)' . ($a || $a) . 'br'); //1 echo ('($b | $b)' . ($b | $b) . 'br'); //0 echo ('($b || $b)' . ($b || $b) . 'br'); //false(outputs nothing) ? The basic difference for another test, where conditions are both true are : ?php $a = 4; $b = 3; echo ('($a | $b))' . ($a | $b) . 'br'); //7 echo ('($a || $b))' . ($a || $b) . 'br'); //1 echo ('($b | $a)' . ($b | $a) . 'br'); //7 echo ('($b || $a)' . ($b || $a) . 'br'); //1 echo ('($a | $a)'. ($a | $a) . 'br'); //4 echo ('($a || $a)' . ($a || $a) . 'br'); //1 echo ('($b | $b)' . ($b | $b) . 'br'); //3 echo ('($b || $b)' . ($b || $b) . 'br'); //1 echo ('($a $b))' . ($a $b) . 'br'); //0 --- echo ('($a $b))' . ($a $b) . 'br'); //1 echo ('($b $a)' . ($b $a) . 'br'); //0 --- echo ('($b $a)' . ($b $a) . 'br'); //1 echo ('($a $a)'. ($a $a) . 'br'); //4 echo ('($a $a)' . ($a $a) . 'br'); //1 echo ('($b $b)' . ($b $b) . 'br'); //3 echo ('($b $b)' . ($b $b) . 'br'); //1 ? ?php $a = 2; $b = 3; echo ('($a | $b))' . ($a | $b) . 'br'); //3 echo ('($a || $b))' . ($a || $b) . 'br'); //1 echo ('($b | $a)' . ($b | $a) . 'br'); //3 echo ('($b || $a)' . ($b || $a) . 'br'); //1 echo ('($a | $a)'. ($a | $a) . 'br'); //2 echo ('($a || $a)' . ($a || $a) . 'br'); //1 echo ('($b | $b)' . ($b | $b) . 'br'); //3 echo ('($b || $b)' . ($b || $b) . 'br'); //1 echo ('($a $b))' . ($a $b) . 'br'); //2 echo ('($a $b))' . ($a $b) . 'br'); //1 echo ('($b $a)' . ($b $a) . 'br'); //2 - echo ('($b $a)' . ($b $a) . 'br'); //1 echo ('($a $a)'. ($a $a) . 'br'); //2 echo ('($a $a)' . ($a $a) . 'br'); //1 echo ('($b $b)' . ($b $b) . 'br'); //3 echo ('($b $b)' . ($b $b) . 'br'); //1 ? 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
Volmar Machado qi.vol...@gmail.com 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 == == 0 In your other examples you had 2 3 == 0010 0011 == 0010 == 2 and 4 3 == 0100 0011 == == 0 Does this help? Bye, Andreas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] date problem
I am comparing to dates. define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( date(m/d/Y, strtotime($jes)) date(m/d/Y, strtotime(WSOFFBEGIN)) ) { $error = MUST begin after . WSOFFBEGIN . \n; } I cannot figure out why the $error is being assigned inside the if statement, since the statement should be false. 01/03/2012 is not less than 09/16/2012. Marc
Re: [PHP] date problem
1/3/2012 is in fact less then 9/16/2012. On Thu, Jan 3, 2013 at 3:57 PM, Marc Fromm marc.fr...@wwu.edu wrote: I am comparing to dates. define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( date(m/d/Y, strtotime($jes)) date(m/d/Y, strtotime(WSOFFBEGIN)) ) { $error = MUST begin after . WSOFFBEGIN . \n; } I cannot figure out why the $error is being assigned inside the if statement, since the statement should be false. 01/03/2012 is not less than 09/16/2012. Marc
Re: [PHP] date problem
Hi. date returns a string You should compare a different type for bigger/smaller than HTH Kind regards/met vriendelijke groet, Serge Fonville http://www.sergefonville.nl Convince Microsoft! They need to add TRUNCATE PARTITION in SQL Server https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table 2013/1/3 Marc Fromm marc.fr...@wwu.edu I am comparing to dates. define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( date(m/d/Y, strtotime($jes)) date(m/d/Y, strtotime(WSOFFBEGIN)) ) { $error = MUST begin after . WSOFFBEGIN . \n; } I cannot figure out why the $error is being assigned inside the if statement, since the statement should be false. 01/03/2012 is not less than 09/16/2012. Marc
Re: [PHP] date problem
At 04:57 PM 1/3/2013, Marc Fromm wrote: I am comparing to dates. define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( date(m/d/Y, strtotime($jes)) date(m/d/Y, strtotime(WSOFFBEGIN)) ) { $error = MUST begin after . WSOFFBEGIN . \n; } I cannot figure out why the $error is being assigned inside the if statement, since the statement should be false. 01/03/2012 is not less than 09/16/2012. You shouldn't be comparing the date strings, but the UNIX timestamp values: define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( strtotime($jes) strtotime(WSOFFBEGIN) ) { $error = MUST begin after . WSOFFBEGIN . \n; } Ken -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] date problem
Thanks for the reply. Every example on comparing dates in PHP that I found uses the strtotime function which I am using. What other type can I use? When is this example below supposed to work? // your first date coming from a mysql database (date fields) $dateA = '2008-03-01 13:34'; // your second date coming from a mysql database (date fields) $dateB = '2007-04-14 15:23'; if(strtotimehttp://www.php.net/strtotime($dateA) strtotimehttp://www.php.net/strtotime($dateB)){ // bla bla } Thanks From: Serge Fonville [mailto:serge.fonvi...@gmail.com] Sent: Thursday, January 03, 2013 2:05 PM To: Marc Fromm Cc: php-general@lists.php.net Subject: Re: [PHP] date problem Hi. date returns a string You should compare a different type for bigger/smaller than HTH Kind regards/met vriendelijke groet, Serge Fonville http://www.sergefonville.nl Convince Microsoft! They need to add TRUNCATE PARTITION in SQL Server https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table 2013/1/3 Marc Fromm marc.fr...@wwu.edumailto:marc.fr...@wwu.edu I am comparing to dates. define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( date(m/d/Y, strtotime($jes)) date(m/d/Y, strtotime(WSOFFBEGIN)) ) { $error = MUST begin after . WSOFFBEGIN . \n; } I cannot figure out why the $error is being assigned inside the if statement, since the statement should be false. 01/03/2012 is not less than 09/16/2012. Marc
Re: [PHP] date problem
Marc, When you take a date and do a strtotime you are converting it to an int which you can compare to each other much easier. So for your above example you would be best doing. define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( strtotime($jes) strtotime(WSOFFBEGIN) ) { $error = MUST begin after . WSOFFBEGIN . \n; } On Thu, Jan 3, 2013 at 4:12 PM, Marc Fromm marc.fr...@wwu.edu wrote: Thanks for the reply. Every example on comparing dates in PHP that I found uses the strtotime function which I am using. What other type can I use? When is this example below supposed to work? // your first date coming from a mysql database (date fields) $dateA = '2008-03-01 13:34'; // your second date coming from a mysql database (date fields) $dateB = '2007-04-14 15:23'; if(strtotimehttp://www.php.net/strtotime($dateA) strtotime http://www.php.net/strtotime($dateB)){ // bla bla } Thanks From: Serge Fonville [mailto:serge.fonvi...@gmail.com] Sent: Thursday, January 03, 2013 2:05 PM To: Marc Fromm Cc: php-general@lists.php.net Subject: Re: [PHP] date problem Hi. date returns a string You should compare a different type for bigger/smaller than HTH Kind regards/met vriendelijke groet, Serge Fonville http://www.sergefonville.nl Convince Microsoft! They need to add TRUNCATE PARTITION in SQL Server https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table 2013/1/3 Marc Fromm marc.fr...@wwu.edumailto:marc.fr...@wwu.edu I am comparing to dates. define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( date(m/d/Y, strtotime($jes)) date(m/d/Y, strtotime(WSOFFBEGIN)) ) { $error = MUST begin after . WSOFFBEGIN . \n; } I cannot figure out why the $error is being assigned inside the if statement, since the statement should be false. 01/03/2012 is not less than 09/16/2012. Marc
RE: [PHP] date problem
Thanks Jonathan. I removed the date() syntax function and it works. From: Jonathan Sundquist [mailto:jsundqu...@gmail.com] Sent: Thursday, January 03, 2013 2:16 PM To: Marc Fromm Cc: Serge Fonville; php-general@lists.php.net Subject: Re: [PHP] date problem Marc, When you take a date and do a strtotime you are converting it to an int which you can compare to each other much easier. So for your above example you would be best doing. define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( strtotime($jes) strtotime(WSOFFBEGIN) ) { $error = MUST begin after . WSOFFBEGIN . \n; } On Thu, Jan 3, 2013 at 4:12 PM, Marc Fromm marc.fr...@wwu.edumailto:marc.fr...@wwu.edu wrote: Thanks for the reply. Every example on comparing dates in PHP that I found uses the strtotime function which I am using. What other type can I use? When is this example below supposed to work? // your first date coming from a mysql database (date fields) $dateA = '2008-03-01 13:34'; // your second date coming from a mysql database (date fields) $dateB = '2007-04-14 15:23'; if(strtotimehttp://www.php.net/strtotime($dateA) strtotimehttp://www.php.net/strtotime($dateB)){ // bla bla } Thanks From: Serge Fonville [mailto:serge.fonvi...@gmail.commailto:serge.fonvi...@gmail.com] Sent: Thursday, January 03, 2013 2:05 PM To: Marc Fromm Cc: php-general@lists.php.netmailto:php-general@lists.php.net Subject: Re: [PHP] date problem Hi. date returns a string You should compare a different type for bigger/smaller than HTH Kind regards/met vriendelijke groet, Serge Fonville http://www.sergefonville.nl Convince Microsoft! They need to add TRUNCATE PARTITION in SQL Server https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table 2013/1/3 Marc Fromm marc.fr...@wwu.edumailto:marc.fr...@wwu.edumailto:marc.fr...@wwu.edumailto:marc.fr...@wwu.edu I am comparing to dates. define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( date(m/d/Y, strtotime($jes)) date(m/d/Y, strtotime(WSOFFBEGIN)) ) { $error = MUST begin after . WSOFFBEGIN . \n; } I cannot figure out why the $error is being assigned inside the if statement, since the statement should be false. 01/03/2012 is not less than 09/16/2012. Marc
Re: [PHP] date problem
On 1/3/2013 5:22 PM, Marc Fromm wrote: Thanks Jonathan. I removed the date() syntax function and it works. From: Jonathan Sundquist [mailto:jsundqu...@gmail.com] Sent: Thursday, January 03, 2013 2:16 PM To: Marc Fromm Cc: Serge Fonville; php-general@lists.php.net Subject: Re: [PHP] date problem Marc, When you take a date and do a strtotime you are converting it to an int which you can compare to each other much easier. So for your above example you would be best doing. define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( strtotime($jes) strtotime(WSOFFBEGIN) ) { $error = MUST begin after . WSOFFBEGIN . \n; } On Thu, Jan 3, 2013 at 4:12 PM, Marc Fromm marc.fr...@wwu.edumailto:marc.fr...@wwu.edu wrote: Thanks for the reply. Every example on comparing dates in PHP that I found uses the strtotime function which I am using. What other type can I use? When is this example below supposed to work? // your first date coming from a mysql database (date fields) $dateA = '2008-03-01 13:34'; // your second date coming from a mysql database (date fields) $dateB = '2007-04-14 15:23'; if(strtotimehttp://www.php.net/strtotime($dateA) strtotimehttp://www.php.net/strtotime($dateB)){ // bla bla } Thanks From: Serge Fonville [mailto:serge.fonvi...@gmail.commailto:serge.fonvi...@gmail.com] Sent: Thursday, January 03, 2013 2:05 PM To: Marc Fromm Cc: php-general@lists.php.netmailto:php-general@lists.php.net Subject: Re: [PHP] date problem Hi. date returns a string You should compare a different type for bigger/smaller than HTH Kind regards/met vriendelijke groet, Serge Fonville http://www.sergefonville.nl Convince Microsoft! They need to add TRUNCATE PARTITION in SQL Server https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table 2013/1/3 Marc Fromm marc.fr...@wwu.edumailto:marc.fr...@wwu.edumailto:marc.fr...@wwu.edumailto:marc.fr...@wwu.edu I am comparing to dates. define('WSOFFBEGIN','09/16/2012'); $jes = 01/03/2012; if ( date(m/d/Y, strtotime($jes)) date(m/d/Y, strtotime(WSOFFBEGIN)) ) { $error = MUST begin after . WSOFFBEGIN . \n; } I cannot figure out why the $error is being assigned inside the if statement, since the statement should be false. 01/03/2012 is not less than 09/16/2012. Marc And hopefully put quotes around 01/03/2012. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
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 andiper...@gmail.com: Volmar Machado qi.vol...@gmail.com 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 == == 0 In your other examples you had 2 3 == 0010 0011 == 0010 == 2 and 4 3 == 0100 0011 == == 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
Re: [PHP] date problem
On 01/03/2013 01:57 PM, Marc Fromm wrote: $jes = 01/03/2012; # php -r echo 01/03/2012; 0.00016567263088138 You might want to put quotes around that value so it is actually a string and does not get evaluated. -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
On 01/03/2013 11:43 AM, Andreas Perstinger wrote: is the bitwise and operator. So is a single pipe. http://php.net/manual/en/language.operators.bitwise.php -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Boolean type forced on string assignment inside if statement
2013/1/4 tamouse mailing lists tamouse.li...@gmail.com Bit operators and | are NOT and should NEVER be confused with Logical operators and ||: ?php /** * Bit operators in PHP */ $format = Decimal: %2d Binary: %4b\n; $a = 4; $b = 6; echo Variable \$a:\n; printf($format, $a, $a); echo Variable \$b:\n; printf($format, $b, $b); $c = $a | $b; echo Result of OR bit operator\n; printf($format, $c, $c); $c = $a $b; echo Result of AND bit operator\n; printf($format, $c, $c); ? OUTPUT: --- Variable $a: Decimal: 4 Binary: 100 Variable $b: Decimal: 6 Binary: 110 Result of OR bit operator Decimal: 6 Binary: 110 Result of AND bit operator Decimal: 4 Binary: 100 Bit operators are not comparing values, they're COMBINING values. Technically spoken they're comparing bits, whereas boolean operators does the same, but treaten every value as a single bit. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- github.com/KingCrunch