Page 1 of 1

calling functions

Posted: Sat Jan 25, 2003 3:28 pm
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; 

   } 
?>

Posted: Sat Jan 25, 2003 4:32 pm
by m3mn0n
hmmm.... :?

Posted: Sat Jan 25, 2003 4:35 pm
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.

Posted: Sun Jan 26, 2003 12:28 am
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]

Posted: Mon Jan 27, 2003 2:36 am
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:

Posted: Mon Jan 27, 2003 8:35 am
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

Posted: Mon Jan 27, 2003 10:58 am
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**

Posted: Mon Jan 27, 2003 1:17 pm
by volka
tell us about call_user_func() anyway. Makes you happier and us probably smarter =]

Posted: Tue Jan 28, 2003 6:26 am
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

Posted: Tue Jan 28, 2003 8:03 am
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)