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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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