Thursday, 26 May 2016

What is the difference between continue and break in PHP

Continue is used to stop script on specific condition and then continue looping statement until reach the end..
Break is used to exist the loop totaly once specific condition match

Bottom of line:
continue iterate loop and skip rest of part on specific condition and start from begining of loop until reach the end, whereas break throw out of loop on specific condition.

Example
 for($i=0; $i<10; $i++){  
   if($i == 5){  
     echo "continue condition is matched, skip rest of part.<br>";  
     continue;  
   }  
   echo $i . "<br>";  
 }  
 echo "<hr>";  
 for($i=0; $i<10; $i++){  
   if($i == 5){  
      echo "break condition is matched, stop execution here.<br>";  
      break;  
   }  
   echo $i . "<br>";  
 }  
Output:
0 1 2 3 4 continue condition is matched, skip rest of part. 6 7 8 9
------------------------------------------------------------------------------
0 1 2 3 4 break condition is matched, stop execution here.

Friday, 20 May 2016

What is the difference between isset and empty in PHP

Isset() checks if a variable has a value including ( Flase , 0 , or Empty string) , But not NULL.
Returns TRUE if var exists; FALSE otherwise.

empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value.

Examples
 function checkIfIsset($value){
      if (isset($value)) {
        return "It is set";
      } else {
        return "It is not set";
      }
 }
 function checkIfnotEmpty($value){  
      if (!empty($value)) {  
        return "It is not empty";  
      } else {  
        return "It is empty";  
      }  
 }  
Examples of Isset method.
Exampl 1: Lets set $value to False and call function checkIfIsset as below

 $value = False;  
 echo checkIfIsset($value);  
Out put: It is set
Example 2: Lets set $value to NULL and call function checkIfIsset as below
 $value = Null;  
 echo checkIfIsset($value);  
Out put: It is not set
Examples of Empty method.
Example 1: Lets set $value to empty string and call function checkIfnotEmpty as below
 $value = '';  
 echo checkIfnotEmpty($value);  
Out put: It is empty
Example 2: Lets set $value to zeo and call function checkIfnotEmpty as below
 $value = 0;  
 echo checkIfnotEmpty($value);  
Out put: It is not empty

Reference Link: http://whatisdifference.com/what-is-the-difference-between-isset-and-empty-in-php/