Jump to content


Photo

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


  • Please log in to reply
9 replies to this topic

#1 longree

longree
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 11 August 2006 - 11:05 AM

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

<?php

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

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


#2 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 11:08 AM

Take the inverted commas out for a start.

#3 longree

longree
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 11 August 2006 - 11:15 AM

You and I both know that wouldn't make a difference in this case...

#4 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 11:19 AM

Don't you have to define functions before you call them?

#5 longree

longree
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 11 August 2006 - 11:25 AM

Either way works...

http://php.net/functions

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.


Don't tell me you still use PHP 3 :o???

#6 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 11:35 AM

No lol, I'm very inexperienced with PHP (approaching 2 weeks, tbh), but in most languages I've learned that is the case.

#7 longree

longree
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 11 August 2006 - 11:43 AM

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

#8 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 11 August 2006 - 12:57 PM

try
<?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";
}
?>


#9 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 01:08 PM

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


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.

#10 longree

longree
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 11 August 2006 - 02:11 PM

arrrr, I didn't know global was an input AND an output function...

Thanks for that!


Good luck with php scott, cya around!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users