Tag Archives: Approach

PHP Array All Functions

PHP Array Functions

The following list show the different function used in an array.
– array_change_key_case : Changes all keys in an array
– array_chunk : Used to split an array into pieces.
– array_column : Used to return the values from a single column in the input array
– array_combine : Used to creates an array by using one array for keys and another for its values.
– array_count_values : Used to counts all the values of an array.
– array_diff_assoc : Computes the difference of arrays with additional index check
– array_diff_key : Computes the difference of arrays using keys for comparison
– array_diff_uassoc : Computes the difference of arrays with additional index check which is performed by a user supplied callback function
– array_diff_ukey : Computes the difference of arrays using a callback function on the keys for comparison
– array_diff : Computes the difference of arrays
– array_fill_keys : Fill an array with values, specifying keys
– array_fill : Fill an array with values
– array_filter : Filters elements of an array using a callback function
– array_flip : Exchanges all keys with their associated values in an array
– array_intersect_assoc : Computes the intersection of arrays with additional index check
– array_intersect_key : Computes the intersection of arrays using keys for comparison
– array_intersect_uassoc : Computes the intersection of arrays with additional index check, compares indexes by a callback function
– array_intersect_ukey : Computes the intersection of arrays using a callback function on the keysfor comparison
– array_intersect : Computes the intersection of arrays
– array_key_exists : Checks if the given key or index exists in the array
– array_keys : Return all the keys or a subset of the keys of an array
– array_map : Applies the callback to the elements of the given arrays
– array_merge_recursive : Merge two or more arrays recursively
– array_merge : Merge one or more arrays
– array_multisort : Sort multiple or multi-dimensional arrays
– array_pad : Pad array to the specified length with a value
– array_pop : Pop the element off the end of array
– array_product : Calculate the product of values in an array
– array_push : Push one or more elements onto the end of array
– array_rand : Pick one or more random entries out of an array
– array_reduce : Iteratively reduce the array to a single value using a callback function
– array_replace_recursive : Replaces elements from passed arrays into the first array recursively
– array_replace : Replaces elements from passed arrays into the first array
– array_reverse : Return an array with elements in reverse order
– array_search : Searches the array for a given value and returns the corresponding key if successful
– array_shift : Shift an element off the beginning of array
– array_slice : Extract a slice of the array
– array_splice : Remove a portion of the array and replace it with something else
– array_sum : Calculate the sum of values in an array
– array_udiff_assoc : Computes the difference of arrays with additional index check, compares data by a callback function
– array_udiff_uassoc : Computes the difference of arrays with additional index check, compares data and indexes by a callback function
– array_udiff : Computes the difference of arrays by using a callback function for data comparison
– array_uintersect_assoc : Computes the intersection of arrays with additional index check, compares data by a callback function
– array_uintersect_uassoc : Computes the intersection of arrays with additional index check, compares data and indexes by a callback functions
– array_uintersect : Computes the intersection of arrays, compares data by a callback function
– array_unique : Removes duplicate values from an array
– array_unshift : Prepend one or more elements to the beginning of an array
– array_values : Return all the values of an array
– array_walk_recursive : Apply a user function recursively to every member of an array
– array_walk : Apply a user function to every member of an array
– array : Create an array
– arsort : Sort an array in reverse order and maintain index association
– asort : Sort an array and maintain index association
– compact : Create array containing variables and their values
– count : Count all elements in an array, or something in an object
– current : Return the current element in an array
– each : Return the current key and value pair from an array and advance the array cursor
– end : Set the internal pointer of an array to its last element
– extract : Import variables into the current symbol table from an array
– in_array : Checks if a value exists in an array
– key_exists : Alias of array_key_exists
– key : Fetch a key from an array
– krsort : Sort an array by key in reverse order
– ksort : Sort an array by key
– list : Assign variables as if they were an array
– natcasesort : Sort an array using a case insensitive “natural order” algorithm
– natsort : Sort an array using a “natural order” algorithm
– next : Advance the internal array pointer of an array
– pos : Alias of current
– prev : Rewind the internal array pointer
– range : Create an array containing a range of elements
– reset : Set the internal pointer of an array to its first element
– rsort : Sort an array in reverse order
– shuffle : Shuffle an array
– sizeof : Alias of count
– sort : Sort an array
– uasort : Sort an array with a user-defined comparison function and maintain index association
– uksort : Sort an array by keys using a user-defined comparison function
– usort : Sort an array by values using a user-defined comparison function

PHP Function

Introduction to function

A function is a part of independent code that can be use to perform any specific task. Function are use to get the required output. The output entirely depends on the code you specified in it. You can call the same function as many time as you want in the same page. A function does the fallowing things:

Accept value

A function accept values from the argument passed to it. But this is an optional part. You may not assign any value to a function.

Process value

A function executes the code specified within it.

Perform an action

Lastly this is the output that returns after the function is executed.

The advantage of using function in code are that, a function makes a code simple to read and understand and also ensure reusability of codes. This is because the same function can be called several times. To use a particular piece of code specified within a function, the developers just need to call the specific function.

Features of PHP functions

– Function are self contained .That is they are self sufficient blocks of code that can operate independently.
– A function needs to called in a spirit to be executed.
– Values can be passed with functions.
– A function can be declared and hen executed whenever required.
– A particular function can be called several times within the same page.
– A function can consist of PHP code, other functions, or a class definition.
– A function cannot be redefined after it is declared.

User defined versus built in function

In PHP ,function can be broadly categorized as user defined or built in functions. User define functions as the name suggest, are function created by a user as per requirement so the programmer can completely customize.

