364,088 Members | 5404 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Shortened "If - Then - Else"?

lucanos@gmail.com
P: n/a
lucanos@gmail.com
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
Share this Question
Share on Google+
13 Replies


Ewoud Dronkert
P: n/a
Ewoud Dronkert
On 4 Jul 2005 02:36:18 -0700, lucanos@gmail.com wrote:[color=blue]
> Just wondering whether there is an abbreviated "If - Then - Else"
> format in PHP, much like that possible in JavaScript.[/color]

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

rush
P: n/a
rush
<lucanos@gmail.com> wrote in message
news:1120469778.415143.13600@g47g2000cwa.googlegro ups.com...[color=blue]
> Just wondering whether there is an abbreviated "If - Then - Else"
> format in PHP, much like that possible in JavaScript.
> Thanks
> Luke[/color]

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

Alvaro G Vicario
P: n/a
Alvaro G Vicario
*** Ewoud Dronkert wrote/escribió (Mon, 04 Jul 2005 11:42:54 +0200):[color=blue]
> On 4 Jul 2005 02:36:18 -0700, lucanos@gmail.com wrote:[color=green]
>> Just wondering whether there is an abbreviated "If - Then - Else"
>> format in PHP, much like that possible in JavaScript.[/color]
>
> Have you looked in the manual yet?[/color]

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

lucanos@gmail.com
P: n/a
lucanos@gmail.com
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

juglesh
P: n/a
juglesh


lucanos@gmail.com wrote:[color=blue]
> 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...[/color]

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

binderup@gmail.com
P: n/a
binderup@gmail.com
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

Alvaro G Vicario
P: n/a
Alvaro G Vicario
*** binderup@gmail.com wrote/escribió (4 Jul 2005 04:47:10 -0700):[color=blue]
> Why would people use the shortened version?
>
> I find it much harder to read, and I doubt that it will shave off many
> milliseconds[/color]

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

Mark Haase
P: n/a
Mark Haase
In article <v31ic1144f42a6u31lggpptcbfju234iv7@4ax.com>,
Ewoud Dronkert <firstname@lastname.net.invalid> wrote:
[color=blue]
> Have you looked in the manual yet?[/color]

Or simply just tried it?

--
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
Jul 17 '05 #9

Andy Hassall
P: n/a
Andy Hassall
On 4 Jul 2005 04:47:10 -0700, "binderup@gmail.com" <binderup@gmail.com> wrote:
[color=blue]
>Why would people use the shortened version?[/color]

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';
[color=blue]
>I find it much harder to read,[/color]

Depends on the circumstance.

It's only really useful for trivial conditions.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #10

Jerry Stuckle
P: n/a
Jerry Stuckle
binderup@gmail.com wrote:[color=blue]
> Why would people use the shortened version?
>
> I find it much harder to read, and I doubt that it will shave off many
> milliseconds
>[/color]

I actually find it easier to read.

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

JDS
P: n/a
JDS
On Mon, 04 Jul 2005 02:36:18 -0700, lucanos wrote:
[color=blue]
> Just wondering whether there is an abbreviated "If - Then - Else"[/color]

Yes

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

Jul 17 '05 #12

JDS
P: n/a
JDS
On Mon, 04 Jul 2005 04:47:10 -0700, binderup@gmail.com wrote:
[color=blue]
> Why would people use the shortened version?
>
> I find it much harder to read, and I doubt that it will shave off many
> milliseconds[/color]

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 | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #13

JDS
P: n/a
JDS
On Mon, 04 Jul 2005 04:06:20 -0700, lucanos wrote:
[color=blue]
> 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...[/color]

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

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

Jul 17 '05 #14

Post your reply

Help answer this question



Didn't find the answer to your PHP question?

You can also browse similar questions: PHP shortened if