Jump Statement in PHP | Break statement | Continue Statement | Return Statement | goto Statement

Hello Friend how are you i hope you all are doing well. Today in this post we will learn about Jump statements like what is break statement , what is continue statement , what is goto statement and return statement with examples. If you want to learn PHP in very simple easy way then this post will help you definitely.

Let's Start

Jump Statement

  • It is used to transfer the control from one point to another point in the program.
  • There are four jump statements are used in PHP.
  • break statement
  • continue statement
  • goto statement
  • return statement

Break statement

  • It is used to transfer the control out of the body of loop.
  • In other word we can say that it terminates the current loop.
  • break statement are mostly used with loops and switch statement.
<?php
for($i=1;$i<=5;$i++)
{
    if($i==3)
	 break;
	else
	 echo $i." ";
}
?>
<!--Output-->
1 2
because when the value of  i will equal to 3 then break will terminate the loop

Continue statement

  • It is used to skip the next statement and continue the loop.
  • continue statement are mostly used with loop(for,while,do while)
<?php
for($i=1;$i<=5;$i++)
{
//skip the statement when i=3
    if($i==3)
	 continue;
	else
	 echo $i." ";
}
?>
<!--Output-->
1 2 4 5
<?php
for($i=1;$i<=5;$i++)
{
//skip the statement when i>=3
    if($i>=3)
	 continue;
	else
	 echo $i." ";
}
?>
<!--Output-->
1 2
<?php
for($i=1;$i<=5;$i++)
{
//skip the statement when i<=3
    if($i<=3)
	 continue;
	else
	 echo $i." ";
}
?>
<!--Output-->
4 5
<?php
for($i=1;$i<=5;$i++)
{
//skip the statement when i!=3
    if($i!=3)
	 continue;
	else
	 echo $i." ";
}
?>
<!--Output-->
3

goto statement

  • It is an user defined control statement that transfer the control as per user requirement.
<?php
for($i=1;$i<=5;$i++)
{
//skip the statement when i!=3
    if($i==3)
	 goto abc;
	else
	 echo $i." ";
}
echo "Hi";
abc:
echo "Hello";
?>
<!--Output-->
1 2 Hello
because when the value of  i will equal to 3 then control will jump to the line 11

return statement

  • return statement terminates the current function and transfer the control to the calling function.
  • we can also use return statement to transfer value from one function to another.
<?php
function sum($x,$y)
{
return ($x+$y);
}
//funtion calling
$result=sum(5,6);
//printing result
echo "Sum=".$result;
?>
<!--Output-->
Sum=11

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks

Post a Comment

0 Comments