Page 1 of 1

switch VERY-Basic problem

Posted: Thu May 20, 2010 3:06 am
by refael_gold

Code: Select all

<?php 
$index=4;

switch ($index)


for ($m=0; $m<100;$m++)
{
 case $m :  echo "lets keep run the numbers now we are in:".$m ; break; 
 }
 

?>
i'm started to learn PHP by lynda.com 2 days ago and really need some help
Q:

1.i have tried to put 100 options at the case part and it didnt run
i try to put {} at the switch and it didnt help also...
i know i can do staff like this at java but have no idea why its not work.
it must be a way to put case numbers with a loop and i will be glad to hear about it

2.i need some method to put value at $index by the client ------(like in java index=scan.nextInt();)-------

Re: switch VERY-Basic problem

Posted: Thu May 20, 2010 3:14 am
by wattee
do u mean something like this?

Code: Select all

$index = 4;

switch ($index) {
	case 1:
	break;
	
	case 2:
	break;
	
	case 3:
	break;
	
	case 4:
	for ($m=0; $m<100;$m++)
	{
		echo "lets keep run the numbers now we are in:".$m. "<br>";
	}

}
or can you clarify what you want to do

Re: switch VERY-Basic problem

Posted: Thu May 20, 2010 9:26 am
by refael_gold
not all the code are showed...
1 second..

Code: Select all

<?php 
$index=4;

switch ($index)


for ($m=0; $m<100;$m++)
{
 case $m :  echo "lets keep run the numbers now we are in:"."<br/>".$m;break;
 }
 

?>
now its more clear?

Re: switch VERY-Basic problem

Posted: Thu May 20, 2010 10:52 am
by Benjamin
Tell us why you are trying to do this. Your approach is not correct. This is possible, but it's not something you would ever want to do.

Re: switch VERY-Basic problem

Posted: Thu May 20, 2010 1:03 pm
by AbraCadaver
Benjamin wrote:Tell us why you are trying to do this. Your approach is not correct. This is possible, but it's not something you would ever want to do.
I would be interested to know why this is needed as well, however I don't think it's possible inside a switch without using eval().

Re: switch VERY-Basic problem

Posted: Fri May 21, 2010 11:47 am
by refael_gold
lets say i having a 100 options that i need to do "copy" to all cases...

i want to do it with FOR statement -how i do it?

its very helpful lets say i need to write some comment on 1-10 grades(for exmple) and 11-20 and so on and so on...
i want to write all the cases with a for(instad write it 100 times the CASE option)


tnx for trying to help

Re: switch VERY-Basic problem

Posted: Fri May 21, 2010 1:03 pm
by AbraCadaver
refael_gold wrote:lets say i having a 100 options that i need to do "copy" to all cases...

i want to do it with FOR statement -how i do it?

its very helpful lets say i need to write some comment on 1-10 grades(for exmple) and 11-20 and so on and so on...
i want to write all the cases with a for(instad write it 100 times the CASE option)


tnx for trying to help
Give us the real world code for 3 or 4 cases and explain why you need a switch with cases. If your example worked, it would be the same as just doing this:

Code: Select all

$index = 4;
echo "lets keep run the numbers now we are in:"."<br/>".$index;