Unlike user defined functions, built in function are prewritten functions that are part of PHP. These functions have a predetermined functionality that doesn’t change. There are hundreds of built in functions available with PHP.

How to Define a Function

<?php
function MyFunction($value1, $value2){
 /* write your code here */
}
?>

Create a Simple Addition Function

<?php
function addMe($value1, $value2){
 $result = $value1 + $value2;
 return $result;
}
/* Call in action */
print addMe('6','3');

?>

The Output will be : 9.

PHP String Operator

String Operator

When you start writing PHP code, you will realize the importance of string operators. PHP uses twokinds of string operators, concatenation (.) the dot sign and concatenating assignment (.=) the dot and equal to sign.The concatenation operator is used when you want to combine the left and right argument between which the operator is placed. The concatenating assignment operator is a combination of the concatenation operators and the assignment operator.

Concatenation Operator
<?php
   $a = "My name is ";
   $b .= "Rishi";
   echo $b;

The output of the code is: My name is Rishi.

Concatenating Assignment operator
<?php
  $a = "My name is";r/>  $a.= "Rishi";
  echo $a;
?>

The output of the code is: My name is Rishi.

PHP Comparison Operator

Comparison Operators

This operator is used to compare two given values.

Name Symbol Usage Description
Equal to == $a==$b The value of $a is equal to the value of $b.
Not equal to != $a != $b The value of $a is not equal to the value of $b.
Less than < $a <$b The value of $a is less than the value of $b.
Greater than > $a > $b The value of $a is more than the value of $b.
Less than equal to <= $a <=$b The value of $a is either less than or equal to the value of $b.
Greater than equal to  >= $a >=$b The value of $a is either more than or equal to the value of $b.
Identical === $a===$b The value of $a is equal to the value of $b when the data type of both is same.

The if construct

In most cases , you will need to use control structures, or constructs in your code to use the operator effectively. Control structures are statement that check conditions assign a value create a loop within the code or call a function.
The if construct is a conditional construct. It is use to check for a conditional statement and perform some action if the condition is true or false.
The if construct is fallowed by a logical expression in which data is compared and a decision is made best of the result of the comparison.

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

The output of the above code will be: The sum is greater than 9.

The if construct has been used to check for a condition. Only when the sum of the variables $a and $b is more than 9 will the message “The sum is greater than 9″ will appear. If the condition specified is false then no message will be displayed. Notice that the action to be performed is enclosed within curly brackets.

The else statement

The else statement is use to specified an action to be performed after a specific condition has been checked, and it is used with he if construct.
A condition can be either true or false. Therefore you might want to perform an action in both cases. The if and else construct can be used together to achieve this result. The fallowing is an example where if and else construct have been used:

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

The output of the above code will be: The sum is not equal to 20.

Incrementing and Decrementing Operators

There are two types of incrementing and decrementing operators, post incrementing/decrementingoperators and pre incrementing /decrementing operators. Post incrementing operators are placed after a variable name, and pre incrementing /decrementing operators are placed before the name.

Operator Usage Output
Post incrementing $a++; Returns the value of $a and then adds 1 to the value
Post decrementing $a–; (2 minus signs) Returns the value of $a and then subtract 1 from the value
Pre incrementing ++$a; Adds 1 to the value of $a and then returns the new value
Pre decrementing –$a; (2 minus signs) Subtract 1 from the value of $a and returns the new value

Consider the fallowing code for a better understanding of incrementing and decrementing operators. This code shows how the post incrementing operators works.

Post Incrementing Operator
<?php
$a = 8;
$b = $a++;
echo $b; //Returns the value 8.
echo $a; //Returns the value 9.
?>
Post Decrementing Operator
<?php
$a = 10;
$b = $a--;
echo $b; // Returns the value 5.
echo $a; // Returns the value 4.
?>
Pre Incrementing Operator
<?php
$a = 8;
$b = ++$a;
echo $b; // Returns the value 9.
echo $a; // Returns the value 9.
?>
Pre Decrementing Operator
<?php
$a = 10;
$b = --$a;
echo $b; // Returns the value 9.
echo $a; // returns the value 9.
?>

String Operator

When you start writing PHP code, you will realize the importance of string operators. PHP uses two kinds of string operators, concatenation (.) the dot sign and concatenating assignment (.=) the dot and equal to sign.The concatenation operator is used when you want to combine the left and right argument between which the operator is placed. The concatenating assignment operator is a combination of the concatenation operators and the assignment operator.

Concatenation Operator

<?php
   $a = "My name is ";  $b .= "Rishi";
   echo $b;

The output of the code is: My name is Rishi.

Concatenating Assignment operator
<?php
  $a = "My name is";
  $a.= "Rishi";
  echo $a;
?>

The output of the code is: My name is Rishi.

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

PHP Arithmetic Operator

Arithmetic operators

Arithmetic operators are used to perform elementary mathematical calculation and are used when value of variable declare in integer type.

Name Example Output
Addition $a+$b The sum of $a and $b
Subtraction $a-$b The difference between $a and $b
Multiplication $a*$b The product of $a and $b
Division $a/$b The quotient of $a and $b
Modules $a%$b The remainder after dividing $a and $b

Let us look at some examples for a better understanding of arithmetic operators:

Addition
$a = 3;
$b = 6;
$c = $a+$b ; // The value of $c is now 9.
Subtraction
$a = 11;
$b = 2;
$c = $a-$b; //The value of $c is now 9.
Multiplication
$a = 3;
$b = 3;
$c = $a*$b; //The value of $c is now 9.
Division
$a = 18;
$b = 2;
$c = $a/$b ; //The value of $c is 9.
Modules
$a = 9;
$b = 3;
$c = $a%$b; //The value of $c is now 0.