getMenu() .... why is it an undefined function?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

getMenu() .... why is it an undefined function?

Post by simonmlewis »

Code: Select all

$menu = $_GET['menu'];
if ($menu != "")
{
function getMenu()
{
$thismenu="includes/menu/".$_GET['menu'].".inc";
if (file_exists($thismenu)) 
  {
   include $thismenu;
  } 
  else 
  {
  echo "<meta http-equiv='Refresh' content='0 ;URL=/error'>";
  }
}}

..............

$menu = $_REQUEST['menu'];
if(isset($_REQUEST['menu']))
{	getMenu();}

I am getting the error in our logs:
[text][08-Apr-2013 05:49:47] PHP Fatal error: Call to undefined function getmenu() in /home/site/public_html/index.php on line 641[/text]
641 is the latter 'getmenu'.

Why am I getting this?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: getMenu() .... why is it an undefined function?

Post by requinix »

Because the function isn't defined.

And before you say "yes it is, it's right there", notice that you define it inside an if block. If that condition isn't true then the function won't be defined.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: getMenu() .... why is it an undefined function?

Post by simonmlewis »

But it's only calling the function if $menu is requested in the URL. If it is, it's also called in the functions.

Isn't it?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: getMenu() .... why is it an undefined function?

Post by requinix »

Actually one uses the URL and the other uses whatever information is available, including POSTed data.

Is that actually your code? Did you paraphrase or simplify the code for your post?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: getMenu() .... why is it an undefined function?

Post by simonmlewis »

That is it, apart from code not related to it. One is te bit to draw in the at, one is the function. He should it be then?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: getMenu() .... why is it an undefined function?

Post by requinix »

What is the URL of this page?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: getMenu() .... why is it an undefined function?

Post by simonmlewis »

I varies, but via the ht access, it would include ...&menu=categ...
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: getMenu() .... why is it an undefined function?

Post by simonmlewis »

Is this correct?

Code: Select all

$menu= isset($_GET['menu']) ? $_GET['menu'] : null;
$menu = $_REQUEST['menu'];
if ($menu != "")
{	getMenu();}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: getMenu() .... why is it an undefined function?

Post by simonmlewis »

Answer: No. I am still getting the errors.
What should it be??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: getMenu() .... why is it an undefined function?

Post by simonmlewis »

Code: Select all

function getMenu()
{
$thismenu="includes/menu/".$_GET['menu'].".inc";
if (file_exists($thismenu)) 
  {
   include $thismenu;
  } 
  else 
  {
  echo "<meta http-equiv='Refresh' content='0 ;URL=/error'>";
  }
}
Should it be like this then - just a function there NOT inside an if statement??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: getMenu() .... why is it an undefined function?

Post by requinix »

Yes actually, you should define functions and classes and such at the very top level - not inside anything conditional.

I still don't know why it thinks the function is undefined. Moving it to the top should definitely fix it.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: getMenu() .... why is it an undefined function?

Post by simonmlewis »

It was always at the top. But I have tried it outside the if statement. Is this what it should be, outside the IF?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: getMenu() .... why is it an undefined function?

Post by requinix »

If it's inside something then it's not at the top.

Yes, outside. Outside everything.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: getMenu() .... why is it an undefined function?

Post by simonmlewis »

Thanks. Now trying that. Will see in the morning if all those errors have gone.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: getMenu() .... why is it an undefined function?

Post by simonmlewis »

Errors gone!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply