Jump to content

[SOLVED] comparing 2 numbers and swapping values via greater/less than value.


boo_lolly

Recommended Posts

i know this is feasible i just can't remember how to do it! let's say i have 2 variables and their values will always be a positive number, except $var1 needs to be compared to $var2 to see which one is greater than the other. i need a function that will run some comparisons and the end result would be to return $var1 as the smaller of the two, and $var2 as the larger one. the trick is to only use 2 variables: $var1 and $var2. i've come up with a few things but none of them work. just a basic debugger for the foundation of this function i have:

<?php
        $first = 10;    $second = 2;
        (($first < 0) ? ($first *= -1) : (''));
        (($second < 0) ? ($second *= -1) : (''));
        echo "First = {$first}<br />\n";
        echo "Second = {$second}<br />\n";

        $second -= $first;
        echo "[\$second -= \$first] is {$second}<br />\n";

        echo "First = {$first}<br />\n";
        echo "Second = {$second}<br />\n";
?>

 

where i'm trying different variations of processing to the variables and printing out the operation in text, then giving the result. i know there's a way to do this without having to use a third variable in just a few lines of code. can anyone help me?

Link to comment
Share on other sites

If you want to know which one has the greater absolute value (offset from 0) then it's simple:

<?php
function greatestNum($first, $second) {
     return abs($first) < abs($second) ? array($second, $first) : array($first, $second);
}
print_r(greatestNum(10, -100));
?>

Should output:

Array (

  [0] => -100

  [1] => 10

)

Because -100 is further away from 0 than 10 is.

Link to comment
Share on other sites

i know how to determine which one has the greater value. what i want to do is replace the value of $var1 with the value of $var2 if the value of $var1 is greater than $var2. but, i want the original value of $var2 to be the previous value of $var1. again, without using anymore variables than $var1 and $var. make sense?

 

btw i was wondering if there was an absolute value function but i didn't think any further than that, i just wrote that one line to do it. so, thanks for illustrating that point, bud.

Link to comment
Share on other sites

You can try this:

<?php
function swapGreater(&$first, &$second) {
     list($first, $second) = $first < $second ? array($second, $first) : array($first, $second);
}
?>

But I'm not sure if it'll work in PHP 4, or even PHP 5 for that matter...as it's untested.

Link to comment
Share on other sites

that wasn't really what i was going for, but i '<' changed to '>' and it worked perfectly and i'm going to use it. hrere's the finished func:

<?php
        function swapGreater(&$first, &$second){
                $first = abs($first);   $second = abs($second);
                list($first, $second) = $first > $second ? array($second, $first) : array($first, $second);
        }
?>

 

btw i'm running PHP 4. to be honest i was look for something like:

<?php
        if($first > $second){
                $first -= $second;
                $second += $first;
        }
?>

 

or something similar. i don't know how to do it but i've seen it done before. i was just trying to see if anyone can help me understand how to do that. also, thank you for illustrating what '&' does. i've seen it every now and then in front of variables but i had no idea why people used it or what it's purpose was. i can see now it allows you to store it in an array, right? thanks again bro.

Link to comment
Share on other sites

Hey! Sorry to but in on this thread, what does += and -= do to the vars? And I've never seen PHP layed out like this:

<?php
        function swapGreater(&$first, &$second){
                $first = abs($first);   $second = abs($second);
##              list($first, $second) = $first > $second ? array($second, $first) : array($first, $second);
        }
?>

 

Specifically the line with the two hashes... Is there somewhere I can read on how to do this? It looks like a really good and solid way of doing things....

-- Nathan

Link to comment
Share on other sites

to explain your first question:

<?php
        $var += 5;
        //is the same as...
        $var = $var + 5;

        $var -= 2;
        //is the same as...
        $var = $var - 2;

        //same goes for '*' and '/'
?>

make sense?

 

and as for the hashed out line you posted. it's just another way of doing an if/else statement.

 

so these:

<?php
        $first < $second ? do_something : do_something_else;
        ($first < $second) ? (do_something) : (do_something_else);
        (($first < $second) ? (do_something) : (do_something_else));
?>

 

are all exactly the same as this:

<?php
        if($first < $second){
                do_something;
        }else{
                do_something_else;
        }
?>

 

there is no difference.

Link to comment
Share on other sites

Awesome, Thanks mate! That's really handy, I've just never seen it layed out like that. Its a lot simpler to write. But a little more complicated to read. But I guess if you know what your doing its ok and nice and neat :)

I'll let this thread get back to its original question. Sorry for hijacking :)

Thanks again..

-- Nathan

Link to comment
Share on other sites

that wasn't really what i was going for, but i '<' changed to '>' and it worked perfectly and i'm going to use it. hrere's the finished func:

<?php
        function swapGreater(&$first, &$second){
                $first = abs($first);   $second = abs($second);
                list($first, $second) = $first > $second ? array($second, $first) : array($first, $second);
        }
?>

 

btw i'm running PHP 4. to be honest i was look for something like:

<?php
        if($first > $second){
                $first -= $second;
                $second += $first;
        }
?>

 

or something similar. i don't know how to do it but i've seen it done before. i was just trying to see if anyone can help me understand how to do that. also, thank you for illustrating what '&' does. i've seen it every now and then in front of variables but i had no idea why people used it or what it's purpose was. i can see now it allows you to store it in an array, right? thanks again bro.

Actually & creates a reference to the original variable.  Here's an example:

 

<?php
$var1 = "Some Text";
$var2 = &$var1;
$var1 = "Some other text";
print $var2;
?>

While most people not knowing what & meant would expect this to print Some Text, it actually prints Some other text, since $var2 is simply a pointer to $var1

Link to comment
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.