Can we call a function inside switch statement?

Can we call a function inside switch case? If yes, then please tell me how we can do that?
designersxAsked:
Who is Participating?
 
Loganathan NatarajanLAMP DeveloperCommented:
you may call functions inside switch...

<?php
switch (true){
    case 1:
        echo "processing stats";
        process_stats();
        break; 
    case 2:
        echo "processing prices";
        process_prices();
        break; 
    default:
        echo = "File not recognized!!.";
}
?> 

Open in new window

0
 
palanee83Commented:
Hi,

Yes you can call the function inside the switch case.


Please find the attached code for how to do this
<?php
 
function test()
{
	echo "Function called";
}
$a = 1;
switch($a)
{
 
case 2:
	echo "Its in 2";
break;
case 1:
	echo "Its in 1";
	test();
break;
 
}
?>

Open in new window

0
 
palanee83Commented:
Sorry i have submitted before refresh. So not noticed "logudotcom" comment.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.