duan205571 2012-07-05 15:58
浏览 493

使用PHP将变量从一个页面传递到另一个页面

Does anyone know what's wrong with the code below that prevents me from printing in File2.php the variable $myusername from File1.php.

I simply wanna print the variable $myusername, or if you know any other way of how to pass that variable that would be very helpful for me.

Here is my example:

File1.php

<?php 
$myusername=$_POST['myusername']; 

function getusername() 
{ 
   return $myusername;    
} 
?>

File2.php

<?php 
require_once('File1.php'); 
getusername(); 
?>
  • 写回答

3条回答 默认 最新

  • dongxian0421 2012-07-05 16:00
    关注

    You need to declare $myusername as global when you are trying to access it from within function, you also need to print is somehow (e.g. echo) ;)

    File1.php
    <?php 
    $myusername=$_POST['myusername']; 
    
    function getusername() 
    { 
       global $myusername; // declare as global
       return $myusername;    
    } 
    ?>
    
    File2.php
    <?php 
        require_once('File1.php'); 
        echo getusername();        // echo value
    ?>
    

    Read more: PHP Variable scope. You could also pass it via $_SESSION.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: