Jump to content

Help with variables


melvnatic

Recommended Posts

How do you make it so that a variable can be accessed from two different scripts?

Let's say you set a variable in script #1, and you want to access that variable in script #2 in the same directory as script #1. How do you do that?

Also, how do you use includes? What do you put in include(???). Can you give me an example?

Thanks!
Link to comment
Share on other sites

Greetings Melvnatic

Ok...so lets say you have a function you want to use a lot. You create a php file with that function.

<?php
function bold($string) {
print "<b>" . $string . "</b>";
}
?>

Save that as bold.inc or just bold.php.

Then you access it in your script by calling it with

include "bold.inc";
or
include (bold.php);

Then you can use the function like:
bold ("This is some bold text right here!");

OR

If you wanted to use it as/with a variable:

$boldPrint = "This is some bold text right here!";
bold ($boldPrint);

I hope this helps.
Link to comment
Share on other sites

[!--quoteo(post=367050:date=Apr 20 2006, 05:11 PM:name=melvnatic)--][div class=\'quotetop\']QUOTE(melvnatic @ Apr 20 2006, 05:11 PM) [snapback]367050[/snapback][/div][div class=\'quotemain\'][!--quotec--]
How do you make it so that a variable can be accessed from two different scripts?

Let's say you set a variable in script #1, and you want to access that variable in script #2 in the same directory as script #1. How do you do that?

[/quote]

You have a variety of ways to persist variables. They are in no particular order:

-pass as either url params or form variables
-store them in a cookie
-store them session variables
-write them to a database, read them out
-write to a file/read out
-write to berkely db/ read out
--- etc other IO options
-Use the shared memory extension to write to a shared memory variables using shmop functions


With databases, there's obviously a whole array of options there.
Link to comment
Share on other sites

$_SESSION['variable_name'] = "This is my variable";

If you have performed all the tasks you need to, you can use session_destroy(); to kill the session, or you can delete individual session variables by using unset($_SESSION[variable_name]);


If you are going to use sessions, you need to have session_start(); AT THE TOP of your document, before anything else.

so your document would start something like;

<?php session_start();
// some php stuff
?>
<HTML>
<HEAD>

etc etc.
Link to comment
Share on other sites

You seem to be clear on the idea that php has page scope, and that a script creates variables and destroys them when the script is done executing. Thus there is a need for persistence/session. This isn't unique to PHP at all -- it's the nature of web development due to the design of HTTP.

To demystify PHP sessions, they are basically files stored on the server. When you create a session variable by assigning a value to it, that value is stored in a session file on the server. Once a session variable is created in Script A, script B can use it, so long as you session_start().

The typical way this is achieved is that php assigns the browser/user a unique ID number called the session ID. This is what ties someone to a session. In most cases this ID is set as a cookie, which is why it appears that magically these otherwise invisible variables are persisting. The advantage over cookies alone is that the values don't have to cross the internet --- only the session ID is needed.

Session variables are like any other variable in tnat you can change them or unset them. So your example above is exactly right.

To remove a sessions variable called 'foo' --

unset($_SESSION['foo']);

This example assumes that register globals is off, which it should be these days ;)

Read the session portion of the php manual for more info.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.