PHP注入HTML中的switch语句 [英] Switch statement in PHP injected HTML

查看:278
本文介绍了PHP注入HTML中的switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想做类似的事情,换句话说,在PHP模板中使用switch语句.使用if语句可以做到这一点.上面的代码在PHP 5.3中生成Parse error: syntax error, unexpected T_INLINE_HTML, expecting T_ENDSWITCH or T_CASE or T_DEFAULT. PHP文档提示应该可行.

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_DEFAULT in PHP 5.3. The PHP docs suggests that this should be possible.

有人可以确认这行不通吗,或者告诉我我在做什么错?

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

谢谢.

推荐答案

我在php文档中读到您的示例不起作用.

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

switch语句和以下命令之间的任何输出(包括空格) 第一种情况将导致语法错误.例如,这是无效的

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

尝试以下方法:

<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

这篇关于PHP注入HTML中的switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