PHP में continue का use किसी दी गयी condition के according current loop iteration को skip करने के लिए किया जाता है। और skip करने के बाद Loop next iteration से Start हो जाता है।  simply हम कह सकते हैं कि PHP में continue का use हम वहाँ करते हैं जब हमें किसी condition पर loop execution को skip करना हो।

? well , PHP में continue statement JavaScript से थोड़ा अलग होता है क्योंकि JavaScript में continue statement में optional parameter की value हम किसी loop (for या while loop ) को identify करने के लिए define किये गए label का name provide कराते हैं। जिसे Labeled Statement भी कहते हैं।

PHP continue Example

<?php for($x=1; $x <= 5 ; $x++) { if($x==3) { continue; } echo $x ."<br>"; } ?>

Output :

1
2
4
5

Example में आप देख सकते हैं कि $x की value 3 होते ही वो level skip हो गयी।

Break की तरह ही continue भी एक Optional parameter accept करता है जो कि बताता है की कितने loop iterate levels skip करने हैं , default parameter 1 set होता है।

PHP continue Example With Optional Parameter

<?php for ($i = 1; $i <= 5; ++$i) { for ($y = 1; $y <= 5; ++$y) { if ($y == 2) continue 2; print "$i $y <br>"; } } ?>

Output :

1 1
2 1
3 1
4 1
5 1

तो Example में आप देख सकते कि Outer Loop तो 5 बार execute  हुआ है , but inner loop हर बार एक ही बार execute हुआ है क्योंकि inner loop में variable की value 2 होते ही , execution दोनों ही loop से skip हो जाता है।

PHP में continue use करते समय कुछ    बातें ध्यान में रखें।

  1. Optional parameter positive integer ही होना चाहिए , String , null , 0 , floating point  , negative integer नहीं  होना  चाहिए , नहीं तो Fatal Error Generate होगी।

  2. Optional Parameter की value हम use किये जाने वाले Loops से ज्यादा नहीं होनी चाहिए , means single loop में Optional Parameter की value 1  ही होगी और 1 level nested Loop में Optional Parameter  की  value 2 से ज्यादा नहीं होगी। 

  3. Continue को सभी तरह के Loops जैसे For Loop , Foreach LoopWhile Loop , Do While Loop , switch  loop  में use कर सकते है , But If Else में use नहीं कर सकते हैं।

नीचे Examples में आप देख सकते है कि PHP में Continue Syntax कौन सा valid है और कौन सा invalid .

<?php /*Invalid It Generates : PHP Fatal error: 'continue' not in the 'loop' or 'switch'*/ if ($y == 2) continue 2; /*Invalid It Generates : PHP Fatal error: Cannot 'continue' 2 levels */ for ($y = 1; $y <= 5; ++$y) { if ($y == 2) continue 2; print "$y"; } /*Invalid It Generates : PHP Fatal error: 'continue' operator accepts only positive numbers*/ for ($y = 1; $y <= 5; ++$y) { if ($y == 2) continue 's'; print "$y"; } /*Invalid It Generates : PHP Fatal error: 'continue' operator accepts only positive numbers*/ for ($y = 1; $y <= 5; ++$y) { if ($y == 2) continue 4.5; print "$y"; } /*Invalid It Generates : PHP Fatal error: 'continue' operator accepts only positive numbers*/ for ($y = 1; $y <= 5; ++$y) { if ($y == 2) continue 0; print "$y"; } ?>

तो ये कुछ wrong syntax थे जिन्हे कुछ developers miss कर देते हैं इसलिए Continue को use करते समय ध्यान रखें।

I Hope अब आपको PHP में Continue के बारे में अच्छे से समझ में आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers