-
-
Save mashiox/47c31461ecccd07f1d4d1abd52a0f04b to your computer and use it in GitHub Desktop.
PHP goto if then else
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 | |
/* If - then */ | |
function if( $conditional = false, $then = function(){} ) | |
{ | |
case ( $conditional ) | |
{ | |
case true: | |
goto isTrue; | |
case false: | |
goto isFalse; | |
} | |
isTrue: | |
$then(); | |
isFalse: | |
return; | |
} | |
/* If then w/ else */ | |
function if( $conditional = false, $then = function(){}, $else = function(){} ) | |
{ | |
case ( $conditional ) | |
{ | |
case true: | |
goto isTrue; | |
case false: | |
goto isFalse; | |
} | |
isTrue: | |
$then(); | |
goto end; | |
isFalse: | |
$else(); | |
end: | |
return; | |
} | |
function if | |
( | |
$conditional = false, | |
$then = function(){}, | |
...$elseIf = array( | |
array( | |
'condition' => false, | |
'function' => function(){} | |
) | |
), | |
$else = function(){} | |
) | |
{ | |
case ( $conditional ) | |
{ | |
case true: | |
goto isTrue; | |
case false: | |
goto isFalse; | |
} | |
isTrue: | |
$then(); | |
goto theEnd; | |
isFalse: | |
if( count( $elseIf ) === 0, | |
function(){ goto theEnd; }, // array is length 0 | |
function() // not length 0 | |
{ | |
$nextElIf = array_shift( $elseIf ); | |
if | |
( | |
$nextElIf['condition'], | |
$nextElIf['function'], | |
$elseIf, | |
$else | |
) | |
} | |
theEnd: | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment