PHP Function

Hi all :). I am new to these forums as I am taking a Web Programming Course at Kent State University for a non-major class and i am struggling!

I have to:
Write a function named “BIGGER” that accepts 2 parameters. The function will be declared this way:

function BIGGER($parameter1,$parameter2) {

My code is here below:

[php]function BIGGER($parameter1, $parameter2) {
if ($parameter1 > $parameter2) {
return parameter1;
//this is an if function that compares the two numbers
} else {
return $parameter2;
};
//the function is now over and will display the higher one
};
//whatever number is higher will appear because of the function[/php]

I believe i am being told it is wrong because of the return function is wrong, if anyone is willing to help I’d really appreciate it!

Have a good day

That’s because you’re missing the $ in $parameter1 in line 3 in my case it was line 4:

[php]<?php
function BIGGER($parameter1, $parameter2) {
if ($parameter1 > $parameter2) {
return $parameter1;
//this is an if function that compares the two numbers
} else {
return $parameter2;
};
//the function is now over and will display the higher one
};
//whatever number is higher will appear because of the function

$number1 = 10;
$number2 = 3;

$biggy1 = BIGGER($number1, $number2);

echo "This number is bigger " . $biggy1 . “!
\n”;
[/php]

You throw in arrays and then you can have functions that will do the sorting for you. Here’s something I threw together a long time ago:

[php]<?php
/* Compare batting averages */

$players = array(
32 => array(‘player’ => ‘Kelly, Don’, ‘ba’ => .245),
24 => array(‘player’ => ‘Cabrera, Miguel’, ‘ba’ => .313),
41 => array(‘player’ => ‘Martinez, Victor’, ‘ba’ => .335),
28 => array(‘player’ => ‘Martinez, J.D.’, ‘ba’ => .315)
);

echo ‘

Array as is

’. print_r($players, 1) . ‘
’;

// Sort by Player’s Name:
uasort($players, function($x, $y) {
return strcasecmp($x[‘player’], $y[‘player’]);
});

echo ‘

Array sorted by player’s name

’ . print_r($players, 1) . ‘
’;

// Sort by Player’s Batting Average:
uasort($players, function($x, $y) {
return ( $x[‘ba’] < $y[‘ba’] );
});

echo ‘

Array sorted by player’s batting average

’ . print_r($players, 1) . ‘
’;[/php]

Of course I modified the data to reflect last season’s stats. ;D

Anyways functions are powerful for they save you from doing repetitive stuff over and over again, plus they help you save in having to write a lot of code.

BTW, if I were doing this in a production setting I wouldn’t be using anonymous functions or I would be sure I wouldn’t be using them over and over for other arrays (or what have you).

Sponsor our Newsletter | Privacy Policy | Terms of Service