0

I would like to know the use of @ character in WordPress. For example:

if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) { ... }

Also what is the difference between @opendir and @ opendir, i.e. using a space after @ character.

I am new to WordPress.

Subrata Sarkar
  • 2,975
  • 6
  • 45
  • 85

1 Answers1

1

That is a part of the PHP language syntax and not strictly part of WordPress itself. The @ symbol denotes that any errors from that function should be suppressed in case it fails for some reason.

This is called an Error Control Operator and you can find more information about it on the PHP documentation website. It stops errors of all types from being displayed (which can be a good or bad thing depending on what is happening).

There doesn't seem to be any difference between having a space and not having one. It will just depend on your style of writing code.

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

<?php
/* Intentional file error */
$my_file = @file ('non_existent_file') or
    die ("Failed opening file: error was '$php_errormsg'");

// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.

?>
  • Suppressing the error reporting is always a bad thing. It provides the developer a false sense of security and correctness while their code can fail spectacularly without them being informed. – axiac May 25 '18 at 14:00
  • @axiac That sentence is redundant, the post is already about [tag:wordpress]. – deceze May 25 '18 at 14:02