Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Functions more than once

Status
Not open for further replies.

madhatt30

Technical User
Mar 30, 2005
5
US
I am trying to find out how to call a function more than once. for example
function myfunction()
{
// Some Code here
}

... rest of php page

myfunction();
the myfunction() gets called numerous times
 
What is your problem?

This is why you have functions, so they can be called more than once. If you only call the function once, you might as well have the code inline.

Ken
 
I don't think you have expressed yourself well here. To answer your question, it is easy. Just call the function as many times as you want or need to call it:
Code:
function myfunction()
{
  // Some Code here
}

... rest of php page

myfunction();
myfunction();
myfunction();

// myfunction was called three times
If you're looking for recursion, you can call the function from within that same function. This means function will loop within itself. You need to be careful with that because if you don't provide a way for function to stop looping, your scripts will timeout while indefinitely looping.
 
<script language="JavaScript">
function refreshIt()
{
<?php echo getUserImage(); ?>;
}
</script>
<?php

function getUserImage()
{
//mycode here for php
}
?>
.. body on load refreshes ever 5 sec ....

everything works fine the first time around but it doesn't execute the php part of it more than once. The java refresh works with no problem just the php doesn't execute more than once

maybe this explains it a little better
 
PHP runs on the server, Javascript runs on the client.

Javascript can't call a PHP function.

Ken
 
I am aware of that fact. however this is a php page. you can run php code just about anywhere. and yes the images do load from the database on the first loop through. It just doesn't call a second time.
 
Assuming that getUserImage() evaluates to valid javascript code then that should work, eg if getUserImage() was this:

Code:
function getUserImage()
{
  return 'alert("The function was called")';
}

to take a very simple example, then it should be fine. When the browser saw it the javascript block would be:

Code:
<script language="JavaScript">
function refreshIt()
 {
   alert("The function was called");
 }
</script>

Then the that function could be called by any javascript code on the page.
 
It doesn't work because the PHP is back on the server while the Javascript is on the client, and once the page is created and sent to the client, the PHP is done running.

Lee
 
trollacious: The code as given will work as long as getUserImage() returns valid javascript code, though it may require several mental leaps before the code makes sense.

madhatt30 : As this way of doing it is quite hard to visualise for a lot of people (and may be quite hard to debug), I would advise you to attempt to find a different way if there is one.
 
I understand what you're doing, do it myself in PHP and ASP both.

Maybe if you showed some more of your code, and use your REAL code rather than the made up stuff you have here, we could get a better idea of what you're trying to do and why it's not happening the way you want.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top