Zane Posted November 27, 2009 Share Posted November 27, 2009 [ot]It's been awhile since I've asked a question in this board. Just goes to show you that you just can't know it all.[/ot] So I was digging through some code when I came across the What is it's purpose. I want to say it's a reverse assignment operator but that just make entirely no sense. Just for the clueless ones reading I'll elaborate with code $someArray = array ( 'keyOne' => "the value", 'keyTwo' => "the value" ); or this foreach ($array as $key=> $value ) while ($key $blah -= $key; $str.= $value; } Link to comment https://forums.phpfreaks.com/topic/183103-and/ Share on other sites More sharing options...
trq Posted November 27, 2009 Share Posted November 27, 2009 Your first example is invalid. Your second says while $key is less than or equal to $blah. Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966335 Share on other sites More sharing options...
Zane Posted November 27, 2009 Author Share Posted November 27, 2009 good god I need to go to sleep. How did I not know that? Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966336 Share on other sites More sharing options...
trq Posted November 27, 2009 Share Posted November 27, 2009 I have no idea. It is a very common comparison operator. Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966338 Share on other sites More sharing options...
oni-kun Posted November 27, 2009 Share Posted November 27, 2009 "=>" is a key assignment. "'keyOne' => "the value"," is like an ARROW. (That's where you were confused) "<=" is an operator, such as.. < OR =, as in 'LESS or EQUAL TO', note operators are before the equals in this math. Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966339 Share on other sites More sharing options...
Mchl Posted November 27, 2009 Share Posted November 27, 2009 Your first example is invalid. It's not. It will evaluate to false. Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966340 Share on other sites More sharing options...
Zane Posted November 27, 2009 Author Share Posted November 27, 2009 I was reading through the SMF code is where I got it... that probably explains my confusion. I still feel completely retarded for posting this topic now.. is like an ARROW. (That's where you were confused) Yeah.. that's it. Lesson of the day: Always reread your OP at least 5 more times before ever posting it. Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966343 Share on other sites More sharing options...
trq Posted November 27, 2009 Share Posted November 27, 2009 Your first example is invalid. It's not. It will evaluate to false. Ah. It actually evaluates to true. I assume because the string "the value" is considered to be less than "something else". Strange thing is, if you swap them. ("something else" <= "the value") it evaluates to null (which is of course also considered false). Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966352 Share on other sites More sharing options...
salathe Posted November 27, 2009 Share Posted November 27, 2009 Ah. It actually evaluates to true. I assume because the string "the value" is considered to be less than "something else". Strange thing is, if you swap them. ("something else" <= "the value") it evaluates to null (which is of course also considered false). It actually doesn't, Mchl was right in saying "the value" <= "something else" evaluates to FALSE. If you swap them ("something else" <= "the value"), that evaluates to TRUE. I'm not sure how you arrived at the FALSE and NULL values. Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966360 Share on other sites More sharing options...
trq Posted November 27, 2009 Share Posted November 27, 2009 Ah. It actually evaluates to true. I assume because the string "the value" is considered to be less than "something else". Strange thing is, if you swap them. ("something else" <= "the value") it evaluates to null (which is of course also considered false). It actually doesn't, Mchl was right in saying "the value" <= "something else" evaluates to FALSE. If you swap them ("something else" <= "the value"), that evaluates to TRUE. I'm not sure how you arrived at the FALSE and NULL values. I was using "a" <= "aa" in my tests. But now, well, its an even weirder result. #!/usr/bin/php <?php $a = array( 1 => 'a' <= 'aa', 2 => 'aa' <= 'a', 3 => 'the value' <= 'something else', 4 => 'something else' <= 'the value' ); var_dump($a); array(4) { [1]=> bool(true) [2]=> bool(false) [3]=> bool(false) [4]=> bool(true) } Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966586 Share on other sites More sharing options...
Mchl Posted November 27, 2009 Share Posted November 27, 2009 Weirder? Why? I would think that's pretty common algorithm for string comparison. Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966601 Share on other sites More sharing options...
trq Posted November 27, 2009 Share Posted November 27, 2009 Weirder? Why? I would think that's pretty common algorithm for string comparison. What is? I can't see the pattern. Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966625 Share on other sites More sharing options...
Mchl Posted November 27, 2009 Share Posted November 27, 2009 Something like this function compareStrings($strA,$strB) { $A = false; //strA is larger $B = false; //strB is larger $toCompare = max(strlen($strA),strlen($strB)) for ($i = 0; $i < $toCompare, $i++) { //first compare character-to-character as long as both strings have characters ;P if (ord($strA[$i]) > ord($strB[$i]) { $A = true; break; } elseif (ord($strA[$i]) < ord($strB[$i]) { $B = true; break; } if (!$A && !$B) { //if strings were equal to this moment, then the one having more character is 'larger' if(strlen($strA) >= strlen($strB)) { $A = true; } if(strlen($strA) <= strlen($strB)) { $B = true; } } if($A && $B) return 0; if($A && !$B) return 1; if(!$A && $B) return -1; if(!$A && !$B) return 'WTF'; //this should not happen } } Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966653 Share on other sites More sharing options...
trq Posted November 27, 2009 Share Posted November 27, 2009 Had to fix a few things and changed the return values somewhat. #!/usr/bin/php <?php function compareStrings($strA,$strB) { $A = false; //strA is larger $B = false; //strB is larger $toCompare = max(strlen($strA),strlen($strB)); for ($i = 0; $i < $toCompare; $i++) { //first compare character-to-character as long as both strings have characters ;P if (ord($strA[$i]) > ord($strB[$i])) { $A = true; break; } elseif (ord($strA[$i]) < ord($strB[$i])) { $B = true; break; } if (!$A && !$B) { //if strings were equal to this moment, then the one having more character is 'larger' if(strlen($strA) >= strlen($strB)) { $A = true; } if(strlen($strA) <= strlen($strB)) { $B = true; } } if($A && $B) return "false"; if($A && !$B) return "true"; if(!$A && $B) return "false"; if(!$A && !$B) return 'error'; //this should not happen } } <?php echo compareStrings('a', 'aa');?> // returns 'false' <?php echo compareStrings('the value', 'something else');?> // returns nothing at all Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966679 Share on other sites More sharing options...
.josh Posted November 28, 2009 Share Posted November 28, 2009 @OP: HAHA FAIL (sorry, had to rub it in) Weirder? Why? I would think that's pretty common algorithm for string comparison. What is? I can't see the pattern. when you compare one string to another like that, php converts both strings to numbers. Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966715 Share on other sites More sharing options...
Mchl Posted November 28, 2009 Share Posted November 28, 2009 Had to fix a few things and changed the return values somewhat. I've never even tried to run it, so that explains syntax errors (yes it does, I tell you!) As for the return values, I made them in accordance to callback used in usort Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966874 Share on other sites More sharing options...
Daniel0 Posted November 28, 2009 Share Posted November 28, 2009 Weirder? Why? I would think that's pretty common algorithm for string comparison. What is? I can't see the pattern. It's called lexicographical order. Link to comment https://forums.phpfreaks.com/topic/183103-and/#findComment-966903 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.