Adscend

Switch Case Statement

Switch Statement

The Switch statement in PHP is used to perform one of several different actions based on one of several different conditions.
Switch statement is used to when a condition may have multiple results.
If you want to select one of many blocks of code to be executed, use the Switch statement.
Switch statement is similar to if..else if..else statement.
Switch statement is used to avoid long blocks of if..else if..else code.

Syntax:
switch (expression) 

case label1: 
  code to be executed if expression = label1; 
  break;   
case label2: 
  code to be executed if expression = label2; 
  break; 
default: 
  code to be executed 
  if expression is different  
  from both label1 and label2;  


How it works:
1.The value of the expression is compared with the values for each case in the structure .
2.If there is a match, the code associated with that case is executed.
3.After a code is executed, break is used to stop the code from running into the next case.
break means, if there is one statement executes, then there is no need to go rest of case statement.
4.The default statement is used if none of the cases are true.
Last case is always default. This is similar to the last else statement.
If none of the above conditions are true, then default will execute.

Example: 1
<?php 
$x 
4
switch (
$x

case 
1
  echo 
"Number 1"
  break; 
case 
2
  echo 
"Number 2"
  break; 
case 
3
  echo 
"Number 3"
  break; 
default: 
  echo 
"No number between 1 and 3"

?>
Output
No number between 1 and 3

Using Switch to Display Random Quotes:
Example: 2
<?php     
//Chooses a random number $num Rand (1,6); 
//Based on the random number, gives a quote 
switch ($num

case 
1
echo 
"I know I can change my life by changing my attitude."
break; 
case 
2
echo 
"If I can't control my anger, I will be the loser."
break; 
case 
3
echo 
"I am free from the influence of all negative and 
      reactive thoughts and words."

break; 
case 
4
echo 
"Positive thoughts and actions will ensure my wellbeing.";
break; 
case 
5
echo 
"With good planning and initiative I am becomingself 
    reliant in every respect."

break; 
case 
6
echo 
"Fortune salutes the brave. I am brave, I am fearless."

?> 

Output
I am free from the influence of all negative and reactive thoughts and words.

Using Switch to Display Random Images:

Example: 3
<?php //Chooses a random number $num Rand (1,4); //Based on the random number, gives a image switch( $num 

case 
1$image_file "images/1.jpg"
break; 
case 
2$image_file "images/2.jpg"
break; 
case 
3$image_file "images/3.jpg"
break; 
case 
4$image_file "images/4.jpg"
break; 

echo 
"Random Image : <img src=$image_file />"?> 

Example: 4
<?php 
$country 
"USA"
switch(
$country

case 
"USA"
echo 
"you are in USA"
break; 
case 
"Japan"
echo 
"you are in Japan"
break; 
default: 
echo 
"you dont know where you are"
break; 
?> 
Output
you are in USA




Global Variable

Global Variable


1.A variable has a global scope if it is defined outside of any function.
2. Global variables can be accessed from any part of the program except function.
3.To access a global variable from within a function, use the global keyword.


Example: 1
<?phpfunction echo_ip(){
  
$user_ip=$_SERVER["REMOTE_ADDR"]; // local scope
  
$str="Your IP address is ".$user_ip;
  echo 
$str;
}
echo_ip();?>
Output
Your IP address is ::1 
Here $user_ip is declared local.


Example: 2
<?php
$user_ip
=$_SERVER["REMOTE_ADDR"]; // global scopefunction echo_ip(){$str="Your IP address is ".$user_ip;
echo 
$str;
}
echo_ip();?>
Output
Here we don't see any output. Because $user_ip is declared global.

To access $user_ip global variable from within a function, use the global keyword.


Example: 3
<?php
$user_ip
=$_SERVER["REMOTE_ADDR"]; // global scopefunction echo_ip(){
  global 
$user_ip;
  
$str="Your IP address is ".$user_ip;
  echo 
$str;
}
echo_ip();?>
Output
Your IP address is ::1 

Example: 4
<?php
$a 
5;$b 10;
function 
sum(){
  global 
$a$b;
  
$b $a $b;
}
sum();
echo 
$b;?>
Output
15 

If Else If Else Statement



Syntax:
if(condition1)
{
     // execute this block if condition1 is true
}
else if (condition2)
{
     // execute this block if condition2 is true
}



else{
     // execute this block if all above conditions are false
}
Conditional statements are used to perform different actions based on different conditions.

1. if else if else statement are also known as conditional statements. This simply means that they are able to take action or decision based on various conditions.
2. if else if else statement is used to check one blocks of code from many blocks of code.
If the condition is true then the code of block inside if will be executed.
Otherwise the code of block inside else if will be executed.
If all conditions are false then the code of block inside else will be executed.
3. if else if else statement allows the computer to make decisions based on the values of variables which can take the specified action based on the input that is given.
If the decision or condition is true then the code of block inside if will be executed.
If the decision or condition is false then the code of block inside else if will be executed.
If all conditions are false then the code of block inside else will be executed.

You can use this elseif statement many times as you want.
You can just use it one time or 40 times, it does not matter.

Example: 1

<?php 
  $name
="mila"
if (
$name=="mila"
  { 
    echo 
"hello mila"
   } 
else if (
$name=="joe"
   { 
    echo 
"hello joe"
    } 
else 
    { 
    echo 
"nobody is here"
    } 
?>
Output
hello mila 

Example: 2

<?php 
  $name
="joe"
if (
$name=="mila"
  { 
    echo 
"hello mila"
   } 
else if (
$name=="joe"
   { 
    echo 
"hello joe"
    } 
else 
    { 
    echo 
"nobody is here"
    } 
?> 
Output
hello joe 

Example: 3

<?php 
  $name
="Aspell"
if (
$name=="mila"
  { 
    echo 
"hello mila"
   } 
else if (
$name=="joe"
   { 
    echo 
"hello joe"
    } 
else 
    { 
    echo 
"nobody is here"
    } 
?> 
Output
nobody is here 




If Statement Syntax Example


If Statement


Syntax:
if(condition)
{
     // execute this block if condition is true
}


Conditional statements are used to perform different actions based on different conditions.
1.If statements is also known as conditional statements. This simply means that they are able to take action based on various conditions.
2.If statements is used to check whether specified condition is true or false.
3.If statements allows the computer to make decisions based on the values of variables which can take the specified action based on the input that is given.

Example: 1

<?php 
$num1
=20$num2=30
if (
$num1<$num2
  echo 
"This line will show on browser"?>
Output
This line will show on browser

Example: 2

<?php 
$num1
=20$num2=30
if (
$num2<$num1
  echo 
"This line will not show on browser"?>
Output
The browser will show nothing
Because, the condition is false and 30 is greater than 20.


If Else Statement

Syntax:
if(condition)
{
     // execute this block if condition is true
}
else{
     // execute this block if condition is false
}
Conditional statements are used to perform different actions based on different conditions.
1.If else statements are also known as conditional statements. This simply means that they are able to take action or decision based on various conditions.
2. If else statements is used to check whether specified condition is true or false.
If the condition is true then the code of block inside if will be executed. If the condition is false then the code of block inside else will be executed.
3. If else statements allows the computer to make decisions based on the values of variables which can take the specified action based on the input that is given.
If the decision or condition is true then the code of block inside if will be executed. If the decision or condition is false then the code of block inside else will be executed.

Example: 1

<?php 
$num1
=20$num2=30
if (
$num1<$num2
  echo 
"This line will show on browser"?>
Output
This line will show on browser

Example: 2

<?php 
$num1
=20$num2=30
if (
$num2<$num1
  echo 
"This line will not show on browser"?>
Output
The browser will show nothing
Because, the condition is false and 30 is greater than 20.






Comment Block Syntax


Comments in PHP


The comment tag is used to insert a comment in the PHP code. In browser, comment will not appear. It is useful to insert commentin PHP code when code is needed to edit later.

In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.


Example
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>