What does this code line do?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zaphod2003
Forum Newbie
Posts: 4
Joined: Wed Apr 18, 2007 6:12 am

What does this code line do?

Post by zaphod2003 »

I am new to PHP but am finding it quite easy(ish) so far to understand it.

However there is a section of code which I cannot quite figure out:


Code: Select all

if (!($this->_resource = @mysql_connect( $host, $user, $pass ))) {

What does the above line do? To clarify, what's the significance of the "->" symbol?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

If a mysql connection cannot by put into the _resource variable in the scope of this object then do this...
zaphod2003
Forum Newbie
Posts: 4
Joined: Wed Apr 18, 2007 6:12 am

Post by zaphod2003 »

OK thanks a lot. I understand now
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Lets break it down...

Code: Select all

if (!($this->_resource = @mysql_connect( $host, $user, $pass ))) {
Attempt to connect to the mysql database server (using the params passed) and pass the result of the mysql_connect function to the class var $_resource ($this->_resource). If there are errors, don't tell anyone (@). If the connection attempt was unsuccessful, the function returns boolean false to the class var $_resource ($this->_resource).


So if the value of $this->_resource is false (!), then execute what is inside the control block ({...}).

! is the negation operator. It means to make the opposite of (essentially).
-> is the object operator which allows access to object methods and properties (for static properties and methods you'd use the scope resolution operator ::)
@ is the error suppression operator. Bad all around, don't use it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Everah wrote:@ is the error suppression operator. Bad all around, don't use it.
I wouldn't say don't use it, simply use it sparingly and only when neccesary.
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Post by staar2 »

Don't use it when you are debuging, try to avoid this error hide thing. All errors what appears try to fix. Only if you release your web hide error, but not with this @.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Jcart wrote:
Everah wrote:@ is the error suppression operator. Bad all around, don't use it.
I wouldn't say don't use it, simply use it sparingly and only when neccesary.
Ok, I'll agree with that. There are cases in which notices are thrown outside of your control that might be better snuffed out as needed. But I would suggest you attempt to not use it as much as possible, instead shooting for correct implementation of code to not throw errors (if it is within your control).
Post Reply