Skip to content
← Back to Snippets
Code

Avoid Ternary Branching with Explicit Flow

Shows a readable if/else branch for choosing a label instead of using a ternary expression.

Purpose

Shows a readable if/else branch for choosing a label instead of using a ternary expression.

Snippet details

ContextControl FlowLevelProductionCopy-and-paste statusMarked safe after review.

Categories

  • Forms and Validation

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Avoid Ternary Branching with Explicit Flow.
 *
 * Purpose:
 * Uses explicit if/else logic to choose a status label.
 *
 * @param bool $is_ready Readiness flag.
 * @return string Status label.
 */
function ogSnippetAvoidTernaryBranchingWithExplicitFlow(bool $is_ready): string {
	$status_label = 'not ready';

	if ($is_ready === true) {
		$status_label = 'ready';
	} else {
		$status_label = 'not ready';
	}

	return $status_label;
}

$status = ogSnippetAvoidTernaryBranchingWithExplicitFlow(true);

echo 'Galactica status: '.$status;