Results 1 to 12 of 12

Thread: [RESOLVED] exit a php function in middle and continue other lines of code

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Resolved [RESOLVED] exit a php function in middle and continue other lines of code

    hi,
    i have this code

    Code:
    <?php
      echo "HELLO";
      abc();
      echo "WORLD";
    ?>
    
    <?php
    function abc()
    {
        if(true)
       {
          exit(0);
       }
    }
    ?>
    it outputs only HELLO

    but i want it to print

    HELLO WORLD

    It does not print world due to the exit function in the function... Please help me to get it work even if exit is executed... :-(

  2. #2
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: exit a php function in middle and continue other lines of code

    I'm guessing what you're looking for is 'return' and not 'exit'.

    'return' is a language construct that allows you to exit a function (with possible return value).
    'exit' is a function that stops execution for the entire script.
    Delete it. They just clutter threads anyway.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: exit a php function in middle and continue other lines of code

    Quote Originally Posted by TheBigB View Post
    I'm guessing what you're looking for is 'return' and not 'exit'.

    'return' is a language construct that allows you to exit a function (with possible return value).
    'exit' is a function that stops execution for the entire script.
    i also tried return, But php keep doing something after return, it executes the whole code and then return. and that cause script to failure Maximum Execution Time 30 Seconds ( Error Comes )

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: exit a php function in middle and continue other lines of code

    PHP Code:
    if(true
    This has no meaning.


    PHP Code:
    function abc()
    {
      return;

    This also has no meaning. It is the same as:
    PHP Code:
    function abc() {} 
    This:
    PHP Code:
    function abc() {}

    echo 
    'HELLO';
    abc();
    echo 
    'WORLD'
    will output:
    Code:
    HELLOWORLD

    But php keep doing something after return, it executes the whole code and then return. and that cause script to failure Maximum Execution Time 30 Seconds ( Error Comes )
    This means you have created an infinite loop.

    Post the code that you tried.

  5. #5
    Member PBertie's Avatar
    Join Date
    Aug 2011
    Location
    Newcastle Upon Tyne
    Posts
    55

    Re: exit a php function in middle and continue other lines of code

    Am I write assuming you want something like this:
    PHP Code:
    <?php
      abc1
    (false);
      echo 
    "HELLO";
      
    abc1(true);
      echo 
    "WORLD";

    function 
    abc1($doIt)
    {
      if(
    $doIt) {
        return;
      }
      echo 
    "&"// This doesn't get run if $doIt == true but the script continues
    }
    ?>
    OUTPUT:
    &HELLOWORLD
    Or:
    PHP Code:
    <?php
      abc2
    (false);
      echo 
    "HELLO";
      
    abc2(true);
      echo 
    "WORLD";

    function 
    abc2($doIt)
    {
      if(
    $doIt) {
        exit(
    0);
      }
      echo 
    "&"// This doesn't get run if $doIt == true but also the whole script stops.
    }
    ?>
    OUTPUT:
    &HELLO

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: exit a php function in middle and continue other lines of code

    i have noticed, that return still continues to the end of the loop, i just want to come out of the function and then continue rest script

    here is the code

    Code:
    <?php
    $prev_root;
    function print_given_level($root, $level)
    {
    	global $prev_root;
    	  $sql = "SELECT * FROM tbl_users where business_id= '$root' ";
    	$res = mysql_query($sql);
    	$row = mysql_fetch_array($res);
    		if($root < 1)
    		{
    			// return 0;
    			echo "<font color='red'>END OF THE TREE Last Root Value $prev_root</font>";
    			exit(0);
    		}			
    		if($level == 1)
    			echo "<hr> EMAIL  =  ".$row['email'] ;   
    		else if($level > 1)
    		{
    			$prev_root = $root;
    			echo "<br> Going to process Left Node (Level) $level = ". ($level -1) . "<br>";
    			print_given_level($row['left_leg_assigned_to'], $level - 1);    
    			echo "<br> Going to process Right Node (Level) $level = ". ($level -1) . "<br>";			
    			print_given_level($row['right_leg_assigned_to'], $level - 1); 			
    		}
    	
    }
    ?>
    i have just found a temp solution for now, to redirect to another page instead of exit;

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: exit a php function in middle and continue other lines of code

    Ah... you didn't mention recursion.

    Returning does exit the function. The problem is that you're expecting it to exit the whole recursion loop, but instead what it actually does is exit the current stack frame only.

    One technique you could use is to return a value (true or false) to indicate whether or not to continue recursing:

    PHP Code:
    <?php
    $prev_root
    ;
    function 
    print_given_level($root$level)
    {
        global 
    $prev_root;
        
    $sql "SELECT * FROM tbl_users where business_id= '$root' ";
        
    $res mysql_query($sql);
        
    $row mysql_fetch_array($res);
        if(
    $root 1)
        {
            echo 
    "<font color='red'>END OF THE TREE Last Root Value $prev_root</font>";
            return 
    false;
        }            
        if(
    $level == 1)
            echo 
    "<hr> EMAIL  =  ".$row['email'] ;   
        else if(
    $level 1)
        {
            
    $prev_root $root;
            echo 
    "<br> Going to process Left Node (Level) $level = ". ($level -1) . "<br>";
            if (!
    print_given_level($row['left_leg_assigned_to'], $level 1))
                return 
    false;
            echo 
    "<br> Going to process Right Node (Level) $level = ". ($level -1) . "<br>";            
            if (!
    print_given_level($row['right_leg_assigned_to'], $level 1))
                return 
    false;
        }
        
        return 
    true;
    }
    ?>
    Traversing a tree can be done without recursion, but it is not as intuitive.

    Also, you avoid using echo to emit HTML.

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: exit a php function in middle and continue other lines of code

    Also also, it strikes me that you might be able to avoid recursion altogether (and many queries) if you restructured your database.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: exit a php function in middle and continue other lines of code

    yes,
    it works now :-)
    Thank you penagate

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: [RESOLVED] exit a php function in middle and continue other lines of code

    Hi,
    i don't know why i can't get out of the loop from the specific place only.

    is it possible to exit from the recursive function below and process other task in the script ? without using of exit(0) function?

    Please make it out after printing the red line in the code.

    Code:
    <?php
    $prev_root;
    function print_given_level($root, $level)
    {
        global $prev_root;
    					if( $the_level != $current_level )
    					{
    
    						if($is_L_found == "empty")
    						{
    							echo "Print_Something_here L And Exit";
    							return false;							
    							//exit(0);
    						}
    					
    						if($is_R_found == "empty")
    						{
    							echo "Print_Something_here R And Exit";
    							return false;							
    							//exit(0);							
    						}
    				
    					}	
        $sql = "SELECT * FROM tbl_users where business_id= '$root' ";
        $res = mysql_query($sql);
        $row = mysql_fetch_array($res);
        if($root < 1)
        {
            echo "<font color='red'>END OF THE TREE Last Root Value $prev_root</font>";
            return false;
        }            
        if($level == 1)
            echo "<hr> EMAIL  =  ".$row['email'] ;   
        else if($level > 1)
        {
            $prev_root = $root;
            echo "<br> Going to process Left Node (Level) $level = ". ($level -1) . "<br>";
             print_given_level($row['left_leg_assigned_to'], $level - 1) 
                 
            echo "<br> Going to process Right Node (Level) $level = ". ($level -1) . "<br>";            
             print_given_level($row['right_leg_assigned_to'], $level - 1) 
                 
        }
        
        return true;
    }
    ?>

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] exit a php function in middle and continue other lines of code

    You need to check the return value like I did in post 7.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: [RESOLVED] exit a php function in middle and continue other lines of code

    yes got it working. :-) thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width