views:

67

answers:

4

So I have a PHP variable that's value get's replaced from another PHP file. Example:

$var = "~~~OtherVariable~~~";

If I echo this variable out, it prints out the appropriate string. Example:

echo $var; //prints out "This is a string of text";

So it looks like everything is working thus far, my PHP variable ($var) shows that it actually contains the string "This is a string of text" and not "~~~OtherVariable~~~".

Now here comes the problem, I want to use this PHP variable in another PHP function I have else where (on the same page), I want the variables ($var) value to be "This is a string of text", but instead the function is reading it as "~~~OtherVariable~~~", which is not what I want!

Is there a way to make the function read the variable as "This is a string of text" instead of "~~~OtherVariable~~~"??

Thanks Guys & Gals

EDIT: Here's the chunk of code:

$string = "~~~ItemTitle~~~"; /*Another php file looks for any string in 
this file with "~~~ItemTitle~~~" and replaces it with another block of 
text, ie. "This is a string of text, http://www.google.ca" */

//Then I have a little function to look for any links inside of a string of text
function do_reg($text, $regex)
{
   preg_match_all($regex, $text, $result, PREG_PATTERN_ORDER);
   return $result[0];
}

$regex = '\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]';

$regex = '$'.$regex.'$i';

$A =do_reg($string, $regex); /* This is where I tell it what string I want 
it to look into for any URL's */
foreach($A as $B)
{
   echo "$B<BR>"; //Prints out just the URL
}

But when I tell it too look in the $string variable it reads it as "~~~ItemTitle~~~" and not the string of text it gets replaced as.

A: 

Make it global? Not really sure what your asking.

Mr-sk
A: 

Well, to answer your question (if I'm understanding it correctly), you would need to use globals:

global $var = 'something';

function foobar() {
   global $var;  # $var should be 'something' in this context, now
}

However, globals make things really hard to figure out in larger programs. If you think you need one, you might need to rethink your design. (That is, they're probably best avoided, depending on the situation.)

Update: Sorry if that code isn't syntactically correct -- you might need to refer to the PHP documentation on global. I haven't written any PHP in a while and don't have it on my current system, so it would be hard to check.

Benjamin Oakes
A: 

If you're including the file which redefined $var within a function, it has a local scope - that definition doesn't exist outside the function. Some code would help. Regardless of where its defined you can pass the value of the variable to the function you want to use it. Here's an example:

$var = "My String.";
printItOut($var);

function printItOut($passed_value) {
   echo $passed_value; // prints "My String";
}

If the value is defined outside any function and you want to access it without passing it you can refer to it as a global:

$var = "My String";

function printItOut() {
   global $var;
   echo $var; // prints "My String"
}
Erik
+6  A: 

Sounds like you are having a variable scoping issue.

When you include a file, the code from the second file runs in the same scope as the code your include statement is located in.

Consider the following code:

first.php

<?php
$var = 'Apples';
include('second.php');

echo ' and ', $var;

second.php

<?php
$var = 'Oranges';
echo $var;

Running first.php will output "Oranges and Oranges" as your code in second.php is run in the global scope. Once $var is overwritten by the code in second.php, there is no way to get the original value since they are the same variable in the same scope.


Now consider the following:

third.php

<?php
function include_isolated($file) {
    include($file);
}

$var = 'Apples';
include_isolated('second.php');
echo ' and ', $var;

Running third.php will output "Oranges and Apples" as second.php will then be run in the scope of the function include_isolated(). In that case, $var and $var are two separate variables as they are not in the same scope.


If you need the value from the global scope (the one defined in third.php, you can use the super global $GLOBALS to access it as such:

$globalVar = $GLOBALS['var']; // $globalVar = "Apples"

You can also modify the include_isolated() function to make $var global as such:

function include_isolated($file) {
    global $var;
    include($file);
}

When doing that though, $var will become a global scope variable again. This will make the $var assignment in the second file to overwrite the value in the first one. Other variables will not be affected.


For more information about variable scoping, please read the PHP Documentation.

Andrew Moore
Thats why i love writing in OOP
streetparade
@streetparade: OOP or not, variable scope while including is still a problem.
Andrew Moore