473,473 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 24096
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: LRW | last post by:
I'm not exactly sure how to even ask the question, and I know my terminology is not good as I'm a SQL beginner, but, here goes. I need to find a way to make an if statement within an array...or,...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
12
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
9
by: chutsu | last post by:
hi I got a simple program, and I was wondering how do you check if the string in an array = a string. For example if I put "APPLE" in array Array then how can I check it with a if statement. if...
13
compman9902
by: compman9902 | last post by:
Hello, and thank you for hwlping me with this run-in with trouble. I am making a script for pig latin transulation, and there is something wrong with the "if" statement in line 51. Here is the code...
9
by: erictheone | last post by:
Ok so what I'm trying to do is create a trans location cipher. For those among us that don't know alot about cryptography it is a method for jumbling up letters to disguise linguistic...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.