星期五, 3月 30, 2007

C/PHP 的單一出口

PHP 裡面看到類似這樣的程式碼:
if (isset($_GET['var1'])) {
$var1 = $_GET['var1'];
if (isset($_GET['var2'])) {
$var2 = $_GET['var2'];
if (isset($_GET['var3'])) {
$var3 = $_GET['var3'];
// do something...
}
}
}

如果條件式再多一點的話就會更爆炸,我看到的 code 是七層... -_-

其實早在 C 語言時這個問題就有一個堪用的解法,拿到 PHP 上用也可以:
do {
if (isset($_GET['var1']))
break;
$var1 = $_GET['var1'];

// 以下類推
} while (0);

2 則留言:

Roy 提到...

> do {
> if (isset($_GET['var1']))
> ...
應該是 if (!isset($_GET['var1'])) 才對吧?

batshevahvagts 提到...

The 영앤리치 토토 operator makes provision for a broad number of promos & giveaways, irrespective of if they're a novice or frequent gamester. Players of Roulette tables can rotate the sport reels & set forth their stakes over either shade or figures. Know that it ought to be only for fun and the home always wins.