每次页面被调用或打开时增量变量? [英] increment variable each time page gets called or opened?

查看:73
本文介绍了每次页面被调用或打开时增量变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <!doctype html> 
< html>
< head>
< title>索引< / title>
< / head>
< form action =newquestion.phpmethod =post>
问题< input type =textname =问题>
< input type =submit>
< / form>
< body>
< / body>
< / html>

PHP文件...

 <?php 

static $ q = 1;

echo $ q;

$ q ++;

?>

我是PHP新手。这会不会在每次调用newquestion.php时将$ q递增1?如果不是每次页面(newquestion.php)被调用或打开时如何增加这个变量?在加载此页面时会执行一个页面。所以每次你的脚本一行一行地执行。您无法通过您正在尝试的过程来计算页面加载数量。

您可以按照下列流程之一进行操作:

<1>您可以将其保存到数据库中,加载时你可以执行查询来增加计数值。

2)你可以这样做:

 在session_start(); 
if(isset($ _ SESSION ['view']))
{
$ _SESSION ['view'] = $ _ SESSION ['view'] + 1;
}
else
{
$ _SESSION ['view'] = 1;
}


<!doctype html>
<html>
<head>
<title>Index</title>
</head>
<form action="newquestion.php" method="post">
Question <input type="text" name="question">
<input type="submit">
</form>
<body>
</body>
</html>

PHP file...

<?php

static $q = 1;

echo $q;

$q++;

?>

I'm new in PHP. Will this not increment $q by 1 each time "newquestion.php" is called? if not how to increment this variable each time this page(newquestion.php) gets called or opened?

解决方案

Every php script inside in a page is executed when you are loading this page. So everytime your script is executes line by line. You can not count page loading number by the process you are trying.

you can follow one of the process below:

1) you can save it to the database and each time when it is loading you can execute query to increment the count value.
2) you can do it by session like this:

session_start();
if(isset($_SESSION['view']))
{
 $_SESSION['view']=$_SESSION['view']+1;
}
else
{
 $_SESSION['view']=1;
}

这篇关于每次页面被调用或打开时增量变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