Program में condition के हिसाब से statements को control करने के लिए control statements का इस्तेमाल किया जाता है |
PHP में छह प्रकार के control statements होते है |
- if Statement
- if_else Statement
- else_if Statement
- switch Case Statement
- break Statement
- continue Statement
if Statement
जब condition true होती है तब statement execute होता है |
Syntax for if Statement
if( condtion ){ //statements; }
Example for if Statement
Source Code :Output :12345678<?php $a = 5; $b = 6; if( $a<$b ) { echo "a is less than b"; } ?>
a is less than b
if_else Statement
अगर if की condition true होती है तो if का statement execute होता है | अगर condition false होता है तो else का statement execute होता है |
Syntax for if_else Statement
if( condtion ){ //statements; } else{ //statements; }
Example for if_else Statement
Source Code :Output :1234567891011<?php $a = 5; $b = 5; if( $a<$b ) { echo "a is less than b"; } else{ echo "a is not less than b"; } ?>
a is not less than b
else_if Statement
else_if Statement में एक से ज्यादा conditions होते है | if की condition अगर false हो तो else_if की condition check होती है |
Syntax for else_if Statement
if( condtion ){ //statements; }elseif( condition ){ //statements; }else{ //statements; }
Example for else_if Statement
Source Code :Output :1234567891011121314<?php $a = 5; $b = 5; if( $a<$b ) { echo "a is less than b."; } elseif( $a>$b ){ echo "a is greater than b."; } else{ echo "a is equal to b."; } ?>
a is equal to b.
switch case Statement
Switch case statement में expression होता है और उससे related कुछ cases होते है | जो case उस expression या declare किये हुए variable से match होती है तब वो output में print होता है | अगर कोई भी case expression से match नहीं होती तो वो default का statement output में print करेगा | आपको हर statement के बाद break लगाना पड़ता है, इसका मतलब वो उसके पहले का statement ही print करेगा | अगर आप break नहीं लगाते तो वो पहला और दूसरा ये दोनों statement को print करेगा | default case के बाद break नहीं लगाते |
Syntax for switch case Statement
switch( switch_expression ){ case 1: //statements; break; case 2: //statements; break; case n: //statements; break; default: //statements; }
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>
Example for switch case Statement
Source Code :Output :12345678910111213141516171819202122232425262728<?php $day = date("l"); switch ( $day ){ case "Sunday": echo "Today is Sunday"; break; case "Monday": echo "Today is Monday"; break; case "Tuesday": echo "Today is Tuesday"; break; case "Wednesday": echo "Today is Wednesday"; break; case "Thursday": echo "Today is Thursday"; break; case "Friday": echo "Today is Friday"; break; case "Saturday": echo "Today is Saturday"; break; default: echo "Day is not found"; } ?>
<script> var d = new Date(); var day = new Array(7); day[0] = "Today is Sunday"; day[1] = "Today is Monday"; day[2] = "Today is Tuesday"; day[3] = "Today is Wednesday"; day[4] = "Today is Thursday"; day[5] = "Today is Friday"; day[6] = "Today is Saturday"; document.write(day[d.getDay()]); </script>
break Statement
break Statement loop को एक विशिष्ट condition पर बंद कर देता है |
Syntax for break Statement
break;
Example for break Statement
Source Code :Output :1234567891011<?php for( $i=0; $i<10; $i++){ echo "Value of i is $i <br />"; if($i==5){ break; } } ?>
Value of i is 0 Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4 Value of i is 5
continue Statement
continue Statement का इस्तेमाल loop को repeat करने के लिए किया जाता है | यहाँ पर कुछ statements को skip भी किया जाता है |
Syntax for continue Statement
continue;
Example for continue Statement
Source Code :Output :123456789101112<?php for( $i=0; $i<10; $i++ ){ if($i==5){ echo "Number $i is skipped <br />"; continue; } echo "Value of i is $i <br />"; } ?>
Value of i is 0 Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4 Number 5 is skipped Value of i is 6 Value of i is 7 Value of i is 8 Value of i is 9
0 comments:
Post a Comment
Thanks