30 April 2016

PHP Basic


  • Variable
PHP has a total of eight data types which we use to construct our variables:

Integers: are whole numbers, without a decimal point, like 4195.

Doubles: are floating-point numbers, like 3.14159 or 49.1.

Booleans: have only two possible values either true or false.

NULL: is a special type that only has one value: NULL.

Strings: are sequences of characters, like 'PHP supports string operations.'

Arrays: are named and indexed collections of other values.

Objects: are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.

Resources: are special variables that hold references to resources external to PHP (such as database connections).



  • Boolean
Interpreting other types as Booleans:

If the value is a number, it is false if exactly equal to zero and true otherwise.
If the value is a string, it is false if the string is empty (has zero characters) or is the string "0", and is true otherwise.
Values of type NULL are always false.
If the value is an array, it is false if it contains no other values, and it is true otherwise. For an object, containing a value means having a member variable that has been assigned a value.
Valid resources are true (although some functions that return resources when they are successful will return FALSE when unsuccessful).
Don't use double as Booleans.

<?php
$true_num1 = 10;
$true_num2 = 3 + 0.14159;
$true_str = "Tried and true";

$true_array5 = array('name'=>'','class'=>'ER-101','score'=>'0','lecture'=>array());
$true_array1 = array(1);
$true_array2 = array(1,2);
$true_array3 = array("");
$true_array4 = array(0);
$true_array[50] = "An array element";
$true_array[51] = 150;
$true_array['class'] = "ER-101";
$true_array['score'] = 100;

$false_array = array();
$false_array[50] = "";
$false_array[51] = "0";
$false_array[52] = 0;
$false_array[53] = -10;
$false_array[54] = array();
$false_array['name'] = "";
$false_array['score'] = "0";
$false_array['score'] = 0;
$false_array['score'] = -10;
$false_array['lecture'] = array();

$false_null = NULL;
$false_num1 = 999 - 999;
$false_num2 = 0;
$false_num3 = -10;
$false_str1 = "";
$false_str2 = "0";

if ($false_array) {
  echo "Have a nice day!";
} else {
  echo "FALSE";
}
?>
  • empty(): Determine whether a variable is empty
       bool empty ( mixed $var 

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
The following things are considered to be empty:
  1. "" (an empty string)
  2. 0 (0 as an integer)
  3. 0.0 (0 as a float)
  4. "0" (0 as a string)
  5. NULL
  6. FALSE
  7. array() (an empty array)
  8. $var; (a variable declared, but without a value) = NULL
<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?>
  • is_array() : Finds whether a variable is an array
       bool is_array ( mixed $var )

<?php
$false_array = array();
if (is_array($false_array)) {
  echo "<h1>TRUE</h1>";
}
?>

  • is_object(): Finds whether a variable is an object
       bool is_object ( mixed $var )

<?php
$true_obj = new stdClass();

if ($true_obj) {
  echo "<h1>TRUE</h1>";
}

if (is_object($true_obj)) {
  echo "<h1>TRUE</h1>";
}

if (!empty($true_obj)) {
  echo "<h1>TRUE</h1>";
}
?>

  • isset(): Determine if a variable is set and is not NULL
      bool isset ( mixed $var [, mixed $... ] )

 <?php
$true = "";
$true = "0";
$true = 0;
$true = -10;
$true = array();
$true = new stdClass();
$false = NULL;

if (isset($false)) {
  echo "<h1>FALSE</h1>";
}
?>
  • if....else statement
<?php
$d="Fri";
if ($d=="Fri")
  echo "Have a nice weekend!"; 
else
  echo "Have a nice day!"; 
?>
  • if...elseif...statement
<?php
$d="Fri";
if ($d=="Fri")
  echo "Have a nice weekend!"; 
elseif ($d=="Sun")
  echo "Have a nice Sunday!"; 
else
  echo "Have a nice day!"; 
?>
  • switch statement
<?php
$d="Thu";
switch ($d)
{
case "Mon":
  
  echo "Today is Monday";
  break;
case "Tue":
  
  echo "Today is Tuesday";
  break;
case "Wed":
  
  echo "Today is Wednesday";
  break;
case "Thu":
  
  echo "Today is Thursday";
  break;
case "Fri":
  
  echo "Today is Friday";
  break;
case "Sat":
  
  echo "Today is Saturday";
  break;
case "Sun":
  
  echo "Today is Sunday";
  break;
default:
  
  echo "Wonder which day is this ?";
}
?>


<?php
$d="Fri";
switch ($d)
{
case "Mon":
case "Tue":
case "Wed":

  echo "Today is boring";
  break;
case "Thu":

  echo "Today is getting better";
  break;
case "Fri":

  echo "Today is Friday";
  break;
case "Sat":
case "Sun":

  echo "Today is much better";
  break;
default:

  echo "Wonder which day is this ?";
}
?>
  • Break: ends execution of the current for, foreach, while, do-while or switch structure. 
eg.1

$array = array(1,2,3);
foreach ($array as $item){
  if ($item == 2) {
    break;
  }
  echo $item;
}

outputs "1" cause loop was broken ever, before echo was able to print "2".
 
eg.2 
$numbers = array(1,2,3);
$letters = array("A","B","C");
foreach ($numbers as $num){
  foreach ($letters as $char){
    if ($char == "C") {
      break 2; // if this was break, o/p will be AB1AB2AB3
    }
    echo $char;
  }
  echo $num;
}
outputs AB cause of break 2 - both statements was broken quite early. If this was break, the output will be AB1AB2AB3.
  • Continue: is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration. Note: Note that in PHP the switch statement is considered a looping structure for the purposes of continue.
eg.1

$array = array(1,2,3);
foreach ($array as $item){
  if ($item == 2) {
    continue;
  }
  echo $item;
}
outputs 13 cause second iteration was passed

eg.2

$numbers = array(1,2,3);
$letters = array("A","B","C");
foreach ($numbers as $num){
  foreach ($letters as $char){
    if ($char == "C") {
      continue 2;
    }
    echo $char;
  }
  echo $num;
} 
output ABABAB, cause of continue 2 - outer loop will be passed every time
 
Ref: http://stackoverflow.com/questions/5167561/loop-break-continue-break-2-continue-2-in-php