【问题标题】:PHP echo with variables and functions带有变量和函数的 PHP 回显
【发布时间】:2017-12-06 06:53:48
【问题描述】:

我正在尝试创建一个如下所示的自动刷新 url,但它似乎不起作用。

<?php 
$lastid = 12345
redirect = echo the_permalink(get_option( 'cts_return_page' )).'?transid='.$lastid';
echo '<meta http-equiv="refresh" content="1; url='$redirect'">';
?>

我希望它重定向/刷新到的网址是http://example.com/page-from-options?transid=12345

对我做错了什么有什么建议吗?

【问题讨论】:

  • 你遇到什么问题??
  • 你用的是WordPress,对吧?

标签: php variables url echo page-refresh


【解决方案1】:

您在代码中犯了几个错误,例如

  • 你不应该像$redirect = echo一样使用echo
  • 您必须使用$redirect 而不是redirect
  • 无需在$lastid 之后使用'
  • 并在12345 之后使用;
  • edit.. 连接,例如url='.$redirect.'

希望对你有帮助。

<?php
  $lastid = 12345;
  $redirect = the_permalink(get_option( 'cts_return_page' )).'?transid='.$lastid;
 echo '<meta http-equiv="refresh" content="1; url='.$redirect.'">';
?>

【讨论】:

  • @RïshïKêshKümar 语法错误已解决.. 大声笑 :D 我没有运行我刚刚直接编辑的代码,这就是我犯错的原因.. 现在我只是检查本地主机中的代码,它现在工作正常。 . 感谢您的建议:)
  • 它不适用于localhost ...
  • @RïshïKêshKümar 你在 wordpress 中检查过吗?
  • 不..实际上它没有在我的本地机器上设置..所以!!我只是在本地服务器上尝试并得到错误。现在等待Jeremy的回复
【解决方案2】:

解决方案:

  <?php
    $lastid = 12345;

    $redirect = the_permalink(get_option( 'cts_return_page' )).'?transid='.$lastid;

    echo " <meta http-equiv='refresh' content='1'; url ='<?php $redirect ?>' > ";
  ?>

<?php

     //$redirect = get_permalink(get_option( 'cts_return_page' )).'?transid='.$lastid;
     //$url = "http://example.com/page-from-options?transid=12345";

       $lastid = 12345;

    // Retrieve the `cts_return_page`, and storing it in a variable.
       echo $get_options = get_option('cts_return_page');

    //Displays the URL to the post:
      echo $redirect = the_permalink($get_options).'?transid='.$lastid;


   //echo " <meta http-equiv='refresh' content='1'; url ='http://example.com/<?php $redirect ?>' > ";

?>

<meta http-equiv='refresh' content='1' url='http://example.com/<?php echo $redirect; ?>' >

问题:

  • 第一个 Syntax Error: 分号 (;) 在 $lastid = 12345 之后缺少以及您在此处使用字符串值 为什么? $lastid 它的整数值像这样使用例如:$lastid = 12345;

  • 当您分配值时(=)然后不要使用echoecho 实际上是用来打印值的,所以避免使用。

  • -

【讨论】:

    【解决方案3】:

    你不能用 echo 语句设置任何变量值,首先你需要设置变量 $redirect 值,然后你可以 echo $redirect

    <?php 
       $lastid = '12345'; // use semicolon
       $redirect = the_permalink(get_option('cts_return_page'))."?transid=".$lastid; // you made mistake here `echo` should not goes here
       echo '<meta http-equiv="refresh" content="1; url="'.$redirect.'">';
    ?>
    

    【讨论】:

    • 你没有提到 OP 会在哪里改变或者是什么问题。
    【解决方案4】:

    您使用的是 WordPress,对吗?我可以通过the_permalinkget_option 函数猜测。如果是这样,下面的代码应该适合你。

    更多解释:[编辑] 有关您所犯的 PHP 错误,请参阅 Nirav Joshi 的文章。此外,当您使用 WordPress 时

    • the_permalink 实际上回显了 url。您应该使用get_permalink 将网址存储在$redirect 变量中。

    使用此代码:

        $lastid = 12345;
        $redirect = get_permalink(get_option( 'cts_return_page' )) . '?transid=' . $lastid;
        echo '<meta http-equiv="refresh" content="1; url=' . $redirect . '">';
    

    【讨论】:

    • 你没有提到 OP 会在哪里改变或者是什么问题。
    • 谢谢。当你发表评论时,我正在编辑答案。
    • 谢谢@SougataBose。更正了变量名和函数名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    相关资源
    最近更新 更多