How can PHP functions be optimized to handle both GPIO control and text output without compromising code structure or readability?

To optimize PHP functions for handling both GPIO control and text output without compromising code structure or readability, you can separate the GPIO control logic into a dedicated function and use conditional statements to determine whether to output text or control GPIO. By encapsulating the GPIO control in a separate function, you can maintain code clarity and readability while efficiently managing both functionalities.

function controlGPIO($pin, $value) {
    // GPIO control logic here
}

function handleOutput($output) {
    if ($output === 'text') {
        echo 'Outputting text';
    } elseif ($output === 'gpio') {
        controlGPIO(17, 1); // Example GPIO control
    }
}

// Example usage
handleOutput('text');
handleOutput('gpio');