Variables do not keep values after leaving function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Robinson

    Variables do not keep values after leaving function

    i set a variable while im in a function, but it does not keep its value
    when returning to the place the function was called. is this something to
    do with register_global s being off? is there anyway i can make the
    variable keep its value when returning to the code where it branched from?
    TIA (again)

    function textsearch($the _items_descript ion){

    $pos = strpos($the_ite ms_description, $search_for);
    if ($the_items_des cription == "") {
    $does_contain_t ext = true;
    } else {
    if ($pos === false) {
    $does_contain_t ext = false;
    } else {
    $does_contain_t ext = true;
    }
    }
    echo ("before leaving <BR>");
    echo ("$does_contain _text");
    echo ("<br>");
    }
  • Matthew Robinson

    #2
    Re: Variables do not keep values after leaving function

    its the $does_contain_t ext variable that i want to keep the val of

    Comment

    • Boyd

      #3
      Re: Variables do not keep values after leaving function

      In article <pan.2004.01.09 .23.49.41.29403 @hotmail.com>,
      mattyrobinson69 @hotmail.com says...[color=blue]
      > i set a variable while im in a function, but it does not keep its value
      > when returning to the place the function was called. is this something to
      > do with register_global s being off? is there anyway i can make the
      > variable keep its value when returning to the code where it branched from?
      > TIA (again)
      >[/color]

      global $varname;
      --
      *************** *************** ********
      The Eldritch Dark:
      Dedicated to Clark Ashton Smith

      Comment

      • Matthew Robinson

        #4
        Re: Variables do not keep values after leaving function

        i fixed it, i just put $does_contain_t ext = true; at the start of the
        file. thanks anyway .

        Comment

        • Matthew Robinson

          #5
          Re: Variables do not keep values after leaving function

          i realised that what i tried wasn't working because it was always
          evaluating to true, for some reason after global $does_contain_t ext; or
          whatever my variable was called, it say's there's a parse error on the
          first line that tried to assign the variable - $does_contain_t ext = true;

          Comment

          • steven mestdagh

            #6
            Re: Variables do not keep values after leaving function

            Matthew Robinson <mattyrobinson6 9@hotmail.com> wrote:[color=blue]
            > i set a variable while im in a function, but it does not keep its value
            > when returning to the place the function was called. is this something to
            > do with register_global s being off? is there anyway i can make the
            > variable keep its value when returning to the code where it branched from?
            > TIA (again)
            >
            > function textsearch($the _items_descript ion){
            >
            > $pos = strpos($the_ite ms_description, $search_for);
            > if ($the_items_des cription == "") {
            > $does_contain_t ext = true;
            > } else {
            > if ($pos === false) {
            > $does_contain_t ext = false;
            > } else {
            > $does_contain_t ext = true;
            > }
            > }
            > echo ("before leaving <BR>");
            > echo ("$does_contain _text");
            > echo ("<br>");
            > }[/color]

            why not return the variable you need? something like:

            function myfunc($var1,$v ar2) {
            // lines of code
            $var_of_intrest = ...
            return $var_of_intrest ;
            }

            $what_i_want = myfunc('var1val ue','var2value' );

            note that, in your code, you are not passing $search_for to your
            function but you do use it. if you do not pass it, you need to put
            global $search_for
            inside your function.

            also, your if/else construction is strange...

            maybe it is a good idea to read the beginning of the manual at least?

            steven.

            Comment

            • Cecile Muller

              #7
              Re: Variables do not keep values after leaving function

              In the function, it considers that it's a local variable unless told
              otherwise, so you have to declare it as a global one: either by
              putting in the function "global $does_contain_t ext;" or using the
              $_GLOBALS['does_contain_t ext'] variable. Read
              http://www.php.net/manual/en/languag...bles.scope.php for more
              informations about it.

              Comment

              Working...