Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-15-2006, 09:07 PM   #1
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
if ( !($this->connection =@ mysql_connect($sqlhost, $sqluser, $sqlpw)) ) {
    return false;
if ( mysql_select_db($sqldb, $this->connection) ) {
    return true;
} else {
    return false;
2nd approach
$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
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
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.


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 > Forums > Non-*NIX Forums > Programming

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

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