Tag Archives: Javascript

PHP Array Sort Functions

The elements in an array can be either be sort ascending, descending, alphabetical and numerical order. Lets discuss the different sort function used in PHP.

– sort() – used to sort arrays in ascending order.
– rsort() – used to sort arrays in descending order.
– asort() – used to sort associative arrays in ascending order as per values.
– ksort() – used to sort associative arrays in ascending order as per key.
– arsort() – used to sort associative arrays in descending order as per  values.
– krsort() – used to sort associative arrays in descending order  as per key.
– shuffle() – used to randomizes the order of the elements in an array.

Sort arrays in ascending order sort()

<?php
$players = array("Sambir","Rupal","Aadarsh");
sort($players);
print_r($players);
?>

Output:
Array(
[0] => Aadarsh
[1] => Rupal
[2] => Sambir
)

Sort Array in Descending Order – rsort()

<?php
$players = array("Rupal","Sambit","Aadarsh");
rsort($players);
print_r($players);
?>

Output:
Array(
[0] => Sambir
[1] => Rupal
[2] => Aadarsh
)

asort()

<?php
$players = array("Sambir"=>"9","Rupal"=>"10","Aadarsh"=>"8");
asort($players);
print_r($players);
?>

Output:
Array(
[Aadarsh] => 8
[Sambir] => 9
[Rupal] => 10
)

ksort()

<?php
$players = array("Sambir"=>"9","Rupal"=>"10","Aadarsh"=>"8");
ksort($players);
print_r($players);
?>

Output:
Array(
[Aadarsh] => 8
[Rupal] => 10
[Sambir] => 9
)

ksort()

<?php
$players = array("Sambir"=>"9","Rupal"=>"10","Aadarsh"=>"8");
ksort($players);
print_r($players);
?>

Output:
Array(
[Aadarsh] => 8
[Rupal] => 10
[Sambir] => 9
)

arsort()

<?php
$players = array("Sambir"=>"9","Rupal"=>"10","Aadarsh"=>"8");
arsort($players);
print_r($players);
?>

Output:
Array(
[Rupal] => 10
[Sambir] => 9
[Aadarsh] => 8
)

krsort()

<?php
$players = array("Sambir"=>"9","Rupal"=>"10","Aadarsh"=>"8");
krsort($players);
print_r($players);
?>

Output:
Array(
[Sambir] => 9
[Rupal] => 10
[Aadarsh] => 8
)

shuffle()

<?php
$players = array("Sambir","Rupal","Aadarsh");
shuffle($players);
print_r($players);
?>

Output: /* Every time you refresh the value changes. */
Array(
 [0] => Aadarsh
[1] => Sambir
[2] => Rupal
)

PHP Array

Introduction to Array

When creating long, complex programs, it sometimes becomes difficult to access certain elements in a program. for example there is a limitation for using variables in a program: when you declare a variable, you can assign only one value to it. In certain case , you may want to declare hundreds of variables and it may not be possible to remember each and every variable and use it. Array help to accomplish this. An array is a special kind of variable that unlike conventional variables, can storemultiple values that are indexed. To understand this definition let it split it into three parts:.
– An array is a special kind of variable because it is primarily use for strong values.
– Unlike conventional variables, array can store multiple values without any limitation.
– Each value that is assigned to an array is accessed using an index in the form of a number or a string.

Features of Arrays

There are a few underlying features of array that help qualify them as one of the most important elements in any complex program.
– Any number of values can be stored in a single array.
– The value stored in an array can be numeric or string.
– After the value are stored in the array, they can be extracted from it at any given time.
– Arrays enhance the scope and flexibility of a program by allowing data to be accessed easily.
– Array organize complex datastructure that are related in some way.
– It is possible to add values to an array after it is created. which is not possible in case of a variable.
– Values in an array can be retrieved sequentially and randomly.

CREATING A SIMPLE ARRAY

Creating an array is as simple as creating a variable. The only difference is that its main purpose is to hold multiple related values in the same place. consider the fallowing example

<?php
     $week[] = "Sunday";
     $week[] = "Monday";
     $week[] = "Tuesday"; /* And So on */  
?>

Declaring an array by using the array () function

<?php
  $week = array("Sunday", "Monday", "Tuesday","etc..");
