php `:` symbol usage

  • Added:
  • |
  • In: Basic PHP

I'm wondering, what are the cases for : use?

I know only of:

    // do Something

    // and others: `for` `foreach` etc.

Are there any other uses?

This Question Has 4 Answeres | Orginal Question | jolt

Ternary conditions: ($a == $b) ? true : false.
Static calls inside a class: self::$a.
Static methods calls: MyClass::MyMethod(),
Static variables inside a class: MyClass::MyVariable
Parent methods calls: parent::hello()

PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.

It is also used as part of the ternary operator "?:".

switch uses it for case 123: and default:.

:: is used to access static class members.

Meet with owner

Sajjad Hossain

Hey, I am Sajjad, working in web development sector since 2012. I love to do amazing things. Let's do a project together.
Connect Social With PHPAns