通过PHP函数传递变量值不工作


passing variable value through php function not working

我试图通过一个函数传递$page="somepage";的值,假设

function example($page)
        {
                    echo $page;
   ?>
   <input type="radio" name="smoke" value="Y"> Yes
   <input type="radio" class="ml_10" name="smoke" value="N"> No
   <input type="radio" class="ml_10" name="smoke" value="O"> Occasionally
   <?php
        }
example ($page);

当我尝试回显页面时,它应该是somepage,而不是1。但是如果我在调用函数时手动编写页面,它会正确打印例如example ("somepage")

您的$page变量在调用example()函数之前包含数字1
所以,你必须让它包含"somepage"值。

您的example()函数包含一些将"somepage"转换为1的代码。

很可能是

$page = include $page;

你不知道为什么在你的问题中遗漏的那一行

无论哪种方式,问题都是过于本地化,因此不适合Stack Overflow

只要在调用函数之前声明$page,您要做的事情就会起作用。

$page = "somepage";
example($page);

function example($page)
{
    echo $page;
    ?>
    <input type="radio" name="smoke" value="Y"> Yes
    <input type="radio" class="ml_10" name="smoke" value="N"> No
    <input type="radio" class="ml_10" name="smoke" value="O"> Occasionally
    <?php
}