?>

ASSOCIATIVE ARRAYS

Arrays that are indexed by using strings are called associative arrays. When you use them, you specified a string instead of number within the square brackets.

<?php
   $student[Name] = "Rishi";
   $student[Age] = "8";
   $student[Game] = "Chess";
?>

OR

<?php
   $student = array(Name=>"Rishi", Age=>"8", Game=>"Chess");
?>

MULTIDIMENSIONAL ARRAYS

As discuss earlier, array can store numbers as well as names. They can also store other arrays. Sucharrays are multidimensional arrays or an array of arrays.

<?php
 $student = array(
 array( Name=>"Rishi", Age=>"8", Game=>"Chess"),
 array( Name=>"Sambit", Age=>"9", Game=>"Chess"),
 array( Name=>"Rupal", Age=>"10", Game=>"Chess"),   
);

echo $student[1][Age];
?>

Output: 9;

Looping through arrays

Another method of accessing the elements in an array and displaying them is by looping. You can use the foreach statement to loop through enumerated, associative and multidimensional arrays.

Looping through an enumerated array

As discuss earlier, the foreach statement comes in handy when looping through an enumerated array. Consider the fallowing example

<?php
foreach ($array as $val){
  echo "My Value=".$val ."<br/>";
}
?>

Looping through an associative array

The foreach statement is also used for looping through an associative arrays, but there is a slight change in the way you use it. You would need to specified the key as well as the value of an element to access a particular element. consider an example

<?php
foreach ($array as $key =>$val){
  echo " $key = $val <br/>";
}
?>

Displaying a multidimensional array

After learning how to loop through enumerated arrays and associative arrays, you might want toknow how to loop through a multidimensional arrays. You can combine your knowledge to looping through enumerated and associative arrays to accomplish this.

<?php
 $student = array(
 array( Name=>"Rishi", Age=>"8", Game=>"Chess"),
 array( Name=>"Sambit", Age=>"9", Game=>"Chess"),
 array( Name=>"Rupal", Age=>"10", Game=>"Chess"),   
);
foreach($student as $st){
 foreach($st as $key => $val){
   echo "$key = $val"."<br/>";
  }
 echo "<hr>";
}
?>

Output:
Name = Rishi
Age = 8
Game = Chess
-------------------------------------
Name = Sambit
Age = 9
Game = Chess
--------------------------------------
Name = Rupal
Age = 10
Game = Chess

PHP Loops

Introduction to Loops in PHP

Using Loops in PHP

Loops are an vital part of a programming language. Typically the need to use loops in a program occur when you want to execute a piece of code repeatedly until a desired condition is achieved. There are many control structures that can be use to implement looping in a program.

The While loop

The while loop is one is the simplest looping control structure in PHP. When you specify a conditionand a code block along with the while loop, the code block is repeatedly execute until the conditionbecomes false.

<?php
$a = 1;
while ($a <= 10){
  echo $a ."<br/>";
}
?>

The for loop

The for loop is similar to the while loop and can achieve the same result. It is use to evaluate a set ofconditional expression at the beginning of each loop. The for loop is one of the most complex loop in PHP.

<?php
for ($a=1; $a <=10 ; $a++){
  echo $a ."<br/>";
}
?>

Breaking a loop

Sometimes, you might want to break a loop before it complete execution .The break statement can do this.

<?php
for ($a=1; $a <=10 ; $a++){
echo $a ."<br/>";
  if($a==5){
    break;
  }
}
?>

The continue statement

The continue statement is the reverse  the break statement, doesn’t breaks a loop. When you break a loop the loop with the break statement, you can’t check for the same condition again. The continue statement just skip the specific iteration and doesn’t cause the loop to end.

<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
  echo $i;
}
?>

The output will be: 2.

Nesting loops

Nesting loops are a way to use a loop statement within another statement. for example, if you have a loop x, and have another loop y in the loop y in the loop x, the loop y will be executed x*y.

<?php
for ($a =10; $a <= 15; $a++){
    echo "First Loop <br/>";
       for ($b = 5; $b < 10; $b++){
           echo "Second Loop <br/>";
       }
}
?>

PHP Control Structures

Introduction to Control Structures

