4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPでgoto

Last updated at Posted at 2016-03-16

phpでもgoto使えたんだという話

プログラムの可読性を破壊的に低下させると噂のgoto
でもPHPでは使いドコロがない
多重ループを設定で抜けられるから break 2;

プログラム

goto.php
<?php

goto end;

start:
    echo 'start.' . PHP_EOL;
goto sotti;

atti:
    echo 'atti.' . PHP_EOL;
goto start;

sotti:
    echo 'sotti.' . PHP_EOL;
exit;

end:
    echo 'end.' . PHP_EOL;
goto atti;

出力結果

$ php goto.php
end.
atti.
start.
sotti.

この程度なら追えるけど、長くなるとヤバイ

制約

  • 対象となるラベルは同じファイル上の同じコンテキスト
    • つまり関数外から関数内の行き来はできない
  • forやifの中のラベルにもgotoできない
  • ラベルにもgotoにも変数は利用できない
    • 出来たらヤバイ 便利かもしれないけど更にカオスになる
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?