Jump to content

question about variable handling


pippo_jedi

Recommended Posts

Hello,

I have a problem with some code i'm writing and I realized I lack knowledge about how to properly handle variables...

 

I have a file  foo.php that has the variables $foo_1 and $foo_2

now I have a main.php in which

there is a function bar which includes foo.php

How can I make $foo_1 and $foo_2 availlable to the rest of the script in main without resorting to returning thngs?

 

I bet it is a very silly question  ::)

 

bye bye

Link to comment
https://forums.phpfreaks.com/topic/66550-question-about-variable-handling/
Share on other sites

If you want to use those variables in a function in 'main.php', you have to make them available to the function by...

 

<?php

  include'foo.php';

    function cancer() {
    global $foo_1; //Now we can use this variable in the cancer() function in 'main.php'

      $message = "Hello $foo_1";

    return $message;
    }

?>

thank you for hte answer but my problem is the opposite  ;D

foo.php

<?php
$foo_1="I'm foo 1";
$foo_2="I'm foo 2";
?>

 

main.php

<?php

function bar(){

include (foo.php);

}
echo  $foo_1; //this doesn't work I don't want to use return($foo_1);

?>

 

thank you for you help

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.