469,290 Members | 1,913 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

Shortened "If - Then - Else"?

Hey All,

Just wondering whether there is an abbreviated "If - Then - Else"
format in PHP, much like that possible in JavaScript.

JavaScript allows an abbreviated version in the following format:
( [CONDITION]? [RESULT_THEN]: [RESULT_ELSE] )

This format is able to be included after the equal sign in a
declaration, such as:
var foo = ( field1=='content'? 'bar': 'foo' );

Any help would be appreciated - I'm just trying to reduce the number of
verbose and nested If's...

Thanks
Luke

Jul 17 '05 #1
13 23846
On 4 Jul 2005 02:36:18 -0700, lu*****@gmail.com wrote:
Just wondering whether there is an abbreviated "If - Then - Else"
format in PHP, much like that possible in JavaScript.


Have you looked in the manual yet?

--
Firefox Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #2
<lu*****@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Just wondering whether there is an abbreviated "If - Then - Else"
format in PHP, much like that possible in JavaScript.
Thanks
Luke


Use the source Luke ;) oups I ment documentation.

yes you can use short form, and in the same way as in other languages, short
form is expression while long one is statement.

rush
--
http://www.templatetamer.com/
http://www.folderscavenger.com/

Jul 17 '05 #3
*** Ewoud Dronkert wrote/escribió (Mon, 04 Jul 2005 11:42:54 +0200):
On 4 Jul 2005 02:36:18 -0700, lu*****@gmail.com wrote:
Just wondering whether there is an abbreviated "If - Then - Else"
format in PHP, much like that possible in JavaScript.


Have you looked in the manual yet?


More specifically:

http://es2.php.net/operators

I've always wondered how people manage to learn a programming language
without even looking at the documentation :)
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Jul 17 '05 #4
Thanks for the link...

Yes, I did try and search through the documentation (I am quite
familiar with the RTFM Directive), but after much stress and pulling of
hair I decided to post something here...

Thanks again.
Luke

Jul 17 '05 #5


lu*****@gmail.com wrote:
Thanks for the link...

Yes, I did try and search through the documentation (I am quite
familiar with the RTFM Directive), but after much stress and pulling of
hair I decided to post something here...


yeah, well this item is not too easy to find in tfm. In fact, I cant
find it now. But here:
< http://www.dmcinsights.com/phpmysql/ternary.php >
"The ternary--also called the trinary--operator "
--
juglesh

Jul 17 '05 #6
Why would people use the shortened version?

I find it much harder to read, and I doubt that it will shave off many
milliseconds

Jul 17 '05 #7
*** bi******@gmail.com wrote/escribió (4 Jul 2005 04:47:10 -0700):
Why would people use the shortened version?

I find it much harder to read, and I doubt that it will shave off many
milliseconds


It saves lines of code, that's all its purpose. Unless the sentence is
really complicated, I prefer to see more code at a time in screen. A
classical example (JavaScript):

function set_cookie (name, value,expires) {
var argv = set_cookie.arguments;
var argc = set_cookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;

document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Jul 17 '05 #8
In article <v3********************************@4ax.com>,
Ewoud Dronkert <fi*******@lastname.net.invalid> wrote:
Have you looked in the manual yet?


Or simply just tried it?

--
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
Jul 17 '05 #9
On 4 Jul 2005 04:47:10 -0700, "bi******@gmail.com" <bi******@gmail.com> wrote:
Why would people use the shortened version?
The ternary operator is not equivalent to an if statement; it is an
expression. So you can also avoid writing code to assign to temporaries.

$x = true;

if ($x)
{
$str = 'true';
}
else
{
$str = 'false';
}

print $str;

# versus

print $x ? 'true' : 'false';
I find it much harder to read,


Depends on the circumstance.

It's only really useful for trivial conditions.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #10
bi******@gmail.com wrote:
Why would people use the shortened version?

I find it much harder to read, and I doubt that it will shave off many
milliseconds


I actually find it easier to read.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #11
JDS
On Mon, 04 Jul 2005 02:36:18 -0700, lucanos wrote:
Just wondering whether there is an abbreviated "If - Then - Else"


Yes

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #12
JDS
On Mon, 04 Jul 2005 04:47:10 -0700, bi******@gmail.com wrote:
Why would people use the shortened version?

I find it much harder to read, and I doubt that it will shave off many
milliseconds


I will use it for things that work better (for me) on one line. For
example, I might use it to optionally stick an "s" on the end of a result
description

<? echo "There were $number item" . ( count($result) != 1 ? "s" : "" ) . "
found";?>

I actually use the ternary operator all the time. This is just one
example.

later...

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #13
JDS
On Mon, 04 Jul 2005 04:06:20 -0700, lucanos wrote:
Yes, I did try and search through the documentation (I am quite
familiar with the RTFM Directive), but after much stress and pulling of
hair I decided to post something here...


Search on "ternary". Its called the ternary operator.

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Ron Adam | last post: by
40 posts views Thread by Steve Juranich | last post: by
35 posts views Thread by David Cleaver | last post: by
12 posts views Thread by junky_fellow | last post: by
9 posts views Thread by chutsu | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.