Jump to content

Archived

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

longree

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

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]

Share this post


Link to post
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???

Share this post


Link to post
Share on other sites
No lol, I'm very inexperienced with PHP (approaching 2 weeks, tbh), but in most languages I've learned that is the case.

Share this post


Link to post
Share on other sites
Oh right, - PHP is very useful to know... - It's just nown and then I come up with some new problems...

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
arrrr, I didn't know global was an input AND an output function...

Thanks for that!


Good luck with php scott, cya around!

Share this post


Link to post
Share on other sites

×

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.