存储变量会话php

Hi, i am having trouble in understanding how session works. I am trying to write a program that will repeatedly request a number to be entered and each time the number is entered the program is to print out:

  1. that number
  2. the sum of all the numbers entered
  3. the count in how many times I submitted

This is my code so far:

<?php
session_start();
if(isset($_session['count']))
{
    $_session['count'] = $count;
}else{
    $_session['count'] = 0; 
}
?>

<html>
    <head>
        <title>number</Title>
            <style>
            </style>
        <body>
            <form action = "numbers.php" method = "post">
                Numbers: <input type "text" name = "number" size = "6"/>
                <input type = "submit" value = "submit" name = "submit"/>
                <p>
            </form>
        </body> 
    </head>
</Html>
<?php   
    if(isset($_POST["submit"]))
    {
        $number = $_POST['number'];
        If (is_numeric($number))
        {   
            $count = $_session['count'] + $number;
            print "Last number entered: ".$number;
            print "<br>Total internal numbers: ".$count;
        }
    }   
?>

Im trying to store my $count variable so each time i submit it outputs the total sum of the numbers entered

On the start you are using undefined variable $count. Edit it to:

if(!isset($_session['count']))
    $_session['count'] = 0; 

Then add a new line:

If (is_numeric($number))
{   
    $count = $_SESSION['count'] + $number;
    $_SESSION['count'] = $count;
    print "Last number entered: ".$number;
    print "<br>Total internal numbers: ".$count;
}

OR second part edit to:

If (is_numeric($number))
{   
    $_SESSION['count'] += $number;
    print "Last number entered: ".$number;
    print "<br>Total internal numbers: ".$_SESSION['count'];
}
<?php   
    if(isset($_POST["submit"]))
    {
        $number = $_POST['number'];
        If (is_numeric($number))
        {   
            $count = $_session['count'] + $number;
            $_session['count'] = $count;
            print "Last number entered: ".$number;
            print "<br>Total internal numbers: ".$count;
        }
    }   
?>

You set $_session['count'] = $count; before you have a variable called $count. You have to update the session value after you've set $count like:

$count = $_session['count'] + $number;
$_session['count'] = $count;

You can then replace the top of your script with:

if(!isset($_session['count']))
{
    $_session['count'] = 0; 
}

Here's the working example. [TESTED]

You have to assign $_SESSION['count']=$count;

<?php
session_start();
//Code commented as not required.
/*if(isset($_session['count']))
{
    $_session['count'] = $count;
}else{
    $_session['count'] = 0;
}
*/?>

<html>
<head>
    <title>number</Title>
    <style>
    </style>
    <body>
    <form action = "" method = "post">
        Numbers: <input type "text" name = "number" size = "6"/>
        <input type = "submit" value = "submit" name = "submit"/>
        <p>
    </form>
    </body>
</head>
</Html>
<?php
if(isset($_POST["submit"]))
{
    $number = $_POST['number'];
    if (is_numeric($number))
    {

        $count = $_SESSION['count'] + $number;
        $_SESSION['count']=$count;
        print "Last number entered: ".$number;
        print "<br>Total internal numbers: ".$count;
    }
}
?>