Jump to content

How to access a local variable globally


basuindranil_84

Recommended Posts

Yes. However, the problem is partly that you never actually call abc(), which means that even with global $name won't be set. Fully fixed the code may read

$name = "";function abc(){global $name;$name="indranil";echo "the name is ".$name;//this gives the value of $name}abc();echo "the name is ".$name;//this will print the value "indranil"

Link to comment
Share on other sites

You should never call echo inside a function unless absolutely necessary. I would instead do this:

<?php  function abc(){  	  return "Name Here";   }   $name = abc();   echo "The name is ".$name;?>

If whatever you are doing must be done inside the function with echoes, then use what Synook posted.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...