Page 1 of 2
IF Function Exists...then
Posted: Thu Jul 20, 2006 10:57 am
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

Posted: Thu Jul 20, 2006 10:59 am
by RobertGonzalez
Posted: Thu Jul 20, 2006 11:01 am
by Luke
lol... aptly named

Posted: Thu Jul 20, 2006 11:08 am
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.
Posted: Thu Jul 20, 2006 11:11 am
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.
Posted: Thu Jul 20, 2006 11:15 am
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.

Now, isnumeric or in_array, they tell me something...
Posted: Thu Jul 20, 2006 11:29 am
by daedalus__
I like the printf functions, especially their names.
They are a few of the easiest to remember, for me.
Posted: Thu Jul 20, 2006 11:32 am
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...
Posted: Thu Jul 20, 2006 11:42 am
by daedalus__
If you could show some code?
Code: Select all
if (function_exists('FunctionName') != FALSE)
{
// use it
}
?
Posted: Thu Jul 20, 2006 11:46 am
by Luke
Daedalus- wrote:If you could show some code?
Code: Select all
if (function_exists('FunctionName') )
{
// use it
}
?
fixed
Posted: Thu Jul 20, 2006 11:48 am
by daedalus__
He should compare the return value to something, and that something should be false.
Posted: Thu Jul 20, 2006 11:52 am
by RobertGonzalez
function_exists returns boolean so you can compare against it.
Posted: Thu Jul 20, 2006 12:55 pm
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.
Posted: Thu Jul 20, 2006 1:00 pm
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.
Posted: Thu Jul 20, 2006 1:20 pm
by Luke
I read you everah