Jump to content

echo variable from external script


Recommended Posts



I am currently trying to get a variable created inside a require_once script to be echoed inside the main page that called the require. The script below is a basic idea of what i want to do. I just want to be able to create a basic variable none of this session stuff as its makes life harder at the moment.


Thank guys, hope the snippet below gives you a better idea.


Main Code:

// Variable I want to be echo "NOT WORKING"
echo $var;


External PHP Code:

//Function gets called by previous code to create the needed variable
function createTheVariable(){
$var = "I am the variable to be called";
return $var;

Link to comment
Share on other sites

Where exactly are you calling the createTheVariable() function at (the code you posted only contains the function definition) and assigning the value it returns to the the $var variable?


You would need a statement like, $var = createTheVariable(); for your existing code to work. Also, you apparently don't have php's error_reporting set to E_ALL and display_errors set to ON in your master php.ini on your development system to get php to help you. You would be getting an undefined variable error message when you try to echo $var, that would alert you that $var has not been assigned a value.

Link to comment
Share on other sites

There's one function before the function where the variable is created so that code doesn't work just pulls up undefined variable. The variable will be returned from a different function that been called first.


external script (defaultList is called then getCategoryList returns the variable):

function defaultList(){
$sortBy = "opt_1";
$order = "DESC";
$result = dbQuery("SELECT opt_1, opt_2 
				   FROM test_tbl
				   ORDER BY $sortBy $order");

function getCategoryList($result){
       //MYSQL code happens and needed stuff is turned into the variable list
return $list;

Link to comment
Share on other sites

There may also be a misunderstanding of scope. When you declare a variable inside a function, like your $var in this example, it doesn't exist outside of that function. You would need to call the function and have it return the value that you wanted from inside the function. You have the function returning, but you are not assigning it to a variable outside the function.

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.

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.