代码之家  ›  专栏  ›  技术社区  ›  Jeremy

带有变量和函数的PHP echo

  •  1
  • Jeremy  · 技术社区  · 7 年前

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

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

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

    4 回复  |  直到 7 年前
        1
  •  3
  •   Nirav Joshi    7 年前

    你在代码中犯了几个错误,比如

    • 你不应该使用 echo $redirect = echo .
    • 你必须使用 $redirect redirect .
    • 无需使用 ' 之后 $lastid
    • ; 12345 .
    • 编辑 .. 喜欢 url='.$redirect.'

    <?php
      $lastid = 12345;
      $redirect = the_permalink(get_option( 'cts_return_page' )).'?transid='.$lastid;
     echo '<meta http-equiv="refresh" content="1; url='.$redirect.'">';
    ?>
    
        2
  •  1
  •   RïshïKêsh Kümar    7 年前

    解决方案:

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

    • 当您指定值时( = )那么不要使用 echo , 实际上用于打印值,因此避免使用。

        3
  •  0
  •   Nirav Joshi    7 年前

    您不能使用echo语句设置任何变量值。首先,您需要在可以echo$redirect之后设置变量$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.'">';
    ?>
    
        4
  •  -1
  •   Junaid    7 年前

    你在用WordPress,对吗?我可以猜到 the_permalink get_option

    更多说明:[编辑]

    • the_permalink公司 实际上与url相呼应。你应该使用 get_permalink 将url存储在 $redirect 变量

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