Follow Star

Control Structures #switch


عدة حالات . تنفيذ الحالة المظابقة . و إلا نفذ الإفتراضي .

ملاحظة : يشبه if elseif


1. بناء الجملة .


1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
switch (variable) {
  case 'value':
    // code...
    break;

  default:
    // code...
    break;
}
?>

  

2. تطبيق البناء .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$a = 5;
switch ($a) {
  case '5':
    echo "a = 5";
    break;

  default:
    echo "a !== 5";
    break;
}
?>  

  

3. strings زيادة القضايا أو الكتل .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
$a = "sign in";
switch ($a) {

  case 'New user':
    echo "Register your blog";
    break;

  case 'member':
    echo "Enter the password";
    break;


  case 'unregistered':
    echo "Register";
    break;


  case 'editor':
    echo "password";
    break;    

  case 'sign in':
    echo "Welcome What's New?";
    break;

  default:
    echo "You cannot access the requested newspaper";
    break;
}
?>  




  

4. Integer زيادة القضايا أو الكتل .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
$a = 5;
switch ($a) {

  case '1':
    echo "Register your blog";
    break;

  case '2':
    echo "Enter the password";
    break;


  case '3':
    echo "Register";
    break;


  case '5':
    echo "password";
    break;    

  case '4':
    echo "Welcome What's New?";
    break;

  default:
    echo "You cannot access the requested newspaper";
    break;
}
?>  




  

البناء السهل .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
$a = 'hallo5';
switch ($a) {
  case 'hallo1': echo "string1"; break;
  case 'hallo2': echo "string2"; break;
  case 'hallo3': echo "string3"; break;
  case 'hallo4': echo "string4"; break;
  case 'hallo5': echo "string5"; break;
  default: echo "string"; break;
}

 ?>




  

يمكن أن نفعل مثل هذا .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
$a = date("h");
$b = date("M,d,Y h:i:s A");  
switch ($a) {
  case '1': echo "The time is now $b"; break;
  case '2': echo "The time is now $b"; break;
  case '3': echo "The time is now $b"; break;
  case '4': echo "The time is now $b"; break;
  case '5': echo "The time is now $b"; break;
  case '6': echo "The time is now $b"; break;
  case '7': echo "The time is now $b"; break;
  case '8': echo "The time is now $b"; break;
  case '9': echo "The time is now $b"; break;
  case '10': echo "The time is now $b"; break;
  case '11': echo "The time is now $b"; break;
  case '12': echo "The time is now $b"; break;

  default: echo "string"; break;
}

 ?>

  
Control Structures      
if      
else      
elseif/else if      
Alternative syntax for control structures      
while      
do-while      
for      
foreach      
break      
continue      
switch      
declare      
return      
require      
include      
require_once      
include_once      
goto      

Tags:

Categories:

Updated:

Comments