Question

just a question about the following loop and the brackets at the end (i just started learning PHP so its likely a noob question). i wrote the following code and it didnt work:

        <?php 
        function maximum(){
        $wieviele = func_num_args();
        $arg = func_get_args();
        $first = $arg[0];

        for ($i = 1;$i < $wieviele;$i++){
        if ($first < $arg[$i])
        $first = $arg[$i];

        return $first;
        }


        }

        echo maximum(1,4,64,9);
        ?>

Now i checked it out in the book i'm reading and the solution is the following code which is nearly equal except the brackets at the end.

        <?php 
        function maximum(){
        $wieviele = func_num_args();
        $arg = func_get_args();
        $first = $arg[0];

        for ($i = 1;$i < $wieviele;$i++)
        if ($first < $arg[$i])
        $first = $arg[$i];

        return $first;



        }

        echo maximum(1,4,64,9);
        ?>

And now i would love to know why it only works without the brackets for the FOR-loop.

Thank you

Was it helpful?

Solution

When you have a control structure such as a for loop or an if statement and you don't include a set of brackets after it, only the next line is considered to be inside the control structure.

So when you have

    for ($i = 1;$i < $wieviele;$i++)
    if ($first < $arg[$i])
    $first = $arg[$i];

    return $first;

It means

    for ($i = 1;$i < $wieviele;$i++){
        if ($first < $arg[$i]){
            $first = $arg[$i];
        }
    }
    return $first;

That is, it sets $first equal to $arg[$i] when appropriate and returns once the loop is finished.

In your code, you have

    for ($i = 1;$i < $wieviele;$i++){
    if ($first < $arg[$i])
    $first = $arg[$i];

    return $first;
    }

Which is equivalent to

    for ($i = 1;$i < $wieviele;$i++){
        if ($first < $arg[$i]){
            $first = $arg[$i];
        }
        return $first;
    }

So you'll enter the first iteration of the loop, check the condition, and then return $first. That is, you return after the first iteration, not when the loop is complete.

My advice concerning omitting/including brackets is to always be as explicit as you can, to make the purpose and effect of your code as intuitive as possible.

OTHER TIPS

You need to add return $first; outside of your for loop on your first code block to make it work properly.

Demonstration

The first code didn't work because you were returning the value prematurely


Why not make it simpler ?

Use max() instead...

$array = array(1,4,64,9); //<--- Add elements to your array
echo max($array); // 64

It is reccommended to use bracket even for one line body of loops.if-statemnts.

Equivalent with bracets of this code

for ($i = 1;$i < $wieviele;$i++)
if ($first < $arg[$i])
$first = $arg[$i];

is

for ($i = 1;$i < $wieviele;$i++)
{
    if ($first < $arg[$i])
    {
        $first = $arg[$i];
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top