Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to come out of a function
Old 06-22-2005, 07:19 AM How to come out of a function
rungss's Avatar
Skilled Talker

Posts: 67
Name: Bijay Rungta
Location: Mumbai, India
Trades: 0
Hey how do you come out of a function in PHP
Doesn't returning a value breaks out from the function as in C and other languages

Is exit;
the option to force the control out of the function

Wont writing exit; stop the execution of the code where the function is called from??

Please clarify my doudt

Thanx
rungss is offline
Reply With Quote
View Public Profile Visit rungss's homepage!
 
 
Register now for full access!
Old 06-22-2005, 07:30 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
The exit() method is not intended to stop the execution of a particular function but of the entire script itself. While execution of a PHP script starts at the top of the file, execution can be modularized using functions. Functions return values based on their input parameters and logic. Use the keyword return to exit a function. Use the exit() method to terminate execution of the entire script.

I hope this answers your question.

Quote:
Doesn't returning a value breaks out from the function as in C and other languages
I don't fully understand this question as it is written. Can you re-write it? C and "other languages" also return values from functions and have similar concepts of returning from functions and exiting programs.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 06-22-2005, 07:40 AM
rungss's Avatar
Skilled Talker

Posts: 67
Name: Bijay Rungta
Location: Mumbai, India
Trades: 0
Quote:
Originally Posted by Kyrnt
.
I hope this answers your question. .
Ya this does answer my question and its as i had expected
Quote:
Originally Posted by Kyrnt
.
I don't fully understand this question as it is written. Can you re-write it? C and "other languages" also return values from functions and have similar concepts of returning from functions and exiting programs.
What I actually meant is

whether a return statement forces the control to get out of the loop

ofcourse a return statement may or may not return a value

I can come out of the function as

return;
or return $value;
or return ($value);


But my code is not behaving as expected may be there is something wrong i'll check that out

Thank you

right buddy???
rungss is offline
Reply With Quote
View Public Profile Visit rungss's homepage!
 
Old 06-22-2005, 08:01 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
Quote:
Originally Posted by rungss
I can come out of the function as

return;
or return $value;
or return ($value);
This is true, but generally you want to write functions that always return the same data type. This is required in more strongly typed languages (e.g., C, Java), but in PHP you have some functionality. But your calling code must then determine what (if anything) was returned, introducing more complexity.

I would submit that if your function normally returns some object or value and you instead want to return from the function prematurely without a value, use -1 or null then check for those values in your calling code.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 06-22-2005, 11:09 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Quote:
Originally Posted by rungss
whether a return statement forces the control to get out of the loop
You can use a break statement to do this, and a continue statement to skip one iteration:

PHP Code:
  <?php
  
function foo() {
    for(
$i 0$i <10$i++ ) {
      if(
$i==3) continue;
      echo 
$i;
    }
    
$j 0;
    while(
true) {
      echo 
$j++;
      if(
$j == 42) break;
    }
    return 
8;
  }
 
 
foo();
 
?>
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)

Last edited by 0beron; 06-22-2005 at 11:11 AM..
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 06-23-2005, 05:43 AM
rungss's Avatar
Skilled Talker

Posts: 67
Name: Bijay Rungta
Location: Mumbai, India
Trades: 0
Quote:
Originally Posted by 0beron
You can use a break statement to do this, and a continue statement to skip one iteration:
[/php]
Hey you can use the break or continue statement in a for,while or do while loop and also break in switch statements but that wont force the control out of the function.

to come out of the function prematurely you need return statement.
rungss is offline
Reply With Quote
View Public Profile Visit rungss's homepage!
 
Reply     « Reply to How to come out of a function
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23172 seconds with 12 queries