PHP初歩解説記事その1[swith][制御構文]

Twitterだとさすがにソースコードの解説は厳しいからこっちに書くことにした

基本ここ参照で
http://php.net/manual/ja/control-structures.switch.php

で、始めるよー



修正前

<?php
$agent = $_SERVER['HTTP_USER_AGENT'];

//switch文はここまで複雑なことはできない
switch(true){//ここには条件判定したい変数をいれる
	//case: は
	//例えば 
	//case: 0 や
	//case: "iPhone"みたいな使い方しかできない
	case(ereg("DoCoMo",$agent)):
		header("Location: http://〜");
		break;
	case(ereg("Vodafone|J-PHONE|SoftBank|MOT-",$agent)):
		header("Location: http://〜");
		break;
	case(ereg("UP.Browser",$agent)):
		header("Location: http://〜");
		break;
	case(ereg("iPhone",$agent)):
		header("Location: http://〜");
		break;
	default:
		header("Location: http://〜");
	}
?>


修正後

<?php
$agent = $_SERVER['HTTP_USER_AGENT'];

if(ereg("DoCoMo",$agent) !== false){
	header("Location: http://〜");
}else if(ereg("Vodafone|J-PHONE|SoftBank|MOT-",$agent) !== false){
	header("Location: http://〜");
}else if(ereg("UP.Browser",$agent) !== false){
	header("Location: http://〜");
}else if(ereg("iPhone",$agent) !== false){
	header("Location: http://〜");
}else{
	header("Location: http://〜");
}

?>


デバッグはしてないからエラーでるかもー><