Re: function I created doesn't work

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Jan 4, 2008 12:28 PM, afan pasalic <afan@xxxxxxxx> wrote:
>
> Daniel Brown wrote:
> > On Jan 4, 2008 12:06 PM, afan pasalic <afan@xxxxxxxx> wrote:
> >> hi
> >> I have function
> >> function get_content($client_id, $form_id, $index1)
> >> {
> >>         $query = mysql_query("
> >>                         SELECT content
> >>                         FROM infos
> >>                         WHERE client_id=".$client_id." AND form_id=".$form_id." AND
> >> index1='".$index1."'");
> >>         if (mysql_num_rows($query) > 0)
> >>         {
> >>                 $result = mysql_fetch_assoc($query);
> >>                 return $result['content'];
> >>         }
> >>         else
> >>         {
> >>                 get_content(0, 0, $index1); // get default value
> >>         }
> >> }
> >>
> >> When I call it
> >> $CONTENT = get_content(12, 104, 'merchant');
> >> echo $CONTENT; // empty, nothing
> >>
> >> But if I use global in the function
> >>
> >> function get_content($client_id, $form_id, $index1)
> >> {
> >>         global $CONTENT;
> >>         $query = mysql_query("
> >>                         SELECT content
> >>                         FROM infos
> >>                         WHERE client_id=".$client_id." AND form_id=".$form_id." AND
> >> index1='".$index1."'");
> >>         if (mysql_num_rows($query) > 0)
> >>         {
> >>                 $result = mysql_fetch_assoc($query);
> >>                 $CONTENT = $result['content'];
> >>         }
> >>         else
> >>         {
> >>                 get_content(0, 0, $index1);
> >>         }
> >> }
> >>
> >>
> >> get_content(12, 104, 'merchant');
> >> echo $CONTENT;  # Shows correct.
> >>
> >> What's wrong with first solution?
> >>
> >> Thanks for any help.
> >
> >     Functions only use variables within their own scope, unless
> > explicitly told to consider a variable as a global (or if the variable
> > is a SUPERGLOBAL).
> >
> not quite sure I understand?!?
> :(
>
>

    The fundamentals of PHP (and general programming): working with globals.

    Specifically for PHP, some required reading:

        http://us.php.net/global


-- 
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux