function does not return

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

function does not return

waigani-2
Can anyone tell me why the following function will not return $return and yet will print_r($return) as expected?

function page($parts, $i, $array){
         
          foreach($parts as $part){
          $page .= $part;
          }
               
                $array[$i] = $page;
                $i++;
               
                if($i < count($parts)){
                array_pop($parts);
                page($parts, $i, $array);
                }
                elseif($i = count($parts)){
                $return = array_reverse($array);

                //this prints as expected
                print_r($return);

                //this returns nothing
                return $return;
                }
        }

//example
$i = 0; $parts = array('one','two',three'); $array = array();

//prints array
$array = page($parts, $i, $array);

//returns nothing.
print_r($array);

thanks,
Jess
Reply | Threaded
Open this post in threaded view
|

Re: function does not return

holografix
Hi

From php manual:

Remember that print_r() will move the array pointer to the end. Use reset() to bring it back to beginning.

Remove print_r($return) or add reset($return) before return $return.

Regards
holo





2007/11/12, Waigani <[hidden email]>:

Can anyone tell me why the following function will not return $return and yet
will print_r($return) as expected?

function page($parts, $i, $array){

          foreach($parts as $part){
          $page .= $part;
          }

                $array[$i] = $page;
                $i++;

                if($i < count($parts)){
                array_pop($parts);
                page($parts, $i, $array);
                }
                elseif($i = count($parts)){
                $return = array_reverse($array);

                //this prints as expected
                print_r($return);

                //this returns nothing
                return $return;
                }
        }

//example
$i = 0; $parts = array('one','two',three'); $array = array();

//prints array
$array = page($parts, $i, $array);

//returns nothing.
print_r($array);

thanks,
Jess
--
View this message in context: http://www.nabble.com/function-does-not-return-tf4790342s16154.html#a13703716
Sent from the Zend Framework mailing list archive at Nabble.com.


Reply | Threaded
Open this post in threaded view
|

Re: function does not return

Dimitar Peev
In reply to this post by waigani-2
The recursive call to function page() is causing your problems.

On Nov 12, 2007 1:14 PM, Waigani <[hidden email]> wrote:

>
> Can anyone tell me why the following function will not return $return and yet
> will print_r($return) as expected?
>
> function page($parts, $i, $array){
>
>           foreach($parts as $part){
>           $page .= $part;
>           }
>
>                 $array[$i] = $page;
>                 $i++;
>
>                 if($i < count($parts)){
>                 array_pop($parts);
>                 page($parts, $i, $array);
>                 }
>                 elseif($i = count($parts)){
>                 $return = array_reverse($array);
>
>                 //this prints as expected
>                 print_r($return);
>
>                 //this returns nothing
>                 return $return;
>                 }
>         }
>
> //example
> $i = 0; $parts = array('one','two',three'); $array = array();
>
> //prints array
> $array = page($parts, $i, $array);
>
> //returns nothing.
> print_r($array);
>
> thanks,
> Jess
> --
> View this message in context: http://www.nabble.com/function-does-not-return-tf4790342s16154.html#a13703716
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
>



--
Dimitar Peev
PHP6 Zend Certified Engineer
[hidden email]
+359887395978
Reply | Threaded
Open this post in threaded view
|

Re: function does not return

Jack Sleight
In reply to this post by waigani-2
Because $return is being output on the second call to that function, via
the page($parts, $i, $array); call at line 13, however your not then
returning the result of that call. You should do: return page($parts,
$i, $array);

Also, elseif($i = count($parts)){ should probably be elseif($i ==
count($parts)){

Waigani wrote:

> Can anyone tell me why the following function will not return $return and yet
> will print_r($return) as expected?
>
> function page($parts, $i, $array){
>
>  foreach($parts as $part){
>  $page .= $part;
>  }
>
> $array[$i] = $page;
> $i++;
>
> if($i < count($parts)){
> array_pop($parts);
> page($parts, $i, $array);
> }
> elseif($i = count($parts)){
> $return = array_reverse($array);
>
>                 //this prints as expected
> print_r($return);
>
>                 //this returns nothing
> return $return;
> }
> }
>
> //example
> $i = 0; $parts = array('one','two',three'); $array = array();
>
> //prints array
> $array = page($parts, $i, $array);
>
> //returns nothing.
> print_r($array);
>
> thanks,
> Jess
>  

--
Jack
Reply | Threaded
Open this post in threaded view
|

Re: function does not return

waigani-2
In reply to this post by holografix
Okay here is the modified code - no print_r no elseif, same problem - no return. I can't see what is wrong with the recursive function??

function page($parts, $i, $array){
         
          foreach($parts as $part){
          $page .= $part;
          }
                $array[$i] = $page;
                $i++;
                if($i < count($parts)){
                array_pop($parts);
                page($parts, $i, $array);
                }else{
                return $array;
                }
        }




Holografix wrote
Hi

From php manual:

Remember that *print_r()* will move the array pointer to the end. Use
reset() <function.reset.html> to bring it back to beginning.
Remove print_r($return) or add reset($return) before return $return.

Regards
holo





2007/11/12, Waigani <jesse.meek@otago.ac.nz>:
>
>
> Can anyone tell me why the following function will not return $return and
> yet
> will print_r($return) as expected?
>
> function page($parts, $i, $array){
>
>           foreach($parts as $part){
>           $page .= $part;
>           }
>
>                 $array[$i] = $page;
>                 $i++;
>
>                 if($i < count($parts)){
>                 array_pop($parts);
>                 page($parts, $i, $array);
>                 }
>                 elseif($i = count($parts)){
>                 $return = array_reverse($array);
>
>                 //this prints as expected
>                 print_r($return);
>
>                 //this returns nothing
>                 return $return;
>                 }
>         }
>
> //example
> $i = 0; $parts = array('one','two',three'); $array = array();
>
> //prints array
> $array = page($parts, $i, $array);
>
> //returns nothing.
> print_r($array);
>
> thanks,
> Jess
> --
> View this message in context:
> http://www.nabble.com/function-does-not-return-tf4790342s16154.html#a13703716
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
>
Reply | Threaded
Open this post in threaded view
|

Re: function does not return

Jack Sleight
As I said, you're not returning the result of the page($parts, $i, $array); call within the function (at line 10).

Waigani wrote:
Okay here is the modified code - no print_r no elseif, same problem - no
return. I can't see what is wrong with the recursive function??

function page($parts, $i, $array){
	  
	  foreach($parts as $part){
	  $page .= $part;
	  }
		$array[$i] = $page;
		$i++;
		if($i < count($parts)){
		array_pop($parts);
		page($parts, $i, $array);
		}else{
		return $array;
		}
	}





Holografix wrote:
  

  

--
Jack
Reply | Threaded
Open this post in threaded view
|

Re: function does not return

Jack Sleight
In reply to this post by waigani-2
By the way, this is not actually a general PHP help mailing list, this list is specifically for Zend Framework discussion. You would probably be better off posting in one of the PHP mailing lists (http://www.php.net/mailing-lists.php), or perhaps one of the many PHP help forums (http://www.google.co.uk/search?q=php+help+forum).

Waigani wrote:
Okay here is the modified code - no print_r no elseif, same problem - no
return. I can't see what is wrong with the recursive function??

function page($parts, $i, $array){
	  
	  foreach($parts as $part){
	  $page .= $part;
	  }
		$array[$i] = $page;
		$i++;
		if($i < count($parts)){
		array_pop($parts);
		page($parts, $i, $array);
		}else{
		return $array;
		}
	}





Holografix wrote:
  

  

--
Jack
Reply | Threaded
Open this post in threaded view
|

Re: function does not return

waigani-2
In reply to this post by Jack Sleight
Ah! Got ya. Thank you! And sorry for asking general questions in the framework list. So working code =

function page($parts, $i, $array){
         
          foreach($parts as $part){
          $page .= $part;
          }
                $array[$i] = $page;
                $i++;
                if($i < count($parts)){
                array_pop($parts);
                return page($parts, $i, $array);
                }else{
                return $array;
                }
        }



Jack Sleight wrote
As I said, you're not returning the result of the page($parts, $i,
$array); call within the function (at line 10).

Waigani wrote:
> Okay here is the modified code - no print_r no elseif, same problem - no
> return. I can't see what is wrong with the recursive function??
>
> function page($parts, $i, $array){
>  
>  foreach($parts as $part){
>  $page .= $part;
>  }
> $array[$i] = $page;
> $i++;
> if($i < count($parts)){
> array_pop($parts);
> page($parts, $i, $array);
> }else{
> return $array;
> }
> }
>
>
>
>
>
> Holografix wrote:
>  
>
>  

--
Jack