-
-
Save KnowTheCodePro/9a1f6a511ba238b9b9993d238be2435f to your computer and use it in GitHub Desktop.
if/else The Right Way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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