Skip to content

Instantly share code, notes, and snippets.

@mashiox
Forked from anonymous/if.php
Last active February 27, 2017 01:38
Show Gist options
  • Save mashiox/47c31461ecccd07f1d4d1abd52a0f04b to your computer and use it in GitHub Desktop.
Save mashiox/47c31461ecccd07f1d4d1abd52a0f04b to your computer and use it in GitHub Desktop.
PHP goto if then else
<?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