LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2006, 09:07 PM   #1
unreal128
Member
 
Registered: Jun 2003
Distribution: SuSE, Slackware, Gentoo
Posts: 207

Rep: Reputation: 30
PHP5: Logical Operators vs. If-Else


I am creating a class function that connects to a mySQL server and then selects a database. There are two approaches to creating a segment of this code.

1st approach
Code:
if ( !($this->connection =@ mysql_connect($sqlhost, $sqluser, $sqlpw)) ) {
    return false;
}
if ( mysql_select_db($sqldb, $this->connection) ) {
    return true;
} else {
    return false;
}
2nd approach
Code:
$this->connection =@ mysql_connect($sqlhost, $sqluser, $sqlpw) || return false;
mysql_select_db($sqldb, $this->connection) || return false;
return true;
Which is the better choice and why?
 
Old 11-15-2006, 11:50 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The first approach is the conventional approach, the second relying on the logical operators being implemented as short circuit logical operators. Because most people will find the first approach easier or even a more natural style I'd go with that. Also it allows more do be done, if for example you want to write to a log file where it failed before returning it is trivial to add that code.
 
Old 11-15-2006, 11:52 PM   #3
unreal128
Member
 
Registered: Jun 2003
Distribution: SuSE, Slackware, Gentoo
Posts: 207

Original Poster
Rep: Reputation: 30
Thanks for the feedback. I guess I am just on a quest to trim down my code and still make it easy to read.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
? on logical operators in BASH eroica Linux - Software 10 02-01-2005 02:12 PM
Operators *^ and >> GodSendDeath Programming 4 11-01-2004 10:47 PM
about bitwise operators? eshwar_ind Programming 17 10-25-2004 03:13 AM
Bitwise operators kamransoomro84 Programming 8 04-22-2004 11:46 PM
operators linuxanswer Programming 3 12-14-2003 07:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration