Page 1 of 1
What does this code line do?
Posted: Wed Apr 18, 2007 6:22 am
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?
Posted: Wed Apr 18, 2007 6:31 am
by onion2k
If a mysql connection cannot by put into the _resource variable in the scope of this object then do this...
Posted: Wed Apr 18, 2007 6:32 am
by zaphod2003
OK thanks a lot. I understand now
Posted: Wed Apr 18, 2007 10:53 am
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.
Posted: Wed Apr 18, 2007 11:08 am
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.
Posted: Wed Apr 18, 2007 11:11 am
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 @.
Posted: Wed Apr 18, 2007 11:15 am
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).