IF Function Exists...then

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

smoogle
Forum Newbie
Posts: 7
Joined: Thu Jul 20, 2006 6:16 am

IF Function Exists...then

Post by smoogle »

I have a PHP template page that looks something like this:

TEMPLATE.php

Code: Select all

<html>

<head>
</head>

<body>

<?php Content();?> //Calls the function Content

</body>
I then have my main page that looks like this:

MAIN.php

Code: Select all

<?php function Content() { ?>

This is my content

<?php } ?>

<?php include("template.php");?> //pulls in the template file
This works fine but what i want to do is have a function defined in template.php that checks to see if the function exists. If the function exisits then it continues, if the function does not exist it retuns a blank for the function.

The reason for wanting to do this is I can then define multiple calls for different functions in my template page and only populate them if they are defined in the page including the template.php reference.

Hope that makes sense

:)
Last edited by smoogle on Thu Jul 20, 2006 11:00 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

lol... aptly named :lol:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yeah, sometimes they get it right, and other times, not so much (sprintf - what the hell does that do?). Same for the argument passing... do I search for a needle in a haystack or do I search the haystack for a needle? Consistency would be nice, but alas, PHP is free so I can't complain.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Everah wrote:sprintf - what the hell does that do?.
Exactly the same as the C function of the same name that has existed for hundreds of years*.

* Almost.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Which is all well and good for C programmers. But for us PHP hicks from the other side of the tracks, sprintf tells me nothing. :wink: Now, isnumeric or in_array, they tell me something...
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I like the printf functions, especially their names.

They are a few of the easiest to remember, for me.
smoogle
Forum Newbie
Posts: 7
Joined: Thu Jul 20, 2006 6:16 am

Post by smoogle »

Thanks very much and sorry for asking a dumb question....being a such a newbie and all ... :)

How do I get the php page to continue running the code if it see that the function doesn't exist.... have a feeling this is going to be another no brainer.... sorry in advance...
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

If you could show some code?

Code: Select all

if (function_exists('FunctionName') != FALSE)
{
   // use it
}
?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Daedalus- wrote:If you could show some code?

Code: Select all

if (function_exists('FunctionName') )
{
   // use it
}
?
fixed
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

He should compare the return value to something, and that something should be false.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

function_exists returns boolean so you can compare against it.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

sprintf is string print format
fprintf is file print format

Likewise for the scans..

though I'm not sure what vsprintf is an abbrev' of, although I know what they do.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I don't want to make it appear that I don't know what they do. I was just using them as an example of how some functions are named versus other named functions. To the novice, function_exists() seems to make sense. If you were a new developer, this would kinda tip you off as to what is happening with it. Others, like sprintf(), don't exactly do that. That's the point I was trying to make.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I read you everah
Post Reply