Jump to content

Nested Function does not work?


Xname

Recommended Posts

HI...I hope u r all fine.

I am using nested function ih php.

this is not working.Here is the code.

<?php
$array = array('a','b','c');

big_func($array);

function big_func($arr) {

function func1($arr) {
foreach($arr as $list1){ echo $list1.'<br />'; }

function func2($arr) {
  foreach($arr as $list2){ echo '<b>'.$list2.'</b><br />'; }
}
function func3(){
	$arra = array('d','e','f');
	func1($arra);
} 
}
}
?>

This is not working.What is the problem with this code?

please help me .....thanks........

Link to comment
https://forums.phpfreaks.com/topic/262662-nested-function-does-not-work/
Share on other sites

The reason your script does not doing anything is because you never call one of those inner functions, all you ever do is define them.

 

That said, PHP doesn't really support nested functions.  The script will parse successfully, but if you ever call the outer function (big_func in your case) more than once you'll get a Fatal error about re-defining the functions.  There really isn't any pratical use for trying to do a nested function setup, just declare all the functions at the top global level and then use them as needed elsewhere.

 

 

Archived

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

×
×
  • 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.