7
1 function foo($i){
2 return bar($i)*4;
3 function bar($i){
4 return $i*4;
5 }
6 }
7 echo foo(4);

return

Fatal error: Call to undefined function bar() in /var/www/index.php on line 2

why doesn't it work? it works well in javascript, while it works when i do this:

function foo($i){
 return bar($i)*4;
 }
function bar($i){
 return $i*4;
 }
asked Jun 25, 2011 at 16:57
5
  • The first doesn't work in JS either. And second works in JS too. (But note that the first, in PHP, doesn't create a locally-scoped closure but simply a global function IIRC). Commented Jun 25, 2011 at 17:00
  • Why do you expect a thing working in one language to work in another one? Commented Jun 25, 2011 at 17:01
  • 3
    @delnan: It does work in JavaScript, because of hoisting. Function and variable declarations are hoisted to the top of the function. Commented Jun 25, 2011 at 17:02
  • that wouldn't work in any languages... code after return.... Commented Jun 25, 2011 at 17:02
  • @Felix: Ah, yes. I keep forgetting this ugly inconsistency between var f = function () {} and function f() {} (wishful thinking?). Commented Jun 25, 2011 at 17:04

6 Answers 6

21

Define the function above your return value, otherwise it never gets executed.

<?php
function foo($i){
 function bar($i){
 return $i*4;
 }
 return bar($i)*4;
}
echo foo(4);
?>
answered Jun 25, 2011 at 17:03
Sign up to request clarification or add additional context in comments.

1 Comment

Yes order of events, saw that immediately
3

It doesn't work as you are calling bar() before it has been created. See example 2 here:- http://www.php.net/manual/en/functions.user-defined.php

answered Jun 25, 2011 at 17:01

Comments

1

Your code never reach function bar(...), therefore it's never defined.

You need to put your bar() function before your foo() or before the return bar. (Answer based on your first example).

With your second example you probably define bar() before you use foo() therefore bar() is defined and it works fine.

So as long as you have your function defined when you hit a certain spot in your code it works fine, no matter if it's called from within another function.

answered Jun 25, 2011 at 17:01

Comments

1

If you define function within another function, it can be accessed directly, but after calling parent function. For example:

function a () {
 function b() {
 echo "I am b.";
 }
echo "I am a.<br/>";
}
//b(); Fatal error: Call to undefined function b() in E:\..\func.php on line 8
a(); // Print I am a.
b(); // Print I am b.
answered Jun 25, 2011 at 17:01

Comments

0

Execution not execute after return statement

change your code like this

 function foo($i){
 function bar($i){
 return $i*4;
 }
 return bar($i)*4; 
}
echo foo(4);
answered Jun 25, 2011 at 17:02

Comments

0

For the sake of having a "recent" answer, here's what I did:

function printRx($conn, $patient)
{
 function rows($rows, $tod)
 {
 for ($x = 0; $x < count($tod); $x++) {
 if ($tod[$x] == 1) {
 $rows++;
 break;
 }
 }
 return $rows;
 }
 $tod['prn'] = (explode('|', $row['prn'])) ?? null;
 $rows = rows($rows, $tod['prn']);
 return;
}

Works beautifully

answered Apr 29, 2022 at 22:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.