0

Im trying to get to grips with php (rather unsuccessfully). I keep getting tripped up on the syntax for defining and calling functions

the code ive written is

$alpha = array(1, "alpha");
$beta = array(2, "beta");

function find_best_provider($provider1, $provider2){
    if($provider1 > $provider2){
        return array($provider1[0], $provider1[1]);
    }
    else {
        return array($provider2[0], $provider2[1]);
    }
}

$winner = find_best_provider($alpha, $beta);
echo $winner;

But i keep getting this notice - Notice: Array to string conversion in /Applications/MAMP/htdocs/find_best_provier_func.php on line 17 Array

Im aware what the problem is but im not quite sure how to solve it, any helps much appreciated !

8
  • $provider1 > $provider2 that's not how you should compare two arrays Commented Feb 14, 2013 at 22:51
  • Show us the whole code of the find_best_provier_func.php file. Looks like the problem not in the code you provided Commented Feb 14, 2013 at 22:52
  • @VadimAshikhman of course the problem is here. $provider1 > $provider2 is not valid checking. Commented Feb 14, 2013 at 22:52
  • @BenM you can check whether an array is greater of another array Commented Feb 14, 2013 at 22:54
  • It doesn't have the desired output here, hence the code here will always return the else... Commented Feb 14, 2013 at 22:56

1 Answer 1

2

If you're trying to evaluate the first element in the array, try this:

function find_best_provider($provider1, $provider2)
{
    if($provider1[0] > $provider2[0])
    {
        return array($provider1[0], $provider1[1]);
    }
    else 
    {
        return array($provider2[0], $provider2[1]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.