duanca3415 2012-01-05 08:18
浏览 41
已采纳

php回显函数内部的变量

I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?

Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.

<?php echo rating_bar('$as',5) ?>

What I get at html file:

<div id="unit_long$as"> 

instead of the item id in place of $as.

  • 写回答

2条回答 默认 最新

  • duansai1314 2012-01-05 08:44
    关注

    Single Quotes do not support variable replace,

    $as = "test";
    echo '$as'; //$as in your end result
    echo "$as"; // test in your end result
    
    echo $as; // test in your end result
    
    //For proper use
    echo " ".$as." "; // test in your end result
    

    Update for newer PHP versions you should not use Template Syntax

    echo "{$as}"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?