?
Solved

Can we call a function inside switch statement?

Posted on 2009-04-15
3
Medium Priority
?
1,234 Views
Last Modified: 2012-05-06
Can we call a function inside switch case? If yes, then please tell me how we can do that?
0
Comment
Question by:designersx
  • 2
3 Comments
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 400 total points
ID: 24154984
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
 
LVL 5

Expert Comment

by:palanee83
ID: 24154991
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
 
LVL 5

Expert Comment

by:palanee83
ID: 24155024
Sorry i have submitted before refresh. So not noticed "logudotcom" comment.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question