When you start writing programs in PHP, you will realize the importance of using control structures. They are use to control how a structure will be executed, adding useful functionality to the PHP code and making it more flexible.

Condition and Statements

Control structure are usually based on condition and therefore always have condition associated with them .They check the status of these conditions ,which can be either true or false .A statement can be used with a control structure to tell the program what to do depending on the result of the condition specified. Consider the fallowing example to understand how a condition differs from a statement. If Rishi promises that he won’t neglect his studies, buy a Tab for him. This sentence can be broken intotwo parts, a condition and a statement. The first part of sentence, If Rishi promises that he won’t neglect his studies, is a condition. The status of the condition will decide whether Rishi  will get the Tab or not. If the condition is true , Rishi will get the Tab.

AS the name suggest, conditioal statement use to check for condition and execute code on the basis of the value returned. This section discuss if, else, elseif and switch conditional statements.

The if statement

We discussed the if statement briefly in the previous chapter; this section contains a detailedexplanation of it .The if statement is one of the most commonly use statement in PHP or any programming language.

Allows conditional execution.

The if statement can check for a particular condition and also perform a specific action depending on whether the condition is true or false.

In capable of containing multiple code blocks. You can set more than one operation to be performed depending on the result of the condition specified in the PHP code.

Can also be nested within an existing if statement. You can use another If statement within an exciting one to check another condition within an exciting condition.

<?php
$a = 5;
$b = 7;
$c = $a+$b;
  if ($c > 9){ 
    echo "The sum is greater than 9.";
  }
?>

The else statement

The else statement as discussed in the previous chapter is always used with the if statement. When you use the if statement you check condition and specify a statement to be executed when the condition is true. The else statement is used to specified a statement in case the result of the condition is false. The uses of else statement is similar to that of the if statement. The else statement is also used to check the alternative block of code.

<?php
$a = 10;
$b = 5;
$c = $a + $b;
 if ($c == 20) {
   echo "The sum is equal to 20.";
 else{
     echo "The sum is not equal to 20.";
 }
?>

The elseif statement

The elseif statement, as the name suggest, is a combination of the else and if  statements. you can check for multiple conditions by using the elseif statement. you can also have multiple elseif statement under an if statement. An else statement is executed only when the previous if statementreturns a false value. You can write both else if or elseif, with space and without space both are correct.

<?php
$playing = "Chess";
if ($playing == "Cricket") {
  echo "Rishi is playing cricket.";
}else if ($playing == "Chess"){
  echo "Rishi is playing Chess.";
}else{
  echo "Rishi is not playing.";
}
?>

The switch statement

The switch statement can be used in placement of the if statement. Similar to the if statement, the switch statement is also used to change the flow of a program depending on the evolution of an expression. Although the if statement is also used to achieve the same result, there are certain differences between the if and switch statement:

– The else if statement can be used with the if statement to evaluate multiple conditions and expressions. In contrast switch evaluates only one expressions at a time and executes the corresponding code specified for the expressions.

– The result generated as part of the if statement returns a Boolean value, which is either true or false, but in the case of switch ,the result of a condition can be evaluated against any number of values.

<?php
$game = "Chess";
switch($game){
    case "Cricket";
       echo "Rishi is playing Cricket";
       break;
    case "Football";
       echo "Rishi is paying Foot Ball";
       break;
    case "Chess";
       echo "Rishi is playing Chess";
       break;
}
?>

In this code notice that
– Multiple case statement have been used with respect to the switch statement.
– Each case statement ends with a colon.
– A break statement is include in each case statement.

The output of the code is: Rishi is playing Chess

PHP Logical Operator

Logical Operators

The Logical operators are used in PHP to determine the status of condition . When you use if…else or while statements, logical operator execute code based on the status of a condition .In this section, we will discuss only two logical operators:

The and operate (&&) is used when a statement has two condition to be checked. If both thecondition are true , the statement is true,.

The or operator ( || ) is also used when a statement has two condition is true, the statement is true.

<?php
  $a = 10;
  $b = 15;
  $c = $a + $b;
  if( ($a > 5) && $c < 20 ){
    echo "Value of c is less than 20";
  }else {
    echo "Value of c is more than 20"; 
  }
?>

The Output of the above code is: Value of c is more than 20