admin管理员组

文章数量:1387287

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

Recently I met the below code

did_action( 'init' ) && $scripts->add_inline_script( 'lodash', 'window.lodash = _.noConflict();' );

inside the file \wp-includes\script-loader.php

Does anyone knows what does it mean? Especially double ampersand between two commands. I 've never met this syntax in PHP before and I cannot find documentation about this.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

Recently I met the below code

did_action( 'init' ) && $scripts->add_inline_script( 'lodash', 'window.lodash = _.noConflict();' );

inside the file \wp-includes\script-loader.php

Does anyone knows what does it mean? Especially double ampersand between two commands. I 've never met this syntax in PHP before and I cannot find documentation about this.

Share Improve this question edited Apr 18, 2020 at 14:54 Nomikos Strigkos asked Apr 18, 2020 at 14:48 Nomikos StrigkosNomikos Strigkos 95 bronze badges 1
  • 1 This is a PHP question, not WordPress. But && is a bitwise operator. It would be the same as writing if( did_action('init') ) { do_something(); } – Nathan Johnson Commented Apr 19, 2020 at 18:20
Add a comment  | 

2 Answers 2

Reset to default 2

I think its actually not allowed or discouraged in WP coding standards.

Its is basically saying "If true go ahead".

It's a short form of this:

if ( did_action( 'init' ) ) {
  $scripts->add_inline_script( 'lodash', 'window.lodash = _.noConflict();' );
}

You can also think of this as if it's inside brackets of an if statement like this.

$var = false;

if ( $var && $scripts->add_inline_script( '...' ) ) { // As long as $var is false PHP won't execute or check what comes after the `&&`

}

The line itself could be refactored to use AND instead of && as well. This technique is sometimes used to make code "speak" English. Example:

$if_my_value_is_TRUE = TRUE;
$if_my_value_is_TRUE AND print "This get's printed to the screen.";

// The exact same thing with `OR` or `||`
$my_value_is_TRUE = FALSE;
$my_value_is_TRUE OR print "This get's printed to the screen.";

It must be something like :

Do both simultaneously or do nothing

because i find out that if the first command cannot be executed neither the second does

本文标签: functionsphp syntax ampampbetween commands