PHP - 有没有办法在回声所在的位置回声文本?

PHP - Is there a way to echo text where the echo is?

所以基本上我想做的是从一个 .php 文件调用我的主 PHP 页面中的一个函数,该文件仅用于处理数据。该函数基本上调用回声。我想以一种真正呼应它所在位置的方式调用该函数,而不是调用它的地方。有办法吗?

php 在 index.php:

<?php
   function writeNot($text) {
       echo '<script type="text/javascript">',
       "addNotification(\"$text\");",
       '</script>';
   }
?>

php 在 process.php:

writeNot('The file has been successfully uploaded!');

我希望函数将 HTML(调用一些 Js)回显到 index.php,但我假设它回显到 process.php 文件,我不太想要。

以便您首先将文件 'index.php' 包含在 'process.php'

使用return代替回显

   <?php
   function writeNot($text) {
       return'<script type="text/javascript">',
       "addNotification(\"$text\");",
       '</script>';
   }
?>

然后使用 echo 打印出从函数

编辑的字符串 return
echo writeNote($text);

你应该调用index.php里面的函数来执行它