473,725 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
+ 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=='conten t'? '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 24124
On 4 Jul 2005 02:36:18 -0700, lu*****@gmail.c om 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******** *************@g 47g2000cwa.goog legroups.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.c om 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.c om 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.argu ments;
var argc = set_cookie.argu ments.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.toGMTSt ring())) +
((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*******@last name.net.invali d> wrote:
Have you looked in the manual yet?


Or simply just tried it?

--
|\/| /| |2 |<
mehaase(at)sas( dot)upenn(dot)e du
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.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4396
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, the "while" portion of a recordset. The best way I can ask is show what I mean. http://oscarguy.mechphisto.net/awardbrowse.php If you go there and select an award (like Best Picture), leave the year field alone, and select YES and submit,...
27
3076
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 few weeks ago while trying to find the best way to form a if-elif-else block, that on a very general level, an 'also' statement might be useful. So I was wondering what others would think of it.
40
3035
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 goodness of Python whenever I talk with fellow developers, but I always hit a snag when it comes to discussing the finer points of the execution model (specifically, exceptions). Without fail, when I start talking with some of the "old-timers"...
35
2728
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 I'm pretty sure the rest of the program is working just fine. The only thing I could think might be wrong is that the if statement can only hold so many values in itself? Let me show what I'm doing: if (table001]>>5]&b&0x1f] != 0 &&
12
3230
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
37
3970
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 a function
9
2856
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 (Array == 'APPLE'){ do something; } or do I need to use a different method to check?
13
1573
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 so far: #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; string platin(string original); int main() { string bub;
9
27633
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 patterns(words). What it does is takes a string as a parameter, determines length of string, tests if the length is a perfect square, if it is then it makes a 2-d array with its length and height equal to the lengths root. If it isn't then it cuts it down to...
0
8886
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9105
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6010
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4509
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3218
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2154
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.