WordPress.org

Make WordPress Core

#42887 closed defect (bug) (invalid)

variable assignment in if statement

Reported by: freetheweb Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9.1
Component: Widgets Keywords:
Focuses: coding-standards Cc:
PR Number:

Description

I saw this page: https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/ajax-actions.php

contains the following code:

if ( $form = $wp_registered_widget_controls[ $widget_id ] ) { call_user_func_array( $form['callback'], $form['params'] );
}

Is this if statement supposed to use an equality operator (two equals signs) instead of assigning a variable? If not, why is it assigning a variable in an if statement?

Change History (1)

#1 @dd32
23 months ago

  • Focuses coding-standards added
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Hi @freetheweb,

Yes, this is intended, and is a supported PHP construct.
The if( ) statement operates on the resulting variable $form and so this could be written simply as:

$form = $wp_registered_widget_controls[ $widget_id ];
if ( $form ) {
   call_user_func_array( $form['callback'], $form['params'] );
}

I'm going to close this as invalid, as there isn't any bug here, however will note that this may be changed with the ongoing coding-standards work, if this doesn't fit a decided coding standard, it'll be automatically updated as that process goes on.

Note: See TracTickets for help on using tickets.