Skip to content

Instantly share code, notes, and snippets.

@KnowTheCodePro
Created March 30, 2016 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KnowTheCodePro/9a1f6a511ba238b9b9993d238be2435f to your computer and use it in GitHub Desktop.
Save KnowTheCodePro/9a1f6a511ba238b9b9993d238be2435f to your computer and use it in GitHub Desktop.
if/else The Right Way
<?php
namespace KnowTheCode;
/**
* This version shows the right way to do the if/else logic. Notice
* that the if conditional expression is testing for a "truthy" condition
* and the work is being done in it's code block.
*
* The else is now set to default state.
*
* @since 1.0.0
*
* @param string $event_name
*
* @return int
*/
function count_number_of_times_fired( $event_name ) {
static $events = array();
if ( isset( $events[ $event_name ] ) ) {
$events[ $event_name ]++;
} else {
$events[ $event_name ] = 1;
}
return $events[ $event_name ];
}
d( count_number_of_times_fired( 'loop_start' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment