PHP Switch 语句,其中字符串封装在双引号中

发布于 2024-12-22 19:32:24 字数 631 浏览 3 评论 0原文

我这里有一个问题,我正在寻找经验丰富的程序员来告诉我哪个是首选解决方案。

我返回的值用引号引起来。 “TOTAL”“VALUE” 是两个示例。这些不应与 TOTALVALUE 混淆——字符串实际上是用双引号引起来的。

我注意到下面的 switch 语句不起作用,因为它正在寻找 TOTAL 而不是 "TOTAL"

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

为了使其正常工作,我必须在周围加上单引号案例 - '"TOTAL"'。 在我的文本编辑器(Notepad++)中,很难看到双引号周围的单引号。

我知道这不是一个常见问题,但是解决这个问题的“专业”方法是什么?我这样做的方式,或者我应该从带引号的字符串中提取字符串并完全删除双引号..?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人间不值得 2024-12-29 19:32:24
 case "\"TOTAL\"":

转义内部双引号。它会以同样的方式工作,并且可能对读者来说更明显一些

 case "\"TOTAL\"":

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

飘落散花 2024-12-29 19:32:24

您遇到的情况确实很常见,您可以通过几种不同的方式来解决。您的操作方式或@KyleBanks 解决方案(转义双引号)没有任何问题。鉴于 php 提供了单引号和双引号字符串定义,我更喜欢第一个。但这取决于您的偏好或您的开发团队。

至于提取字符串引号内的子字符串..这首先取决于它们的用途。

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.

我建议在 Notepad++ 中使用更好的字体。我个人使用 Consolas,但是在这里您可以找到很多其他不错的选择:
推荐用于编程的字体?

按照建议更改字体转义引号是另一种选择:

 case "\"TOTAL\"":

您可以也尝试删除引号:

switch (substr($statTypeName, 1, -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.

后来的我们 2024-12-29 19:32:24

除非您给定的代码不是某种 StatType 类的一部分,并且在内部处理 stat 类型的表示,否则我的答案可能有点遗漏了要点,但无论如何,这里都是如此。

事实上,您在这里做错了,您所要求的是找到一种方法来解决您遇到的基本问题。相反,你应该解决根本问题。

根本问题是您缺少一层抽象,该抽象位于表示 statType 的方式和使用它的方式之间。

因此,您的程序不应该关心您是否调用 statType:

“总计”或“总计”或“总计”或“总计”

你需要关心的是你的 statType 在程序执行的某个时刻处于某种状态。如何实现状态的表示(带引号的字符串或数字)是实现的细节,您的 switch 语句不应该关心它。

例如,如果您决定将 statTypeName 更改为不带引号,会发生什么情况。比您必须转到依赖于它的引号的每一行代码并更改它。如果您以某种方式隐藏实现细节,则无需更改多于一行的代码。

也许是围绕 statTypes 设置抽象的一种方法? (为了清晰起见,进行了简化)

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文