473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can the switch statement only evalute ==

Claus Mygind
571 512MB
In my dBase code I use the "do case", which is similar to php "switch".

However in dBase each "case" can have a different method of evaluation ==, contains, greater than etc.
In php can I do something similar to the dBase code below?
Expand|Select|Wrap|Line Numbers
  1.    do case
  2.  
  3.       /*is equal to one value*/
  4.       case cScreen == "Client"
  5.          ...code to execute ...      
  6.  
  7.       /*is equal to one of two values*/  
  8.       case cScreen == "Proposal" or cScreen == "Job"
  9.          ...code to execute ...      
  10.  
  11.       /*contains string in value*/
  12.       case "Cme" $ cScreen
  13.          ...code to execute ...      
  14.  
  15.    endcase
  16.  
  17.  
Can that be written in php with the switch command or am I forced to use the if () {...}else if(){...}else{...}?

Expand|Select|Wrap|Line Numbers
  1. switch ( cScreen ){
  2.    case "Client":
  3.          ...code to execute ...      
  4.  
  5.    /*testing for one of two values. Can this even be done?*/
  6.    case "Job" or "Prop":
  7.          ...code to execute ...      
  8.  
  9.    /*php's version of contains strpos($haystack,$needle);*/
  10.    case strpos($cScreen,"Cme"):
  11.          ...code to execute ...      
  12.  
  13. }
  14.  
Mar 17 '11 #1

✓ answered by code green

Not really. It is simpler to re-write as elseif statements
Expand|Select|Wrap|Line Numbers
  1. switch ( cScreen ){ 
  2.    if($cScreen == "Client")
  3.          ...code to execute ...       
  4.  
  5.    /*testing for one of two values. Can this even be done?*/ 
  6.    elseif($cScreen == "Job" || $cScreen == "Prop") 
  7.          ...code to execute ... 
  8.  
  9.  
  10.     elseif(strpos($cScreen,"Cme")
  11.          ...code to execute ...  
  12.  

5 3226
code green
1,726 Expert 1GB
Not really. It is simpler to re-write as elseif statements
Expand|Select|Wrap|Line Numbers
  1. switch ( cScreen ){ 
  2.    if($cScreen == "Client")
  3.          ...code to execute ...       
  4.  
  5.    /*testing for one of two values. Can this even be done?*/ 
  6.    elseif($cScreen == "Job" || $cScreen == "Prop") 
  7.          ...code to execute ... 
  8.  
  9.  
  10.     elseif(strpos($cScreen,"Cme")
  11.          ...code to execute ...  
  12.  
Mar 17 '11 #2
Claus Mygind
571 512MB
Thanks for the confirmation of my suspicions
Mar 17 '11 #3
Err well you can execute code in the case so its not that hard to add an extra check for OR/AND whatever. Still better than the elseif mess if that works for you.
Mar 18 '11 #4
johny10151981
1,059 1GB
you would have to understand about the things, what exactly returned from condition

1. 0
2. 1


if you print the returned value you will get either 0 or 1

lets check
Expand|Select|Wrap|Line Numbers
  1.  $var=$x>$y;
  2.  
if you print the above result $var you will get either zero or one.

So Why not judge your case statement.
Mar 19 '11 #5
Claus Mygind
571 512MB
I don't see the connection to the question here?

Are you saying it is possible to make a test in one of the case statements for a sub-string containment? In my original question, I asked if 3 different test could be made in one single switch statement. Is that what you are saying? If so please give a complete example!
Mar 21 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

26
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
18
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
1
by: Berko | last post by:
I'm having problems with a switch statement that is wrapped in a foreach statement so that each index in an array is evaluated against the switch statement. The switch/case will then return some...
19
by: rdavis7408 | last post by:
Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to...
4
by: priyanka | last post by:
Hi there, I had a question. Is there any way of testing a string value in a switch statement. I have about 50 string values that can be in a string variable. I tried cheking them with the if...
5
by: mark4asp | last post by:
Every time the function below is called I get the alert. So I put a deliberate error in there and I check the value of (reportType=='MANDATE') in Firebug, which is found to be true. But still the...
2
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.