Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

How to "globalize" PHP variables?

I have a page named ChangeApprovalInfo.php - It has a function called Row_Rendered as follows;

function Row_Rendered() {

    // To view properties of field class, use:
    //var_dump($this-><FieldName>);

    $RecordOwner = $this->RequestUser->CurrentValue;  
        echo $RecordOwner;
} 

Echoing $RecordOwner gets me the data I will need for a sql query on another page....

I have another page called ChangeApprovalEdit.php - This page has

<?php include_once "ChangeApprovalinfo.php" ?>

at the top of the file.

ChangeApprovalEdit.php has a function where I need the $RecordOwner variable as defined in ChangedApprovalInfo.php

If I add "echo $RecordOwner" on the ChangeApprovalEdit.php page, I get an error saying it's an unknown variable. My understanding is that I need to "make it global" or some such business. I know very little about PHP and the pages I am editing are long and complex. (to me, at least)

  • What do I need to do? I know that the information I have provided might not be enough to answer the question. I don't know enough to even know exactly what I need to ask. If more information is needed, I will edit and follow up.

pastebin of the files

ChangeApprovalInfo.php = http://pastebin.com/bSRM1wwN
ChangeApprovalEdit.php = http://pastebin.com/AStG9pqb

EDIT: Changing Row_Rendered to this seems to be more effective. I'm having trouble seeing WHERE I can later echo this variable... but I'm getting somewhere with this...

function Row_Rendered() {
    // To view properties of field class, use:
    //var_dump($this-><FieldName>);
    $GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;  
} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't echo variables from functions, which just outputs them to the standard output. return them from the function so you can use the value elsewhere as well.

function Row_Rendered() {
    $RecordOwner = $this->RequestUser->CurrentValue;  
    return $RecordOwner;
} 

Then instead of

$obj->Row_Rendered();

use

echo $obj->Row_Rendered();

and if you want to use the value elsewhere, use

$value = $obj->Row_Rendered();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...