duangouyan3328 2018-05-03 12:13
浏览 27

变量如何在PHP中运行?

Can anyone explain how variables work in PHP?

When I wrote the script to a game (San Andreas Multiplayer, Pawn language), i used variables like this:

new Health = 100;

but it was incorrect, because it was the same variable for all players. So, the correct way was:

new Health[NUMBER_OF_PLAYERS];
Health[player_ID] = 100;

And now, my question is: How do it works in PHP?

When I create a variable

$x = 100;

and two buttons in JS:

  • buttonA - that increase value by 1
  • buttonB - display current value of variable $x

What will happen, when one user click buttonA few times? The other users will see (by clicking buttonB) value higer than 100, because there is only one variable $x on the server? Or they will see 100, because each user has "own variable"?

Is there a difference, when it's working by AJAX, or directly inserting PHP code into .html file (using <?php tag)?

  • 写回答

1条回答 默认 最新

  • dongsaohu6429 2018-05-03 12:19
    关注

    PHP is a server side language, as such each new connection to the server would have its own variables.

    By default, variables are not persistent unless stored in some form, how they are stored, it is completely your choice.

    You can globalise the variables by storing them in one centralised location and updating / retrieving them as and when you need for each user.

    Using AJAX wouldn't solve the persistence issue of the variable, however, it would cut out the page reload aspect.

    Reading Material

    Session

    Cookies

    Database

    Memcache

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?