php `:` symbol usage

    |
  • Added:
  • |
  • In: Basic PHP

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?

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.

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

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

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

switch uses it for case 123: and default:.

:: is used to access static class members.


Search
I am...

Sajjad Hossain

I have five years of experience in web development sector. I love to do amazing projects and share my knowledge with all.
Connect Social With PHPAns
Top