Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Remember variables

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Remember variables

Postby baagio » Sun Dec 02, 2012 5:51 am

Hello,

for my homework from school I must determine how to remember php variables,
because we want to write script in php, which request input values from user. Then, with these values provide calculation and for user control (excercise mode) request input values again. These steps may repeat more times. However, classmate told me that in php language scripts request on their start input and then runs to the end, while in javascript i may have more inputs in my code and I always will be promted to enter input.
So in php I must remember variables between steps. It is true?

Can anybody help me with this? I am absolutly newbie in php and need to only present the solution in my classmate group.

Many thanks.
baagio
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sun Dec 02, 2012 5:20 am

Re: Remember variables

Postby egami » Mon Dec 03, 2012 9:09 am

Do a bit of research on sessions.

www.php.net/sessions
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Remember variables

Postby Barrikor » Mon Dec 03, 2012 6:06 pm

Before any output, (Html code, echo(), print() ), call session_start().

Then you can do something like this:

1st page:
Code: Select all

<?php
    session_start
();
    
    $food 
= 'pizza';
    
    $_SESSION
['food'] = $food;
?>


2nd page:
Code: Select all

<?php
    session_start
();
    
    $food 
= $_SESSION['food'];

    echo $food; // will print out 'pizza'
?>
User avatar
Barrikor
New php-forum User
New php-forum User
 
Posts: 16
Joined: Sun Dec 12, 2010 3:14 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron