Jump to content

How to declare global variables?


kvijayhari

Recommended Posts

hi , i'm developing a web application and as for as i used databases , i have no probs in storing info or data.when i thinked about using files, i had a doubt about the data.I'm using different pages(Scripts) for a single application and in a single page i'm setting a value such as preferences on selecting radio buttons and i have to store different values based on the selection and i want to check the value of that variable in more other scripts where i would list all other details using the value.how can i access the value of the variable.Is there any special keyword for declaring a variable as a global variable which is accessible thru more scripts or pages.Note: I'm not using any classes and i'm simply using coded scripts.Pl help me...

Link to comment
Share on other sites

<?php$x = 1;$y = 2;function myFunction() {	global $x, $y;	$ans = $x + $y;	echo $ans;}?>

hi Adam, thanks for ur reply.it would fine and i know this.but what i want to know is that , suppose the above file is 'xx.php' and i'm having a file called 'yy.php' and i want to use the variables $x,$y in 'yy.php'here what i have to do?the two scripts xx.php and yy.php does two different things but the values from yy.php is needed to xx.php
Link to comment
Share on other sites

Regular or global variables do not hold their values from page to page. For example, on a.php if you set $var to some value, and then check the variable $var on b.php, it will not have the value that you set on a.php. This is pretty much what databases are for, you will want a.php to update the database with the new value, and then b.php will check the database and get the updated value out of it to use on the page.You can also store things in sessions, and the session is available on multiple pages. So on a.php if you set $_SESSION['var'] = "some value" then on b.php you can check $_SESSION['var'] and it will have the same value that you set on a.php. Sessions move from page to page, but the session ends when the user closes their browser. So, sessions are good for storing temporary data that you want on every page, but if you need to save the data for the next time the person comes back you still need to update the database.If you want to use sessions, check the session reference page here:http://www.php.net/manual/en/ref.session.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...