Jump to content

Recommended Posts

[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 :confused::shrug:

 

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

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

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

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

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.