0

I have an issue here, and I'm looking for experienced programmers to tell me which is the preferred solution.

I have values being returned that are surrounded in quotes. "TOTAL" and "VALUE" being two examples. These should not be confused with TOTAL and VALUE -- the string is actually surrounded by double quotes.

What I noticed is that the switch statement below doesn't work because it's looking for TOTAL not "TOTAL":

switch ($statTypeName) {
    case "TOTAL":
        echo "<br>TOTAL";
        break;
    case "VALUE":
        echo "<br>VALUE";
        break;
}

To get this working, I had to put a single quote around the case -- '"TOTAL"'. In my text editor (Notepad++), it is difficult to see the single quote around the double quotes.

I know this isn't a common issue, but what would be the "professional" way of solving this? The way I did it, or should I be extracting the string from the quoted string and do away with the double quotes altogether..?

Thanks!

pr0tocol
  • 323
  • 1
  • 4
  • 14

4 Answers4

2
 case "\"TOTAL\"":

Escape the inner double quotes. It will work the same way and might be a little more visible to the reader

kz3
  • 785
  • 2
  • 10
  • 23
1

What you're running into is indeed common, and you can go about it a couple different ways. There's nothing wrong with the way you're doing it, or @KyleBanks solution (escaping the double quotes). Given php provides single and double quote string definitions, I prefer the first. But its up to your preference, or your dev team.

As far as extracting the substring within the string quotes.. it depends on what they're there for in the first place.

Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
  • I am parsing a file, so the original author intended for the value to be contained within quotes. I have no control over that, only the way in which I parse :). I am to use single quotes around the double-quoted string. Thanks! – pr0tocol Dec 22 '11 at 23:33
  • If you don't need the quotes for the purpose of your program, you could parse around them and avoid working with the quotes in your code, yea? – Nick Rolando Dec 22 '11 at 23:43
1

I would suggest using a better font in Notepad++. I personally use Consolas however here you can find heaps of other good options: Recommended Fonts for Programming?

Other then changing font escaping quotes as was suggested is another alternative:

 case "\"TOTAL\"":

You can also try to strip quotes:

switch (substr($statTypeName, 1, -1)) {...}

but i consider it as a more dangerous approach unless you start using more complicated code to strip them with checks and everything in which case it clearly becomes an overkill.

Community
  • 1
  • 1
Alexei Tenitski
  • 9,030
  • 6
  • 41
  • 50
0

Except if your given code is not part of some kind of StatType class and is dealing internally with the representation of stat type states my answer might be missing the point a bit, but in any case here it is.

In fact you are doing something wrong here and what you are asking is to find a way to workaround the essential problem you are having. Instead, you should fix the essential problem.

Essential problem is that you are missing one layer of abstraction which will sit between the way you are representing your statType and the way you are using it.

So, your program should not care if you call your statType:

"TOTAL" or '"TOTAL"' or "Total" or "total"

What you need to care is that your statType is in certain state in one moment of program execution. How that representation of the state is implemented ( a string with quotes or a number) is detail of implementation and your switch statement should not care about it.

What happens if you decide to change your statTypeName to be without quotes for example. Than you'll have to go to every line of code that depended on it having quotes and to change it. If you would hide the implementation details in some way you would not need to change more than one line of code.

Maybe one approach to setting abstraction around statTypes? (simplified for clarity)

class StatType
{
    const TOTAL = 0;
    const VALUE = 1;
    // etc.
}

switch ($statType->type()) {
    case StatType::TOTAL:
        echo "<br>TOTAL";
        break;
    case StatType::VALUE:
        echo "<br>VALUE";
        break;
}
ivanjovanovic
  • 511
  • 4
  • 9