calling functions

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
jamal
Forum Commoner
Posts: 57
Joined: Sat Oct 26, 2002 7:53 pm
Location: London

calling functions

Post by jamal »

Hi guys,
I am still new in calling functions in php.
I wondered if anyone could show me an example in details on how to call this funciton in an application??

Code: Select all

<?php
 
$dbinfo = array("host"     => "localhost", 
                "user"     => "", 
                "pass"     => "", 
                "database" => "search_engine", 
                "keywords" => "keywords_list", 
                "desc"     => "page_data"); 

function search($db, $query_terms) { 

   $matches = array(); 

   $db_h = mysql_connect($db['host'], 
        $db['user'], 
        $db['pass']); 

    if (!$db_h) { 

$error = "Couldn't connect to MySQL database [" . 
    mysql_errno() . "]: "; 
        $error .= mysql_error(); 

        return $error; 
    } 

    mysql_select_db($db['name']);

$keywords = explode(' ', addslashes(strtolower($query_terms))); 
$reg_ex = "[(" . implode(")(", $keywords) . ")]"; 

$query = "SELECT count(keywords_list.key_id) as score, 
page_data.url as url,    page_data.title as title, 
                       page_data.desc as desc 
                  FROM keywords_list, page_data 
                 WHERE (page_data.page_id = 
  keywords_list.page_id) 

while($row = mysql_fetch_array($query_h)) { 

      array_push($matches, $row); 

   } 

    return $matches; 
} 
?> 
                   AND (keywords_list.keys REGEXP 
       $reg_ex) 
              GROUP BY keywords_list.page_id 
              ORDER BY score DESC"; 

   $query_h = mysql_query($db_h, $query); 

   if(!$query_h) { 

      $error = "Couldn't execute query [" . 
mysql_errno($dbh) . "]: "; 
        $error .= mysql_error($dbh); 

        return $error; 

   } 
?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

hmmm.... :?
Last edited by m3mn0n on Sat Jan 25, 2003 4:40 pm, edited 3 times in total.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

You must remember functions must be the first thing placed on the PHP document. I am not sure exactally of why it is so important or why it doesn't work if not done so but my php training book recommends that and i've never doubted it before. :wink:

Also make sure no existing built-in functions of php have the same name as your created one.

In the case of your function, all you need to do is this:

Code: Select all

<?php
search($submitted_db, $submitted_query_terms);
?>
submitted meaning a form or GET method submitted the variables.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://www.php.net/manual/en/functions.php[quote] In PHP 3, functions must be defined before they are referenced. No such requirement exists in PHP 4. Except when a function is conditionally defined such as shown in the two examples below.[/quote]
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Well i'll be damned. 8O

*throws the book againts the wall*

The author sure will get an e-mail from me about this. :roll:
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

.. it is good practise to place anything being called above what is calling it, several programming languages require this, such as Pascal/Delphi/Kylix
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post by DeGauss »

pff.

Place functions in a seperate file (function.inc.php or something) and then include that file in your main script.

That way you can build up a huge library of functions in one file that you can use over and over again.

**upset that this question wasn't related to call_user_func**
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

tell us about call_user_func() anyway. Makes you happier and us probably smarter =]
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post by DeGauss »

ALRIGHT!

call_user_func is a very useful function that calls functions that YOU wrote.

Doesn't make sense? DON'T WORRY!

Let's say you have a function called foo();

<?
function foo() {
$foo="foo";
return $foo;
}
?>

Now. Imagine you have a CMS that works like say, this bulletin board. phpBB converts [b] into <b>.

Somewhere in phpBB there's a call for either an ereg or a str_replace.

So let's borrow that line and do this...

<?
if (ereg("{foo}",$text)) {
call_user_func(foo);
}
?>

(The above is metacode, so don't try it directly ;) )

Alright, let's make some sample text...

"Blah Blah Blah [b]{foo}[/b] Blah"

Every instance of {foo} is replaced by the returned contents of the function foo();

This is useful if you want to insert bits of sql information into dynamic text.

Such as "There are {numberOfUsers} online"

This concludes my mini-tutorial on call_user_func

*satisfied*

:D :D
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

while call_user_func() can be very useful for some things, the example used could be handled better and more efficient by preg_replace_callback()
(ereg should nver be used unless you have a very specific reason for it, use str functions or preg instead, posix/eregs wastes a lot of resources)
Post Reply