Jump to content

Echo a var that is in a function, that has been defined in another function?


longree

Recommended Posts

Can anybody tell me how I would echo $test?
Inother words, how can I global test out of test()?

[code]<?php

  test();
  testtwo();
 
   function test()
  {
    $test = "Hello this test is working!";
  }

   function testtwo()
  { extract($GLOBALS);
                 echo"$test";
  }

?>[/code]
Link to comment
Share on other sites

Either way works...

http://php.net/functions
[quote]In PHP 3, functions must be defined before they are referenced. No such requirement exists since PHP 4. Except when a function is conditionally defined such as shown in the two examples below. [/quote]

Don't tell me you still use PHP 3 :o???
Link to comment
Share on other sites

try[code]<?php
$test='Bla, bla';
echo "1st call testtwo:\n";
testtwo();
echo "\n-------------\n2nd call testtwo:\n";
test();
testtwo();
function test(){
global $test;
$test = "Hello this test is working!";
}
function testtwo(){
global $test;
echo"$test";
}
?>[/code]
Link to comment
Share on other sites

[quote author=longree link=topic=103836.msg413813#msg413813 date=1155296581]
Oh right, - PHP is very useful to know... - It's just nown and then I come up with some new problems...
[/quote]

I agree with that, although I've only been learning it a very short while, I'm already amazed by some of the things it can do, I'm really glad I'm learning it.
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.