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

后端 未结 12 1648
佛祖请我去吃肉
佛祖请我去吃肉 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 05:53

    To complement @Marko's answer, be aware that when using the dot operator for this matter, you should enclose each operation in parentheses, otherwise it will reverse the order.

    For instance, this code below will print PHP >= 7.0 required yours is 5.6.15.

    (print "PHP >= 7.0 required yours is ") . (print phpversion()) . (print "\n");
    

    While this will print 5.6.151PHP >= 7.0 required yours is 1.

    print("PHP >= 7.0 required yours is ") . print(phpversion()) . print("\n");
    

    You can also use the and operator for this to work. Like so:

    if (PHP_VERSION_ID < 70000)
        print("PHP >= 7.0 required yours is ") and
        print(phpversion()) and
        print("\n");
    

提交回复
热议问题