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

Home Posts Topics Members FAQ

Ternary Operator Problem

[PHP]
$sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
$_GET['sortBy'] : 1;
[/PHP]

This script should set $sortBy to the $_POST or the $_GET or the
default of 1 in one line.

I do not want to have to do this:

[PHP]
if ($_POST['sortBy']) {
$sortBy = $_POST['sortBy'];
} elseif ($_GET['sortBy']) {
$sortBy = $_GET['sortBy'];
} else {
$sortBy = 1;
}
[/PHP]

This is too bloated for the 2000-line script I already have, and I
want to master ternary operators as I have never done so in 7 years.

I simply want the ternary operator equivalent of:

if - elseif - else
NOT

If - else
as the default..

Thanx
Phil
Jul 17 '05 #1
7 2139
["Followup-To:" header set to comp.lang.php.]
Phil Powell wrote:
[PHP]
$sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
$_GET['sortBy'] : 1;
[/PHP]


Try more parenthesis:

$c1 = isset($_POST['sortBy']);
$c2 = isset($_GET['sortBy']);

$sortBy = ($c1) ? ($_POST['sortBy']) : ( ($c2) ? ($_GET['sortBy']) : (1) );

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2
In article <1c**************************@posting.google.com >, Phil Powell wrote:
[PHP]
$sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
$_GET['sortBy'] : 1;
[/PHP]
$val = isset($_POST['val']) ? $_POST['val'] : (isset($_GET['val']) ? $_GET['val'] : 1);
I do not want to have to do this:

[PHP]
if ($_POST['sortBy']) {
$sortBy = $_POST['sortBy'];
} elseif ($_GET['sortBy']) {
$sortBy = $_GET['sortBy'];
} else {
$sortBy = 1;
}
[/PHP]


Who cares about lines anyway? Imho the if - elseif - else structure is
more readable, because more people will understand what is going on...
(As you said yourself that you hadn't heard about ternary operator..)

--
http://home.mysth.be/~timvw
Jul 17 '05 #3
In message <1c**************************@posting.google.com >, Phil
Powell <so*****@erols.com> writes
[PHP]
$sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
$_GET['sortBy'] : 1;
[/PHP]

This script should set $sortBy to the $_POST or the $_GET or the
default of 1 in one line.

I do not want to have to do this:

[PHP]
if ($_POST['sortBy']) {
$sortBy = $_POST['sortBy'];
} elseif ($_GET['sortBy']) {
$sortBy = $_GET['sortBy'];
} else {
$sortBy = 1;
}
[/PHP]
This might be useful for the _POST & _GET parts of your query::
http://uk.php.net/manual/en/function...-variables.php


This is too bloated for the 2000-line script I already have, and I want
to master ternary operators as I have never done so in 7 years.
Maybe it's time to split that script into some component parts?

I simply want the ternary operator equivalent of:

if - elseif - else

NOT

If - else
as the default..


--
Five Cats
Email to: cats_spam at uk2 dot net
Jul 17 '05 #4
"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
[PHP]
$sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
$_GET['sortBy'] : 1;
[/PHP]

This script should set $sortBy to the $_POST or the $_GET or the
default of 1 in one line.


Use $_REQUEST--it holds the content of $_POST, $_GET, and $_COOKIE.
Jul 17 '05 #5
Tim Van Wassenhove <eu**@pi.be> wrote in message news:<2g************@uni-berlin.de>...
In article <1c**************************@posting.google.com >, Phil Powell wrote:
[PHP]
$sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
$_GET['sortBy'] : 1;
[/PHP]


$val = isset($_POST['val']) ? $_POST['val'] : (isset($_GET['val']) ? $_GET['val'] : 1);
I do not want to have to do this:

[PHP]
if ($_POST['sortBy']) {
$sortBy = $_POST['sortBy'];
} elseif ($_GET['sortBy']) {

$sortBy = $_GET['sortBy'];
} else {

$sortBy = 1;
}
[/PHP]


Who cares about lines anyway?


[snip]

The person who inherits your 50 PHP scripts each averaging 1700 lines
each, might care.

Phil
Jul 17 '05 #6
In article <1c**************************@posting.google.com >, Phil Powell wrote:
Tim Van Wassenhove <eu**@pi.be> wrote in message news:<2g************@uni-berlin.de>...
In article <1c**************************@posting.google.com >, Phil Powell wrote:
> [PHP]
> $sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
> $_GET['sortBy'] : 1;
> [/PHP]


$val = isset($_POST['val']) ? $_POST['val'] : (isset($_GET['val']) ? $_GET['val'] : 1);
> I do not want to have to do this:
>
> [PHP]
> if ($_POST['sortBy']) {
> $sortBy = $_POST['sortBy'];
> } elseif ($_GET['sortBy']) {

$sortBy = $_GET['sortBy'];
> } else {

$sortBy = 1;
> }
> [/PHP]


Who cares about lines anyway?


[snip]

The person who inherits your 50 PHP scripts each averaging 1700 lines
each, might care.


Then that person should get himself a decent editor, so he can collapse
code blocks etc...

All that matters is the readability, and i think the if - else structure
is more readable.

--
http://home.mysth.be/~timvw
Jul 17 '05 #7
It's one code block, 1700 lines each, 20 files.

They're files consisting of 20 - 30 classes each. All in one code
block. Total lines: avg 1700.

Phil

Tim Van Wassenhove <eu**@pi.be> wrote in message news:<2g***********@uni-berlin.de>...
In article <1c**************************@posting.google.com >, Phil Powell wrote:
Tim Van Wassenhove <eu**@pi.be> wrote in message news:<2g************@uni-berlin.de>...
In article <1c**************************@posting.google.com >, Phil Powell wrote:
> [PHP]
> $sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
> $_GET['sortBy'] : 1;
> [/PHP]

$val = isset($_POST['val']) ? $_POST['val'] : (isset($_GET['val']) ? $_GET['val'] : 1);

> I do not want to have to do this:
>
> [PHP]
> if ($_POST['sortBy']) {
> $sortBy = $_POST['sortBy'];
> } elseif ($_GET['sortBy']) { $sortBy = $_GET['sortBy']; > } else { $sortBy = 1; > }
> [/PHP]

Who cares about lines anyway?


[snip]

The person who inherits your 50 PHP scripts each averaging 1700 lines
each, might care.


Then that person should get himself a decent editor, so he can collapse
code blocks etc...

All that matters is the readability, and i think the if - else structure
is more readable.

Jul 17 '05 #8

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

Similar topics

6
by: praba kar | last post by:
Dear All, I am new to Python. I want to know how to work with ternary operator in Python. I cannot find any ternary operator in Python. So Kindly clear my doubt regarding this ...
4
by: Bob Gregory | last post by:
Hi all, I don't actually have a mac with which to test this, but I'm informed by a colleague that one of my scripts has failed in IE on the Mac; endless twiddling seems to point to the ternary...
6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
6
by: Robert Zurer | last post by:
In his paper on Coding Standard found on http://www.idesign.net/idesign/DesktopDefault.aspx Juval Lowy discourages the use of the ternary conditional operator with no specific reason given. ...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
15
by: Arthur Dent | last post by:
Hi all, im just curious if anyone knows..... With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck away in a corner somewhere? By ternary, i mean something like C's a?b:c...
5
by: PerlPhi | last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show...
4
by: raiderdav | last post by:
I understand how the ternary operator (question mark - ?) works in an if/else setting, but what does it mean when used as a type? For instance, I'm trying to add a get/set in some existing code,...
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
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,...
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
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,...
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: 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...
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.