If else shortcut.

I always forget this and then I have to search for it on google. So I'm putting it here where I can remember.

The Javascript if else shortcut goes like this:

Expression?true part:false part

For example:
(actor=="kevin bacon") ? alert("My favourite Actor") : alert("who?");

The PHP if else shortcut is similar:

(myexpression) ? //do the true thing : //the false part;

For Example:

$failure = ($president == 'George W. Bush') ? 1 : 0;

The Alternative Syntax for PHP is here:
http://www.php.net/manual/en/control-structures.alternative-syntax.php
There's a good advanced Javascript tutorial here:
http://www.webmonkey.com/webmonkey/98/29/index0a.html?tw=programming




Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options