Forum Moderators: coopster

Message Too Old, No Replies

Using function variables outside functions

         

Hero_Doug

10:22 pm on Feb 12, 2004 (gmt 0)

10+ Year Member



I have a function that gets a page based on the ID grabbed out of the URL (Called $choice).

When the Page is set it pulls the correct URL and Title values out of an array and put's them into variables, but I can only use them inside the function, and not outside.

Here is the complete index page:

[PHP]<?php

//Start Session
session_start();
header("Cache-control: private");

//Get Settings
include_once("../config.php");

//Get choice from URL
$choice = $_GET['id'];

//Get Last Page they were on
$LastPage = $_SERVER["HTTP_REFERER"];

//If no choice exists or their not verified show login screen
if ($choice == "" or $_SESSION['AccessCode']!= $AccessCode or substr($LastPage, 0, $DomainLength)!= $SiteURL) {

include("login.php");

//If allowed then proceed
} elseif ($choice!= "") {

function getpage() {

global $choice;

//Pages values
$AdminMenu = array(
'ManageCategories' => array("manage-categories.php", 'Manage Categories'),
'ManageForums' => array("manage-forums.php", 'Manage Forums'),
);

//Assign Values and Destroy Array
$url = $AdminMenu[$choice][0];
$title = $AdminMenu[$choice][1];
unset($AdminMenu);

//Include Selected Page
include("$url");

}

include("template.php");

}

?>[/PHP]

Here is the template so you can see how it's being included.

[PHP]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title><?php echo $title;?></title>
<!--STYLE SHEET HAS BEEN REMOVED -->
</head>

<body>

<div class="box-wrap">

<div class="box-header">
Open Forum Release #1
<div class='right-header'><a class='mm' href='../index.php' target='_blank'>Return to Main board</a>
</div>
</div>

<div class="main-menu">
<?php include("admin-welcome.php");?>
</div>

<div class="columns-float">

<div class="column-one">
<div class="column-one-content">
<?php getpage();?>
</div>
</div>

<div class="column-two">
<div class="column-two-content">
<?php include("admin-menu.php");?>
</div>
</div>

</div>
</div><!-- close columns-float -->

<div class="clear">&nbsp;</div><!-- clear only required for NN4 compatibility -->
<div class="box-footer" align='center'>&nbsp;
</div>
<div class='copyright' align='center'>
Copyright &copy; 2004 Arcane Nexus. All rights reserved.<br />
<a href='#'>CSS</a> - Valid - <a href='#'>XHTML</a>
</div>

</div><!-- close box-wrap -->

</body>
</html>[/PHP]

Thanks for any help

jatar_k

11:34 pm on Feb 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



take a look at this
Variable scope [ca2.php.net]

you may need to add
global $url = $AdminMenu[$choice][0];
global $title = $AdminMenu[$choice][1];

if those are the vars you are trying to access

you also won't be able to echo $title until after you call your getpage function

coopster

12:48 am on Feb 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't believe you can assign a value to a variable while you are declaring it as a global. I remember stumbling my way through this once -- it was a learning experience to say the least.

The

global
keyword allows you to declare a variable global within a function. If you try to assign something to it you will get a parse error.

Try this and see:


Incorrect

function mytitle() {
global $title = 'Title';
}
mytitle();
echo '$title is: ' . $title . '<br />';

You should have received a parse error on the first line of the function. Now, if you first declare the variable, then assign the value, you can use it in the global scope:


Correct

function mytitle() {
global $title;
$title = 'Title';
}
mytitle();
echo '$title is: ' . $title . '<br />';

If this isn't the way it works, somebody please correct me. I banged my head on this for the better part of one morning when I first tried doing the same thing that jatar_k mentioned! I searched everywhere trying to get a more definitive conclusion other than the short sentence in the manual:


First, an example use of global:

Wow. Thanks fellas! Don't get me wrong, the manual, for the most part, is written quite well. However, the
global
keyword definition is lacking.

[edited by: coopster at 12:55 am (utc) on Feb. 13, 2004]

Hero_Doug

12:49 am on Feb 13, 2004 (gmt 0)

10+ Year Member



Oh god, I wasn't even paying attention to the order I was calling the variables in.

Thanks