UrbanDweller Posted April 20, 2012 Share Posted April 20, 2012 Hey, 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: <body> <?php require_once("makesVariable.php"); <div> // Variable I want to be echo "NOT WORKING" echo $var; </div> ?> </body> External PHP Code: <?php //Function gets called by previous code to create the needed variable function createTheVariable(){ $var = "I am the variable to be called"; return $var; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/261319-echo-variable-from-external-script/ Share on other sites More sharing options...
xyph Posted April 20, 2012 Share Posted April 20, 2012 There's a syntax error in your example. <body> <div> <?php require_once("makesVariable.php"); $var = createTheVariable(); echo $var; ?> </div> </body> Quote Link to comment https://forums.phpfreaks.com/topic/261319-echo-variable-from-external-script/#findComment-1339082 Share on other sites More sharing options...
MMDE Posted April 20, 2012 Share Posted April 20, 2012 No need to put it inside a function, but if you put it inside a function, then you gotta use said function. In main code: echo createTheVariable(); instead of: echo $var; Quote Link to comment https://forums.phpfreaks.com/topic/261319-echo-variable-from-external-script/#findComment-1339083 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2012 Share Posted April 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261319-echo-variable-from-external-script/#findComment-1339087 Share on other sites More sharing options...
UrbanDweller Posted April 20, 2012 Author Share Posted April 20, 2012 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"); getCategoryList($result); function getCategoryList($result){ //MYSQL code happens and needed stuff is turned into the variable list return $list; } } Quote Link to comment https://forums.phpfreaks.com/topic/261319-echo-variable-from-external-script/#findComment-1339090 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2012 Share Posted April 20, 2012 LOL, three different people posted code showing how you would call your function and either assign the value it returns to a program variable or directly echo the value that your function returns. Quote Link to comment https://forums.phpfreaks.com/topic/261319-echo-variable-from-external-script/#findComment-1339091 Share on other sites More sharing options...
UrbanDweller Posted April 20, 2012 Author Share Posted April 20, 2012 WELL, it doesnt seem to be working. Quote Link to comment https://forums.phpfreaks.com/topic/261319-echo-variable-from-external-script/#findComment-1339095 Share on other sites More sharing options...
batwimp Posted April 20, 2012 Share Posted April 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261319-echo-variable-from-external-script/#findComment-1339096 Share on other sites More sharing options...
UrbanDweller Posted April 20, 2012 Author Share Posted April 20, 2012 Well i found a good example on global variable which seems tidy and is working too. Quote Link to comment https://forums.phpfreaks.com/topic/261319-echo-variable-from-external-script/#findComment-1339098 Share on other sites More sharing options...
batwimp Posted April 20, 2012 Share Posted April 20, 2012 Prepare for a global variable flame from the guru(s). Quote Link to comment https://forums.phpfreaks.com/topic/261319-echo-variable-from-external-script/#findComment-1339101 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2012 Share Posted April 20, 2012 If this topic is solved, the forum's Topic Solved button is at the lower-left-hand side of the page. Quote Link to comment https://forums.phpfreaks.com/topic/261319-echo-variable-from-external-script/#findComment-1339110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.