note 86283 modified in control-structures.switch by danbrown

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
<?php
	/* script 1  */
	$foo = "not a number";
	switch(false)
	{
		case "1":	{	$foo = "1";	break;	}
		case "2":	{	$foo = "2";	break;	}
		default:	{	$foo = "0";	}
	}
	
	echo $foo;	// will produce "not a number"
	
	/* script 2  */
	$foo = "not a number";
	$arr = array("not a number"); // 1 element only !
	switch($arr[1])	// element $foo[1] doesn't defined 
	{
		case "1":	{	$foo = "1";	break;	}
		case "2":	{	$foo = "2";	break;	}
		default:	{	$foo = "0";	}
	}
	
	echo $foo;	// will produce "not a number" ( not 0 ! )
	
	/* script 3  */
	$foo = "not a number";
	$arr = array("not a number"); // 1 element only !
	switch($arr[1]?$arr[1]:"1")	// element $foo[1] doesn't defined 
	{
		case "1":	{	$foo = "1";	break;	}
		case "2":	{	$foo = "2";	break;	}
		default:	{	$foo = "0";	}
	}
	
	echo $foo;	
	// will produce :
	// 1 if $arr[1] isn't set
	// 1 if $arr[1]=1
	// 2 if $arr[1]=2
	// 0 if none of above
?>

--was--
<?
	/* script 1  */
	$foo = "not a number";
	switch(false)
	{
		case "1":	{	$foo = "1";	break;	}
		case "2":	{	$foo = "2";	break;	}
		default:	{	$foo = "0";	}
	}
	
	echo $foo;	// will produce "not a number"
	
	/* script 2  */
	$foo = "not a number";
	$arr = array("not a number"); // 1 element only !
	switch($arr[1])	// element $foo[1] doesn't defined 
	{
		case "1":	{	$foo = "1";	break;	}
		case "2":	{	$foo = "2";	break;	}
		default:	{	$foo = "0";	}
	}
	
	echo $foo;	// will produce "not a number" ( not 0 ! )
	
	/* script 3  */
	$foo = "not a number";
	$arr = array("not a number"); // 1 element only !
	switch($arr[1]?$arr[1]:"1")	// element $foo[1] doesn't defined 
	{
		case "1":	{	$foo = "1";	break;	}
		case "2":	{	$foo = "2";	break;	}
		default:	{	$foo = "0";	}
	}
	
	echo $foo;	
	// will produce :
	// 1 if $arr[1] isn't set
	// 1 if $arr[1]=1
	// 2 if $arr[1]=2
	// 0 if none of above
?>

http://php.net/manual/en/control-structures.switch.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.