Jump to content

Call function variable outside function


jaymc

Recommended Posts

I need to use a variable that I have set within a function, outside of that function

 

e.g

 

<?
function beans () {

$cheese = "nice";

echo "hello";
}

echo $cheese;

?>

 

 

Is this possible, something like setting the function globally so it can be used outside of the function container. It must use the above syntax, please dont suggest echoing $cheese within the function  :-[

Link to comment
Share on other sites

Impossible. All variables within a function are local to that function only. Here's a couple ways to achieve what you want though

 

Pass by reference

<?php
function foo ( & $bar ) {

    $bar = 'Hello world';

}

$var = 'sample';
echo $var;
foo($var);
echo $var;
?>

 

Using the GLOBAL super variable.

 

<?php
function foo ($bar) {

    $GLOBALS[$bar] = 'Hello World';

}

$var = 'sample';
echo $var;
foo('var');
echo $var;
?>

Link to comment
Share on other sites

That allows me to use the variable SET outside of a function, inside a function

 

I need to set the variable inside the function, or atleast change it, and use it outside the function

 

Like my example above

Link to comment
Share on other sites

Look at my pass by reference example as well as my super variable example. Both change variables in the global scope within the function.

Ok the GLOBALS is working, but 2 questions

 

1: Isnt this some kind of security hazard? I have always learned to turn off in php.ini

2: How can I assign multiple variables inside a function..

Link to comment
Share on other sites

<?php
function blah() {
   global $somevar;
   
   $somevar = "sometext";
}

blah();
echo $somevar;
?>

Cant have that as I am echoing info inside the function, so it will return everything

Link to comment
Share on other sites

1: everything is a potential security hazard

2: same way you would make multiple vars anywhere else.  declare/assign them individually or as an array, if it suits you. 

 

<?php
function blah() {
   global $somevar;
   
   $somevar = "sometext";
}

blah();
echo $somevar;
?>

Cant have that as I am echoing info inside the function, so it will return everything

 

I don't see how that matters...my script declares it inside your function just like you asked.  It's declared as a global variable so you can access it outside the function just like you asked.  What does it matter what you do inside your function? Do whatever you want to do. Return whatever you want to return.  Notice inside my script you aren't returning anything, and yet you can still echo $somevar outside the function. 

Link to comment
Share on other sites

1: everything is a potential security hazard

2: same way you would make multiple vars anywhere else.  declare/assign them individually or as an array, if it suits you. 

 

<?php
function blah() {
   global $somevar;
   
   $somevar = "sometext";
}

blah();
echo $somevar;
?>

Cant have that as I am echoing info inside the function, so it will return everything

 

I don't see how that matters...my script declares it inside your function just like you asked.  It's declared as a global variable so you can access it outside the function just like you asked.  What does it matter what you do inside your function? Do whatever you want to do. Return whatever you want to return.  Notice inside my script you aren't returning anything, and yet you can still echo $somevar outside the function. 

 

The problem was I had to execute that function before I call the variable, therefor it has not yet been parsed, thats why that was not working

 

I cant move the function to the top of my script as it just echoes the contents of the function where ever is is placed, it needs to be in the center of the page wrapped in a div..

 

I have 90 odd pages too, $page, so cant really rejig them

 

Link to comment
Share on other sites

1: everything is a potential security hazard

2: same way you would make multiple vars anywhere else.  declare/assign them individually or as an array, if it suits you. 

 

<?php
function blah() {
   global $somevar;
   
   $somevar = "sometext";
}

blah();
echo $somevar;
?>

Cant have that as I am echoing info inside the function, so it will return everything

 

I don't see how that matters...my script declares it inside your function just like you asked.  It's declared as a global variable so you can access it outside the function just like you asked.  What does it matter what you do inside your function? Do whatever you want to do. Return whatever you want to return.  Notice inside my script you aren't returning anything, and yet you can still echo $somevar outside the function. 

Same as above, I need to call that function before I set <title>$pageTitle</title>

 

If i do that, I get my page content in the HTML HEAD

Link to comment
Share on other sites

Register globals is different than using the GLOBALS super variable.

 

You should avoid using globals altogether... as you can easily lose track of whats declared/defined where. Your best bet is to use a class in this case

 

<?php

class test {

# Declare some variables that are public
public $foo, $bar;

# This will be executed as soon as you create the class
# For older PHP versions use 'public function test'
public function __construct ( $someValue = FALSE ) {
	$this->foo = 'some value';
	$this->bar = $someValue;
}

# Internal function
public function changeFoo ( $someValue ) {

	echo '<br>Foo used to be ' . $this->foo;
	$this->foo = $someValue;
	echo '<br>Foo is now ' . $this->foo;

}

}

# Now we create the class

$var = new test( 'another value' );

echo $var->foo . '<br>';
echo $var->bar . '<br>';

$var->foo = 'something else';
echo $var->foo . '<br>';

# Or we can use internal functions
$var->changeFoo( 'classes are very handy!' );

?>

Link to comment
Share on other sites

Register globals is different than using the GLOBALS super variable.

 

You should avoid using globals altogether... as you can easily lose track of whats declared/defined where. Your best bet is to use a class in this case

 

<?php

class test {

# Declare some variables that are public
public $foo, $bar;

# This will be executed as soon as you create the class
# For older PHP versions use 'public function test'
public function __construct ( $someValue = FALSE ) {
	$this->foo = 'some value';
	$this->bar = $someValue;
}

# Internal function
public function changeFoo ( $someValue ) {

	echo '<br>Foo used to be ' . $this->foo;
	$this->foo = $someValue;
	echo '<br>Foo is now ' . $this->foo;

}

}

# Now we create the class

$var = new test( 'another value' );

echo $var->foo . '<br>';
echo $var->bar . '<br>';

$var->foo = 'something else';
echo $var->foo . '<br>';

# Or we can use internal functions
$var->changeFoo( 'classes are very handy!' );

?>

 

 

The solutions do work, but I have hit a new problem

 

the 90+ pages i have ($page.php) either echo the contents within the function or have RAW HTML, therefor as soon as the function is executed, it spits out the contents of $page wherever it stands

 

To get this working, I need to execute the function at the very top of my html, so I can use it inside the <TITLE> tag

 

In doing this, I get my content before all my html, head,title,body tags etc

Link to comment
Share on other sites

I need to use a variable that I have set within a function, outside of that function

 

e.g

 

<?
function beans () {

$cheese = "nice";

echo "hello";
}

echo $cheese;

?>

 

 

Is this possible, something like setting the function globally so it can be used outside of the function container. It must use the above syntax, please dont suggest echoing $cheese within the function  :-[

 

change it to:

function beans () {

$cheese = "nice";

echo "hello";

return $cheese;
}

$value = beans();

echo $value // == "nice"

 

if you have more variables put them in an array and return the array.

 

Best regards!

Link to comment
Share on other sites

I managed to find a way to store the contents in a variable, even when echoed from within that script

 

function test() {

ob_start();

include("page.php");

$ret = ob_get_contents();
return $ret;
}

 

page.php

<table><tr><td>content here</td></tr></table>
<?
$pageTitle = "hello";
echo hello again;
?>
<table><tr><td>test</td></tr></table>

 

 

The problem now is, because of object buffering, variables I set inside page.php cant be used outside of the function/object buffer

 

Thus rendering it a failure

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.