Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在div中显示PHP变量_Php_Html_Css - Fatal编程技术网

在div中显示PHP变量

在div中显示PHP变量,php,html,css,Php,Html,Css,我在计算器的div中显示输入时遇到问题。它没有显示任何内容。我做错了什么?我对PHP非常陌生,所以请尽可能多地解释,以便我可以从中学习。如果可能,则应不断刷新变量,使其始终是最新的输入。这是我的代码,问题可以在代码的PHP部分找到: HTML: jsFiddle: 对于PHP,您需要一个PHP环境,比如apache和PHP(对于初学者:使用“xampp”) 此文件通常需要文件ending.php,以便服务器端解析器可以解析文件中的php代码。 <?php $input =

我在计算器的div中显示输入时遇到问题。它没有显示任何内容。我做错了什么?我对PHP非常陌生,所以请尽可能多地解释,以便我可以从中学习。如果可能,则应不断刷新变量,使其始终是最新的输入。这是我的代码,问题可以在代码的PHP部分找到:

HTML:

jsFiddle:


对于PHP,您需要一个PHP环境,比如apache和PHP(对于初学者:使用“xampp”)

此文件通常需要文件ending.php,以便服务器端解析器可以解析文件中的php代码。


 <?php 
      $input = "0";

      function showInput($input)    {
          echo $input;
      };

      while () {
          showInput($input);
     };
 ?>
在PHP中声明变量时不使用
var
。只要
$
。并了解“范围”。您试图使用在函数外部、函数中声明的变量。这不管用。因此,在我的代码中,我将该变量作为参数传递给函数内部使用

我给了你很多关键词和基本解释让你做更多的挖掘


编辑:我不认为
while
循环会做任何事情。什么时候?有些东西需要放在括号里。你在循环什么?

这里我从你的代码中提取了一个片段

        <div id="display">
                <?php 
                    var $input = "0";

                    function showInput()    {
                        echo $input;
                    };

                    while () {
                        showInput();
                    };
                ?>
        </div>
上面的一行只是初始化输入变量的值,您可以用任何您喜欢的值更改0

echo $input;

这将呼出上面PHP代码第一行中输入变量中保存的值。如果您已经声明了另一个变量,您也可以回显它,只需在$sign之后更改变量名即可。$签名让PHP知道它的变量名正在启动。

将这些部分更正为这些部分,代码将正常工作

            <div class="row">
            <div id="9" class="button">
                9
            </div>

            <div id="6" class="button">
                6
            </div>

            <div id="3" class="button">
                3
            </div>

            <div id="equal" class="button">
                =
            </div>
        </div>

        <div class="row">
            <div id="plus" class="button">
                +   
            </div>

            <div id="minus" class="button">
                -
            </div>

            <div id="multiply" class="button">
                x
            </div>

            <div id="divide" class="button">
                /
            </div>

            <div id="display">
                <?php

                function showInput() {
                    $input = "0";

                    echo $input;
                }
               ?>
            </div>
        </div>

9
6.
3.
=
+   
-
x
/

您尚未在
while
循环中设置参数<代码>而具体是什么?您还需要查找变量范围。函数无法访问函数中不包含的变量,在本例中,请在中解析它们。乙二醇

<?php
    $input = "0";

    function showInput($input)
    {
        echo $input;
    };

    showInput($input);

?>


下面是一个使用我的编辑的代码示例

JSFIDDLE不适用于php。您需要了解PHP是一种服务器端语言。因此,它不能像您描述的那样工作。您可能对使用客户端语言(如JavaScript)更感兴趣。您的PHP代码应该至少触发一个解析错误。您似乎混淆了web开发中涉及的几种语言。你真的在你的计算机上安装了PHP吗?如果你想要动态效果,除了javascript,你不能使用PHP。在php中,声明变量时不需要使用
var
。这将导致错误。
$input = "0";
echo $input;
            <div class="row">
            <div id="9" class="button">
                9
            </div>

            <div id="6" class="button">
                6
            </div>

            <div id="3" class="button">
                3
            </div>

            <div id="equal" class="button">
                =
            </div>
        </div>

        <div class="row">
            <div id="plus" class="button">
                +   
            </div>

            <div id="minus" class="button">
                -
            </div>

            <div id="multiply" class="button">
                x
            </div>

            <div id="divide" class="button">
                /
            </div>

            <div id="display">
                <?php

                function showInput() {
                    $input = "0";

                    echo $input;
                }
               ?>
            </div>
        </div>
<?php
    $input = "0";

    function showInput($input)
    {
        echo $input;
    };

    showInput($input);

?>