By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

please explain or refer: $string = isset($xyz) ? $xyz : "something else";

P: n/a
"$string = isset($xyz) ? $xyz : "something else";"
Hello, someone gave code like this in another thread. I understand (by

inference) what it does, but have not found any documentation on this
type of syntax.
Any one have links to this shortuct(?) syntax and other types of
syntax?
thanks
j

Jul 17 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On 10 May 2005 12:40:31 -0700, juglesh wrote:
Any one have links to this shortuct(?) syntax and other types of
syntax?


http://php.net/operators.comparison#...arison.ternary
And more importantly: http://php.net/download-docs.php !
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #2

P: n/a
On 10 May 2005 12:40:31 -0700, juglesh wrote:
Any one have links to this shortuct(?) syntax and other types of
syntax?


http://php.net/operators.comparison#...arison.ternary
And more importantly: http://php.net/download-docs.php !
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #3

P: n/a
juglesh wrote:
"$string = isset($xyz) ? $xyz : "something else";"
Hello, someone gave code like this in another thread. I understand (by

inference) what it does, but have not found any documentation on this
type of syntax.
Any one have links to this shortuct(?) syntax and other types of
syntax?
thanks
j


Look here:

http://www.php.net/manual/en/language.expressions.php

and search for "ternary conditional operator"

$first ? $second : $third

$first is evaluated, and if true, $second is the result. otherwise
$third is the result.

To store the result in a var:
$result = ($first) ? $second : $third

Jul 17 '05 #4

P: n/a
juglesh wrote:
"$string = isset($xyz) ? $xyz : "something else";"
Hello, someone gave code like this in another thread. I understand (by

inference) what it does, but have not found any documentation on this
type of syntax.
Any one have links to this shortuct(?) syntax and other types of
syntax?
thanks
j


Look here:

http://www.php.net/manual/en/language.expressions.php

and search for "ternary conditional operator"

$first ? $second : $third

$first is evaluated, and if true, $second is the result. otherwise
$third is the result.

To store the result in a var:
$result = ($first) ? $second : $third

Jul 17 '05 #5

P: n/a
The value of $string depends on the fact if $xyz is set or not.
It's the same as:

If(isset($xyz))
{

$string = $xyz;

}
else
{

$string = "something else";

};

Jul 17 '05 #6

P: n/a
The value of $string depends on the fact if $xyz is set or not.
It's the same as:

If(isset($xyz))
{

$string = $xyz;

}
else
{

$string = "something else";

};

Jul 17 '05 #7

P: n/a
juglesh wrote:
"$string = isset($xyz) ? $xyz : "something else";"
Hello, someone gave code like this in another thread. I understand (by

inference) what it does, but have not found any documentation on this
type of syntax.


That's the ternary operator, which you'll find in a lot of languages
besides just PHP.

The above example is exactly equivalent to:

if (isset($xyz)) {
$string = $xyz;
} else {
$string = "something else";
}

In the interest of *easily readable code* I would recommend that you try
*NOT* to use the ternary much just to appear clever. Sure, it's more
compact, but it takes a couple extra seconds to figure out what's going
on VS an if/else.
Jul 17 '05 #8

P: n/a
juglesh wrote:
"$string = isset($xyz) ? $xyz : "something else";"
Hello, someone gave code like this in another thread. I understand (by

inference) what it does, but have not found any documentation on this
type of syntax.


That's the ternary operator, which you'll find in a lot of languages
besides just PHP.

The above example is exactly equivalent to:

if (isset($xyz)) {
$string = $xyz;
} else {
$string = "something else";
}

In the interest of *easily readable code* I would recommend that you try
*NOT* to use the ternary much just to appear clever. Sure, it's more
compact, but it takes a couple extra seconds to figure out what's going
on VS an if/else.
Jul 17 '05 #9

P: n/a
Jason F. wrote:
That's the ternary operator, which you'll find in a lot of languages
besides just PHP.

The above example is exactly equivalent to:

if (isset($xyz)) {
$string = $xyz;
} else {
$string = "something else";
}

In the interest of *easily readable code* I would recommend that you
try *NOT* to use the ternary much just to appear clever. Sure, it's
more compact, but it takes a couple extra seconds to figure out what's
going on VS an if/else.


