All posts by andreas

jQuery: understanding attr() and prop()

The attr() function in jQuery is used to get or set the value of an attribute. As an example, this line will return the value of href for the first a in the set:

// returns the href of the first a in the document
$('a').attr('href');

When setting an attribute with attr() all elements in the set will be affected.

// sets the href for all a elements in the document
$('a').attr('href', 'http://example.com');

HTML elements can have attributes and properties. A good example to understand the difference between an attribute and a property is the checked attribute of a checkbox. If the checked attribute is set in HTML a checkbox will be initially checked.

<input type="checkbox" checked>

Here it gets interesting: If the checkbox is clicked, the attribute does not change. The value returned by attr(‘checked’) will always be ‘checked’, no matter how often the checkbox is clicked. But the identically named property does change.

// returns the current state of a checkbox (true/false)
$(elem).prop('checked');

The above line will always return the actual state of a checkbox. It will return true if checked and false if not checked.

The attribute should be used only to set the initial value of the checkbox. Although it’s technically possible to alter the state of the checkbox by setting/unsetting the attribute, it is not good practice.

// use prop('name', value) to check or uncheck a checkbox dynamically
$(elem).prop('checked', true);

The same is true for other attributes like selected, value and disabled.

api.jquery.com/attr/
api.jquery.com/prop/

Ignoring Errors For the Good?

In PHP the @ operator temporarily sets the error reporting level to 0 for one expression.  You will find it in many PHP scripts preceding mysql_connect often in combination with die to terminate the script with a custom error message.

@mysql_connect( $dbhost, $dbuser, $dbpassword) or die (‘Error establishing a database connection’);

Recently I found this line in the source code of Magento:

if(@$array[$key_that_might_exist])

Basically it looks like a more convenient way to check if the key exists and evaluates as true.

if(isset($array[key])) {
if($array[key]) {
// do something
}
}

// or, in one line

if(isset($array[key]) && $array[key]) {
// do something
}

While checking the PHP documentation I found one comment stating that the @ is pretty slow.

Be aware that using @ is dog-slow, as PHP incurs overhead to suppressing errors in this way. It’s a trade-off between speed and convenience.

I don’t believe what I don’t see, so I ran run a speed comparison test. This are the results:

// the time was measured with microtime(), each line has been iterated 100000 times.

// test array
$array = array('123' => 'test');

if(@$array['123']){}
// took 0.0344sec

if(isset($array['123']) && $array['123'] ){}
// took 0.0153sec

if(@$array['not']){}
// took 0.0730sec

if(isset($array['not']) && $array['not'] ){}
// took 0.0092sec

Conclusion

It is up to 8 times faster to  use isset() instead of the little more convenient @. The comment on php.net is right, it is everything but fast to use @ for this purpose.