Jump to content

PHP and variables help!!


cuprasteve

Recommended Posts

Hi

If I have a PHP page called page1.php thats sets a variable, ie

$myvariable=1;

and i have a PHP page call page2.php that has an include(page1.php);

will it be able to know the value of the variable that is created in the other page?

If not is there a way i can get it to do this?

thanks
steve
Link to comment
Share on other sites

[quote author=Orio link=topic=106371.msg425299#msg425299 date=1157025692]
page2.php will know that $myvariable=1.


You can test it:
page1.php will contain:
[code]<?php
$var="test";
?>[/code]

page2.php will be:
[code]<?php
include("page1.php");
echo $var;
?>[/code]


Orio.
[/quote]


that didnt seem to work for me, any ideas?
Link to comment
Share on other sites

[quote author=cuprasteve link=topic=106371.msg425306#msg425306 date=1157026510]
is there a way of doing it without the include? like make it accessible just for that variable and now have to try and access the whole page?
[/quote]

[code]<?php
include("page1.php");
echo $var;
?>[/code]

I may be wrong but you aren't getting all of the information and/or variables on the entire page.  Only the variable mentioned after 'echo' *found* on "page1.php"--where the variable was defined.
Link to comment
Share on other sites

Easiest way to get your head around include's is to picture them as cut-and-pastes.

page2 includes page1

it achieves exactly the same functionality as if you were to cut and paste the contents of page1 into page2. (bar open and close tags)
Link to comment
Share on other sites

Why don't you try using the empty or isset functions. These are designed to test if a variable is empty/exists respectively.

Try this:
[code]<?PHP
include ( 'page1.php' );

if( ! isset( $var ) )
{
    echo ' Variable isn't set!';
}
else
{
  echo ' Variable $var is '.$var;
}
?>
[/code]

Normally a variable defined in one page which is included or required into another still exists and can be used in the page which it is included into. As Jenk said above consider it like a copy and paste. All the code from page 1 - except the opening and closing tags (<?PHP ?>) - is copied and used in page 2 excatly as it appears in page 1.

Hope that helps ;)
Cold
Link to comment
Share on other sites

I have this code in a page called album_common

[code]if (!isset($album_root_path) || empty($album_root_path))
{
$album_root_path = $phpbb_root_path . 'album_mod/';
}

include($album_root_path . 'album_functions.' . $phpEx);
include($album_root_path . 'album_hierarchy_functions.' . $phpEx);
include($album_root_path . 'clown_album_functions.' . $phpEx);[/code]

and then further down

[code]$template->assign_vars(array(
'GLOBTEST'=> $testvar,
)
);[/code]

then in album_hierarchy_functions.php which you can see it including in that previous bit of code i Have $testvar="test";

But GLOBTEST in my template outputs nothing, if i stick $testvar="test"; in the same page it works fine
Link to comment
Share on other sites

If you want to use arrays in your site I highly suggest you read up on them..  http://www.oreilly.com/catalog/progphp/chapter/ch05.html

This will explain a lot.  I hope.  Other than that I can only assume you're not including the right page or you're mispelling the variable.
Link to comment
Share on other sites

make the variable global or return it

Examples:

[b]Global variable[/b]
[code=php:0]function foobar($bar)
{
    global $foo;

    $foo = 'You passed ' . $bar . ' to foobar()';
}

foobar('Hello World');

echo $foo;
// returns 'You passed Hello World to foobar()'[/code]


[b]Return variable[/b]
[code=php:0]function foobar($bar)
{
    global $foo;

    return 'You passed ' . $bar . ' to foobar()';
}

echo foobar('Hello world');
// returns 'You passed Hello World to foobar()'[/code]
Link to comment
Share on other sites

hmm iv messed around with what i think will work from your examples but cant seem to get anywhere, let me show you.

The top of the function is as follows

[quote]function album_display_admin_index($cur = ALBUM_ROOT_CATEGORY, $level = 0, $max_level = -1, $column_offset=1)

{
global $db, $template, $phpEx, $lang, $images, $album_data, $userdata, $user_id, $cat_id, $var;
$var = "Dfg";
static $username = '';

// display 'the' level
$AH_this = isset($album_data['keys'][$cur]) ? $album_data['keys'][$cur] : ALBUM_ROOT_CATEGORY; //-1;

if (defined('IN_ADMIN'))
{
$admin_url = "admin_album_cat." .$phpEx;
$is_root = false;
}
else
{
$admin_url = "album_personal_cat_admin." .$phpEx;
$is_root = (($AH_this == ALBUM_ROOT_CATEGORY || $AH_this == 0)) ? true : false;
}

// root level
if ($AH_this == ALBUM_ROOT_CATEGORY)
{
$level = ALBUM_ROOT_CATEGORY;[/quote]

you can see a few lines down where I have tried to make $var= something.

below the function I have

echo $var

But i get nothing. As you can see iv added it to the Global list
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.