【问题标题】:Smarty PHP - If statement with variable in double quotes with slashSmarty PHP - 带有斜线双引号的变量的 If 语句
【发布时间】:2018-11-06 22:28:10
【问题描述】:

我正在用 PHP Smarty 编写 if 语句,但语法有问题。

$post['user_name'] = Chris

我想要这样,如果 $post['post_type'] == "hiddenshared" 并且 URL URI 等于 "/$profile['user_name']" 在这种情况下为 /chris,那么什么都不会显示。我很难将 / 连接到 $profile['user_name'] 变量。如何在$profile['user_name'] 变量之前添加/,以使$profile['user_name'] 显示其中包含的字符串而不是文本$profile['user_name']?还是我不应该使用REQUEST_URI

这是我的代码

{if $post['post_type'] == "hiddenshared" && ($smarty.server.REQUEST_URI === "/$profile['user_name']")} 

{else}
    <!-- post -->
    {$smarty.server.REQUEST_URI}
    {$profile['user_name']}
{/if}

if 语句将/$profile['user_name'] 读取为/$profile['user_name'],而它应该将其读取为/Chris。我应该如何解决这个问题?

【问题讨论】:

  • 试试$smarty.server.REQUEST_URI === "/".$profile['user_name']
  • 我刚试了一下,没用。它必须是 "/chris" 和 "/"。不幸的是,$profile['user_name'] 并没有把整个事情放在引号中。
  • 然后尝试$smarty.server.REQUEST_URI === '"/'.$profile['user_name'].'"',非常农民的方式:D
  • 添加 $smarty.server.REQUEST_URI === '"/'.$profile['user_name'].'"' 导致 HTTP 500 错误,所以我尝试删除第一对引号和然后被系统读取为:/'.Array['user_name'].',也不起作用
  • 感谢您对 Mojo 的帮助,非常感谢!

标签: php string if-statement concatenation smarty


【解决方案1】:

您可以在双引号字符串中使用大括号分隔符

$smarty.server.REQUEST_URI === "/{$profile['user_name']}"

【讨论】:

  • 这就像一个魅力!现在看起来很明显。谢谢 Scuzzy!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多