How to declare and call 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

Post Reply
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

How to declare and call function?

Post by ljCharlie »

I want to know how to create and call a function. Here's what I have. I have a PHP block of at the very begining of the page even before the <html> and then at the <body> tag, I declare the function as: function myfunction(){
do my stuff here including echo file download link
}

Then on the PHP block of code that's before <html>, I have myfunction() as my function calling.

Can anyone tell me if what I'm doing is right? If not, will you show me how?

Many thanks in advance!

ljCharlie
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

does it work how you want it to?
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Heres' what I want it.

Code: Select all

<?PHP
   do my database connection and testing here
  if ($btnDate =="Extract Date")&#123;
      queryByDate();
      &#125;
?>
<html>
<head></head>
<body>
<?PHP
   function queryByDate()&#123;
       echo my links here
      &#125;
?>
</body>
</html>
When do this, the page would not display anything. Howver, if I combined both PHP block of code into one inside the body tag then it works just fine. Suggestions are much appreciated.

ljCharlie
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

It isn't the fact that it's inside or outside, you must be doing something differently.

Make sure you're getting the value expected, and maybe add an else to that if statement with a msg to tell what it did (debug your script).
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Thanks! I think I got it working.

ljCharlie
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Oh, by the way, I have one more question.

In the <body> tag, I have the function echo something onto the page, however, since the function was called before the <html> tag, the echo is being displayed before the <html> and not the in the <body> tag where I put the function in. How do i solve this problem?

ljCharlie
Post Reply