PHP - If/else, for, foreach, while - without curly braces?

后端 未结 12 1659
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 05:31

Something that really would like to know but never found out are shortcuts in PHP.

I am currently coding a function with a foreach loop with just a sing

12条回答
  •  一向
    一向 (楼主)
    2020-11-27 06:12

    The answer is easy. This is the same in C/C++.

    if, if/else, if/else if/ else and loops => If the next statement is one single line, you can omit the curly braces.

    Ex:

    for($i= 0; $i<=100; $i++)
    {
        if($i % 7 == 0)
        {   
            echo $i . "
    "; } }

    It can also be written in this way:

    for($i= 0; $i<=100; $i++) if($i % 7 == 0) echo $i . "
    ";

提交回复
热议问题