It takes a couple of extra seconds (actually about .5 seconds) the first
few times. After that it's quite natural and no less confusing than the
if you constructed above.
--
I was born by Cesarean section, but you really can't tell...except that
when I leave my house, I always go out the window...
Jul 17 '05 #10

P: n/a
Jason F. wrote:
That's the ternary operator, which you'll find in a lot of languages
besides just PHP.

The above example is exactly equivalent to:

if (isset($xyz)) {
$string = $xyz;
} else {
$string = "something else";
}

In the interest of *easily readable code* I would recommend that you
try *NOT* to use the ternary much just to appear clever. Sure, it's
more compact, but it takes a couple extra seconds to figure out what's
going on VS an if/else.


It takes a couple of extra seconds (actually about .5 seconds) the first
few times. After that it's quite natural and no less confusing than the
if you constructed above.
--
I was born by Cesarean section, but you really can't tell...except that
when I leave my house, I always go out the window...
Jul 17 '05 #11

P: n/a
Andrew DeFaria wrote:
Jason F. wrote:
That's the ternary operator, which you'll find in a lot of languages
besides just PHP.

The above example is exactly equivalent to:

if (isset($xyz)) {
$string = $xyz;
} else {
$string = "something else";
}

In the interest of *easily readable code* I would recommend that you
try *NOT* to use the ternary much just to appear clever. Sure, it's
more compact, but it takes a couple extra seconds to figure out what's
going on VS an if/else.

It takes a couple of extra seconds (actually about .5 seconds) the first
few times. After that it's quite natural and no less confusing than the
if you constructed above.


The ternary always takes extra time to grok, even if you've been
[ab]using it for years. Many agree that the Best Practice(TM) is to not
use it, or to at least limit its use to very simple cases. The worst are
the "clever" nested ternaries that span multiple lines.
Jul 17 '05 #12

P: n/a
Andrew DeFaria wrote:
Jason F. wrote:
That's the ternary operator, which you'll find in a lot of languages
besides just PHP.

The above example is exactly equivalent to:

if (isset($xyz)) {
$string = $xyz;
} else {
$string = "something else";
}

In the interest of *easily readable code* I would recommend that you
try *NOT* to use the ternary much just to appear clever. Sure, it's
more compact, but it takes a couple extra seconds to figure out what's
going on VS an if/else.

It takes a couple of extra seconds (actually about .5 seconds) the first
few times. After that it's quite natural and no less confusing than the
if you constructed above.


The ternary always takes extra time to grok, even if you've been
[ab]using it for years. Many agree that the Best Practice(TM) is to not
use it, or to at least limit its use to very simple cases. The worst are
the "clever" nested ternaries that span multiple lines.
Jul 17 '05 #13

P: n/a
Jason F. wrote:
Andrew DeFaria wrote:
Jason F. wrote:
That's the ternary operator, which you'll find in a lot of languages
besides just PHP.

The above example is exactly equivalent to:

if (isset($xyz)) {
$string = $xyz;
} else {
$string = "something else";
}

In the interest of *easily readable code* I would recommend that you
try *NOT* to use the ternary much just to appear clever. Sure, it's
more compact, but it takes a couple extra seconds to figure out
what's going on VS an if/else.


It takes a couple of extra seconds (actually about .5 seconds) the
first few times. After that it's quite natural and no less confusing
than the if you constructed above.

The ternary always takes extra time to grok, even if you've been
[ab]using it for years. Many agree that the Best Practice(TM) is to not
use it, or to at least limit its use to very simple cases. The worst are
the "clever" nested ternaries that span multiple lines.


I agree with Andrew. It takes me no extra time to understand, and I
find it *very* natural. If anything, I find it *faster* to understand
than your if construct above.

You and some others may agree that you shouldn't use it. Fine. But
that doesn't make it bad. There are a lot of other programmers who find
it quite easy and natural.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #14

P: n/a
Jason F. wrote:
The ternary always takes extra time to grok, even if you've been
[ab]using it for years.
No, it doesn't. Perhaps you need a new groker! :-)
Many agree that the Best Practice(TM) is to not use it, or to at least
limit its use to very simple cases.
But we are talking about a simple case here.
The worst are the "clever" nested ternaries that span multiple lines.


And we are not talking about that! Nor did I advocate that!

--
Only in America can a pizza get to your house faster than an ambulance.

Jul 17 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.