0

I am looking to return an integer with a value of 50 or value of 25. Is this the correct way to do this or should I create a variable $temp1=50 and $temp2=25 and return the variable instead of just returning 50 and 25.

function somefunction($length)
{

    if ($length > 50) 
    {
        return 50;
    } else {
        return 25;
    }
}

Sorry if duplicate, I looked.

6 Answers 6

3

It is perfectly fine the way you're doing it. Assigning a value to a variable to only return it makes no sense really.
As a better version to your alternative, for some more complicated cases, where you'd eventually need to return a variable, you could use only one variable instead of two as you suggested. Something more like

function somefunction($length)
{
    $myVar = 0;

    if ($length > 50) {
        $myVar = 50;
    } 
    else {
        $myVar = 25;
    }

    return $myVar;
}
Sign up to request clarification or add additional context in comments.

Comments

3

It is not necessary to assign a variable before. Just write

return ($length > 50 ? 50 : 25);

1 Comment

It is equally not necessary to put round brackets around that expression.
2

Assigning to a variable before returning is pointless. You're returning a value. 50 is a perfectly good value by itself, it does not need to be assigned to a variable before being returned.

In other words: you're doing it right already.

Comments

0

It depends on what you want to do :

if you just want to return a constant or if you want to parameter this constant.

in a case of a constant, for readability, you can name them :

define('RETURN_CONSTANT_SUP',50);
define('RETURN_CONSTANT_INF',25);

function somefunction($length)
{

if ($length > RETURN_CONSTANT_SUP) 
{
    return RETURN_CONSTANT_SUP;
} else {
    return RETURN_CONSTANT_INF;
}
}

So you do it the right way, you can just use constant if, one day, you want to reuse those values.

Comments

0

I'd say the problem with your function is less the return but the input.

Anyway, your question is a bit theoretic. Because both returning a constant integer value as well as a variable would work. It wouldn't even make much of a difference. See the following example:

function somefunction($length)
{
    return 25 + 25 * ($length > 50);
}

The problem with the code is that you have written it only for these specific values. So not using a variable can be a sign that the code is limited. But less because of the return and more because of the flow.

function integer_top_cut_drop($value, $top, $default)
{
    $value   = (int) $value;
    $top     = (int) $top;
    $default = (int) $default;

    if ($value > $top) 
    {
        return $top;
    }

    return $default;
}

As this function shows, there are no numbers in there, but only variables. It pretty much does what your existing function does, but everything is variable. The numbers with their constant values have been removed and are now being passed as parameters:

function somefunction($length)
{
    return integer_top_cut_drop($length, 50, 25);
}

So you normally never are concerned whether your return a variable, a constant, some other kind of expression or another functions return value.

More important is, that your code does what it has to do and you do it in a useful way.

That could mean in your code that your function already is totally fine.

But consider you enable yourself that you can re-use common parts, e.g. like shown here with a new function. That might be a bit over the top for your example, but just to show the picture.

A function can preserve the logic, so you don't need to write that part more than once. It also reduces the size of the other parts of the code using that function.

1 Comment

Multiplying times a conditional... oh the pain, the rest I like though.
0

There is no correct way to do this.

This question is about style and preferences and you won't be able to get a definitive answer.
Just use what you like best (unless you are working in a team of course, in which case you should adapt the team's coding styleguide).

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.