Jump to content

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

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.