0

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

I know only of:

if(expression):
    // do Something
endif;

while(expression):
    // and others: `for` `foreach` etc.
endwhile;

Are there any other uses?

Amber
  • 507,862
  • 82
  • 626
  • 550
tomsseisums
  • 13,168
  • 19
  • 83
  • 145
  • 1
    I'm not a fan of this syntax and haven't seen it in any other places than i view "templates" – baloo May 18 '12 at 16:32
  • all control structures may use this alternative syntax. it is widely used when PHP is embedded within HTML views. typically I have seen people use normal syntax in their business logic (controllers and models), and alternative syntax within views. I believe it helps see the ends of control structures better when intermixed into HTML, therefore making it easier to read. – dqhendricks May 18 '12 at 16:34

4 Answers4

5

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.

http://us3.php.net/manual/en/control-structures.alternative-syntax.php

Amber
  • 507,862
  • 82
  • 626
  • 550
  • -1'd because the answer originally was *just* a link and I'm in a bad mood and hate FGITW, will rescind although the answer is still far from complete. @dqhendricks – Wesley Murch May 18 '12 at 16:30
  • 1
    on this day in history: i learned what 'FGITW' is. – Kristian May 18 '12 at 16:40
  • @WesleyMurch For the record, I don't try to FGITW; I just happen to occasionally ask people to RTFM when there's a manual page specifically devoted to the concept. – Amber May 18 '12 at 16:55
  • Well you can cast a vote to close as dupes of these ones if you're on the RTFM tip: http://stackoverflow.com/questions/2908095/what-is-this-in-php, http://stackoverflow.com/questions/4747761/what-does-this-mean-in-php Found by Googling "`php : symbol`" (once again, I'm a little pissy today, sry) – Wesley Murch May 18 '12 at 17:01
4

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()

Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
3

switch uses it for case 123: and default:.

:: is used to access static class members.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
2

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

http://php.net/manual/en/language.operators.comparison.php