PHP 注入 HTML 中的 Switch 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6096552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:21:00  来源:igfitidea点击:

Switch statement in PHP injected HTML

phptemplatesswitch-statement

提问by Znarkus

<? switch ($var): ?>
  <? case 1: ?>
    It's 1!
  <? break ?>
<? endswitch ?>

I want to do something like that, in other words use the switch statement in PHP templates. This is possible with the if statement. The above code generates Parse error: syntax error, unexpected T_INLINE_HTML, expecting T_ENDSWITCH or T_CASE or T_DEFAULTin PHP 5.3. The PHP docssuggests that this should be possible.

我想做这样的事情,换句话说,在 PHP 模板中使用 switch 语句。这可以通过 if 语句实现。以上代码Parse error: syntax error, unexpected T_INLINE_HTML, expecting T_ENDSWITCH or T_CASE or T_DEFAULT在 PHP 5.3 中生成。在PHP文档表明,这应该是可能的。

Can someone confirm that this doesn't work, or rather, tell me what I'm doing wrong?

有人可以确认这不起作用,或者更确切地说,告诉我我做错了什么?

Thanks.

谢谢。

回答by j_freyre

I read in the php doc that your example should not work.

我在 php doc 中读到你的例子不应该工作。

Any output (including whitespace) between a switch statement and the first case will result in a syntax error. For example, this is invalid

switch 语句和第一个 case 之间的任何输出(包括空格)都将导致语法错误。例如,这是无效的

Try this instead:

试试这个:

<div>
<?php switch($variable): 
case 1: ?>
    <div>
    Newspage
    </div>
<?php break; ?>
<?php case 2: ?>
    </div>
    Forum
    <div>
<?php break; ?>
<?php endswitch; ?>
</div>

http://php.net/control-structures.alternative-syntax

http://php.net/control-structures.alternative-syntax

回答by Emyr

You have

你有

?>\n\n<?

after your switch which becomes

在你的开关变成

switch($var):
echo "\n\t";
case 1:

Think of your script as PHP with HTML in it, rather than embellished HTML. The PHP binary gets the file first.

将您的脚本视为带有 HTML 的 PHP,而不是修饰的 HTML。PHP 二进制文件首先获取文件。

You might like to use something like HEREDOC to echo out unescaped HTML.

您可能喜欢使用 HEREDOC 之类的东西来回显未转义的 HTML。

回答by Davide

I use this friendly formatting, tested and working:

我使用这种友好的格式,经过测试和工作:

<? switch($var) : case 1 : ?>

  <div>One</div>

<? break; case 2 : ?>

  <div>Two</div>

<? break; case 3 : ?>

  <div>Three</div>

<? break; endswitch; ?>

回答by Daniel

I had a similar issue when using a switch or a foreach statement with the new style short tags running on php 5.6.

在 php 5.6 上使用带有新样式短标签的 switch 或 foreach 语句时,我遇到了类似的问题。

in my case: changing

就我而言:改变

<?=

  switch ($number_of_columns) {
    case "1":
        echo '<div class="col-sm-12 col-md-12" >';
        break;
    case "2":
        echo '<div class="col-sm-12 col-md-6" >';
        break;
    case "4":
        echo '<div class="col-sm-6 col-md-3" >';
        break;
    default:
        echo '<div class="col-sm-6 col-md-4" >';
    }
?>

to

 <?php 
      switch ($number_of_columns) {
        case "1":
            echo '<div class="col-sm-12 col-md-12" >';
            break;
        case "2":
            echo '<div class="col-sm-12 col-md-6" >';
            break;
        case "4":
            echo '<div class="col-sm-6 col-md-3" >';
            break;
        default:
            echo '<div class="col-sm-6 col-md-4" >';
        }
    ?>

resulted in no syntax error i.e. syntax error, unexpected 'switch' (T_SWITCH)

导致没有语法错误,即语法错误,意外的“开关”(T_SWITCH)