Monday, March 31, 2014

PHP Switches



The switch block allows a program to choose what code to execute based on the value of a variable. This is helpful in handling multi-case scenarios where you are unsure about what the variable's value will be at the time of execution. It also servers as a more organized alternative to multiple "if else" statements.

The following example deals with code that executes based on the numeric value of variable $x

switch ($x)
{
case 4:
  $x=$x+2
  break;

case 5:
$x=$x+4  break;

case 6:
$x=$x+10
  break;

default:
  $x=7;
}

The code above determines what value to add to $x, based on what value it holds going into the switch statement. For example, 2 is added to $x if $x=4 at the time of execution. If $x is not equal to 4, 5, or 6 at the time of execution, then the "default" code is executed, setting $x=7.

While it would be possible to execute the same code with a handful of "if and else" statements, most would agree that the switch option provides for a cleaner code.


Saturday, March 15, 2014

Arrays in PHP

In most languages, including PHP, an array is a collection of objects of the same data type. An array could consist of strings,  integers,  or virtually any other data type supported by PHP. Using array's can add some organization to code, when they are strategically used in place of local variables


To define an array, the following two syntax's can be used

1. Declaring and defining the array at the same time
$colors= array("blue", "green", "red", "yellow");

2. Declaring the array, and defining it later

$colors=array()
$colors[0]="blue"
$colors[1]="green"
$colors[2]="red"
$colors[3]="yellow"

No matter what syntax you used to define the array, individual array elements can be referenced by their position at a later point in time

echo $colors[1]
//would print "green"

In addition to referring to individual array elements, PHP allows a handful of sorting mechanisms for arrays:
  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key
[provided by W3 schools]

Finally, a very useful feature with Arrays in PHP is the ability to manually choose and assign a key to each array element.
Picture a situation in which instead of referring to a colour by its number, we could use a person's name. This would be useful in a situation where we were keeping track favorite colours.

$age=array();
$favorite['jon']="green";
$favorite['mike']="red";
$favorite['sam']="blue";
$favorite['andrew']="yellow";


$favorite['jon'] would equal 'green'
echo "Jon's favorite colour is". $favorite['jon']



Midsize Program



The following is a simple Binary to decimal converter program written in PHP.
Please note that the Binary input is hard coded, as this blog has yet to cover user inputs within PHP.
However, the program will run and execute for any Binary number that is written in.

<?php

$BinaryNumber="1010";
$StringLength=strlen($BinaryNumber);
 $DecimalTotal=0;
$NumericvalueofDigit=0;

for ($i=1; i<=$StringLength; $i++){

$BinaryDigit=substr($BinaryNumber, ($i*-1),1);
$NumericvalueofDigit=((int)$BinaryDigit)*pow(2,($i-1));
$DecimalTotal=$DecimalTotal + $NumericvalueofDigit;
}

echo $DecimalTotal;

?>