basuindranil_84 Posted March 11, 2008 Share Posted March 11, 2008 function abc(){ $name="indranil"; echo "the name is ".$name;//this gives the value of $name}echo "the name is ".$name;//this does not print the value "indranil"How could I solve this problem.Please Help!!! Link to comment Share on other sites More sharing options...
real_illusions Posted March 11, 2008 Share Posted March 11, 2008 my guess is, its because its outside the curly brackets where the variable is set??Other than that, i have no idea. Is that all there is to your script or is there something else? Link to comment Share on other sites More sharing options...
basuindranil_84 Posted March 11, 2008 Author Share Posted March 11, 2008 my guess is, its because its outside the curly brackets where the variable is set??Other than that, i have no idea. Is that all there is to your script or is there something else?Could this be solved by using global or $GLOBAL[] concepts. Link to comment Share on other sites More sharing options...
Synook Posted March 11, 2008 Share Posted March 11, 2008 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 More sharing options...
jhecht Posted March 12, 2008 Share Posted March 12, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.