# Monthly Archives: November 2015

## Sorting Arrays – PHP

Welcome to all,
Today we practise how to sort an array using php. We know this same problem solving using C, CPP or JAVA etc. But here I share how to solve this problem in php.
To arrange the elements in an array in numerical order from highest to lowest values (descending order) or lowest to highest value (ascending order). In php, the inbuilt functions are available for performing sorting operations.They are:

• 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

### Sort Array in Ascending Order – sort()

The code below sorts array elements of the \$test in ascending alphabetical order.

```

";
}
?>

```

Now array elements will go in the alphabetical order. Output will be the following:

Delhi
Kerala

We can sort values by numerical order too. The code below sorts array elements of the \$test in ascending numerical order.

```

";
}
?>

```

Now array elements will go in the numerical order. Output will be the following:

0.01
0.567
19.45465
70

### Sort Array in Descending Order – rsort()

The code below sorts array elements of the \$test in descending alphabetical order.

```

";
}
?>

```

Now array elements will go in the alphabetical order. Output will be the following:

Kerala
Delhi

We can sort values by numerical order too. The code below sorts array elements of the \$test in descending numerical order.

```

";
}
?>

```

Now array elements will go in the numerical order. Output will be the following:

70
19.45465
0.567
0.01

### Sort associative arrays in ascending order, according to the value- asort()

The code below sorts an associative array in ascending order based on values.

```

"39", "BHJHG"=>"37", "AAE"=>"43");
asort(\$test);
foreach(\$test as \$x => \$x_value)
{
echo "Key=" . \$x . ", Value=" . \$x_value;
echo "";
}
?>

```

The output will be the following:

Key=BHJHG, Value=37
Key=ABCS, Value=39
Key=AAE, Value=43

### Sort associative arrays in ascending order, according to the key- ksort()

The code below sorts an associative array in ascending order based on key.

```

"39", "BHJHG"=>"37", "AAE"=>"43");
ksort(\$test);
foreach(\$test as \$x => \$x_value)
{
echo "Key=" . \$x . ", Value=" . \$x_value;
echo "";
}
?>

```

The output will be the following:

Key=AAE, Value=43
Key=ABCS, Value=39
Key=BHJHG, Value=37

### Sort associative array in descending order, according to the value – arsort()

The code below sorts an associative array in descending order based on values.

```

"39", "BHJHG"=>"37", "AAE"=>"43");
arsort(\$test);
foreach(\$test as \$x => \$x_value)
{
echo "Key=" . \$x . ", Value=" . \$x_value;
echo "";
}
?>

```

The output will be the following:

Key=AAE, Value=43
Key=ABCS, Value=39
Key=BHJHG, Value=37

### Sort associative arrays in descending order, according to the key – krsort()

The code below sorts an associative array in descending order based on key.

```

"39", "BHJHG"=>"37", "AAE"=>"43");
krsort(\$test);
foreach(\$test as \$x => \$x_value)
{
echo "Key=" . \$x . ", Value=" . \$x_value;
echo "";
}
?>

```

The output will be the following:

Key=BHJHG, Value=37
Key=ABCS, Value=39
Key=AAE, Value=43

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

## AND and OR Operators – SQL

Hi Guys,
Today we share our idea related to AND and OR operator with example.
The AND and OR operators are the conditional operator in SQL. It is used to filter the data or records based on conditions. The main difference between AND and OR operators are: the AND operator displays a record if both the first condition AND the second condition are true and the OR operator displays a record if either the first condition OR the second condition is true.

### The AND Operator

The AND operator use in SQL statement’s WHERE clause in SELECT query. It allows the existence of multiple conditions in an SQL statement’s WHERE clause.

### Syntax:

The basic syntax of AND operator with WHERE clause is as follows:

SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] AND [condition2]…AND [conditionN];

We can combine N number of conditions using AND operator.The AND operator displays a record if both the first condition AND the second condition are true

Example using AND operator. Consider following Employee table

Employee_id Employee_name salary
1 Appu 9000
2 Ammu 8000
3 Achu 6000
4 Arathy 10000
5 Ashwathi 8000

SQL query selects all Employee from the Employee_name “Ashwathi” AND the salary “8000”, in the “Employee” table:

SELECT * FROM Employee
WHERE Employee_name=’Ashwathi’
AND salary=8000;;

Result of the above query will be,

Employee_id Employee_name salary
5 Ashwathi 8000

### The OR Operator

The OR operator use in SQL statement’s WHERE clause in SELECT query. It allows the existence of multiple conditions in an SQL statement’s WHERE clause.

### Syntax

The basic syntax of OR operator with WHERE clause is as follows:

SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] OR [condition2]…OR [conditionN]

We can combine N number of conditions using OR operator.The OR operatordisplays a record if either the first condition OR the second condition is true.

Example using OR operator. Consider following Employee table

Employee_id Employee_name salary
1 Appu 9000
2 Ammu 8000
3 Achu 6000
4 Arathy 10000
5 Ashwathi 8000

SQL query selects all Employee from the Employee_name “Ashwathi” OR the Employee_name “Achu”, in the “Employee” table:

SELECT * FROM Employee
WHERE Employee_name=’Ashwathi’
OR Employee_name=’Achu’;;

Result of the above query will be,

Employee_id Employee_name salary
3 Achu 6000
5 Ashwathi 8000

### Combining AND & OR

You can also combine AND and OR operator.

Example using Combining AND and OR operator. Consider following Employee table

Employee_id Employee_name salary
1 Appu 9000
2 Ammu 8000
3 Achu 6000
4 Arathy 10000
5 Ashwathi 8000

SQL query selects all Employee from the Employee_name “Ashwathi” AND the salary “8000” or salary ‘10000’ in the “Employee” table:

SELECT * FROM Employee
WHERE Employee_name=’Ashwathi’ AND(salary=8000 OR salary=10000);

Result of the above query will be,

Employee_id Employee_name salary
5 Ashwathi 8000

Example2 using Combining AND and OR operator. Consider following Employee table

Employee_id Employee_name salary
1 Appu 9000
2 Ammu 8000
3 Achu 6000
4 Ashwathi 10000
5 Ashwathi 8000

SQL query selects all Employee from the Employee_name “Ashwathi” OR the Employee “Achu”, in the “Employee” table:

SELECT * FROM Employee
WHERE Employee_name=’Ashwathi’ AND(salary=8000 OR salary=10000);

Result of the above query will be,

Employee_id Employee_name salary
4 Ashwathi 10000
5 Ashwathi 8000

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

## MYSQL Functions

Hi all,
Welcome to MYSQL functions tutorial. MYSQL functions are useful while performing mathematical calculations, string concatenations, sub-strings etc. SQL provides many built in functions to performe these functionality or performing calculations on data.

The most commonly used MySQL functions including:

• Aggregate functions
• String functions
• Date time functions
• control flow functions

• ### Aggregate Functions

The aggregate functions returns the single value after calculating from a group of values or from values in a column.

1) AVG()

The MySQL AVG aggregate function returns the average values. Its general syntax:

SELECT AVG(column_name) from table_name

Example using AVG(). Consider following Employee table

Employee_id Employee_name salary
1 Appu 9000
2 Ammu 8000
3 Achu 6000
4 Arathy 10000
5 Ashwathi 8000

SQL query to find average of salary will be,

SELECT avg(salary) from Employee;

Result of the above query will be,

avg(salary)
8200

2) COUNT()

It is used to count the number of rows in a database table.Its general Syntax is,

SELECT COUNT(column_name) from table-name

Example using COUNT(). Consider following Employee table

Employee_id Employee_name salary
1 Appu 9000
2 Ammu 8000
3 Achu 6000
4 Arathy 10000
5 Ashwathi 8000

SQL query to count employees, satisfying specified condition is,

SELECT COUNT(Employee_namee) from Employee where salary = 8000;

Result of the above query will be,

count(name)
2

3) FIRST()

It is used for returns the first values from selected coloumn.Syntax for FIRST function is,

SELECT FIRST(column_name) from table-name

Example using FIRST(). Consider following Employee table

Employee_id Employee_name salary
1 Appu 9000
2 Ammu 8000
3 Achu 6000
4 Arathy 10000
5 Ashwathi 8000

SQL query

SELECT FIRST(Employee_name) from Employee;

Result will be,

first(Employee_name)
Appu

4) LAST()

It is used for returns the last values from selected coloumn.Syntax for LAST function is,

SELECT LAST(column_name) from table-name

Example using LAST(). Consider following Employee table

Employee_id Employee_name salary
1 Appu 9000
2 Ammu 8000
3 Achu 6000
4 Arathy 10000
5 Ashwathi 8000

SQL query

SELECT LAST(Employee_name) from Employee;

Result will be,

last(Employee_name)
Ashwathi

5) MAX()

It is used for returns the maximum values from selected coloumn.Syntax for LAST function is,

SELECT MAX(column_name) from table-name

Example using MAX(). Consider following Employee table

Employee_id Employee_name salary
1 Appu 9000
2 Ammu 8000
3 Achu 6000
4 Arathy 10000
5 Ashwathi 8000

SQL query

SELECT MAX(salary) from Employee;

Result will be,

MAX(salary)
10000

6) MIN()

It is used for returns the minimum values from selected coloumn. Returns the smallest values. Syntax for LAST function is,

SELECT MIN(column_name) from table-name

Example using MIN(). Consider following Employee table

Employee_id Employee_name salary
1 Appu 9000
2 Ammu 8000
3 Achu 6000
4 Arathy 10000
5 Ashwathi 8000

SQL query

SELECT MIN(salary) from Employee;

Result will be,

MIN(salary)
6000

7) SUM()

It is used for returns thetotal sum values from selected coloumn. Syntax for LAST function is,

SELECT SUM(column_name) from table-name

Example using MIN(). Consider following Employee table

Employee_id Employee_name salary
1 Appu 9000
2 Ammu 8000
3 Achu 6000
4 Arathy 10000
5 Ashwathi 8000

SQL query

SELECT SUM()salary) from Employee;

Result will be,

SUM(salary)
41000

### String Functions

Complete list of MySQL functions required to manipulate strings in MySQL.

1) CONCAT()

This is used to concatenate any string inside any MySQL command. It is used to combine two or more string into one string.The syntax is:

CONCAT(str1,str2,…)

2) LENGTH and CHAR_LENGTH

MySQL string length functions that allow you to get the length of strings measured in bytes and in characters.

3) REPLACE

REPLACE() function allows to replace the old string to new string. The general syntax is

REPLACE(str,old_string,new_string);

4) SUBSTRING

The SUBSTRING function returns a substring from a string starting at a specific position with a given length. The general syntax is:

SUBSTR(string,position);

### Date and Time Functions

1) DATEDIFF

The DATEDIFF() function used for calculate the number of days between two date.The DATEDIFF function accepts two arguments which are two valid DATE. The general syntax is:

DATEDIFF(date_expression_1,date_expression_2)

Example

SELECT DATEDIFF(‘2011-08-17′,’2011-08-17’); — 0 day
SELECT DATEDIFF(‘2011-08-17′,’2011-08-08’); — 9 days
SELECT DATEDIFF(‘2011-08-08′,’2011-08-17’); — -9 days

2) DATE_FORMAT

To format a data value to a specific format.the syntax is:

DATE_FORMAT(date,format);

The following table illustrates the specifiers and their meanings that you can use to construct date format string:

Specifier Meaning
%a Three-characters abbreviated weekday name e.g., Mon, Tue, Wed, etc.
%b Three-characters abbreviated month name e.g., Jan, Feb, Mar, etc.
%c Month in numeric e.g., 1, 2, 3…12
%D Day of the month with English suffix e.g., 0th, 1st, 2nd, etc.
%d Day of the month with leading zero if it is 1 number e.g., 00, 01,02, …31
%e Day of the month without leading zero e.g., 1,2,…31
%f Microseconds in the range of 000000..999999
%H Hour with 24-hour format with leading zero e.g., 00..23
%h Hour with 12-hour format with leading zero e.g., 01, 02…12
%I Same as %h
%i Minutes with leading zero e.g., 00, 01,…59
%j Day of year with leading zero e.g., 001,002,…366
%k Hour in 24-hour format without leading zero e.g., 0,1,2…23
%l Hour in 12-hour format without leading zero e.g., 1,2…12
%M Full month name e.g., January, February,…December
%m Month name with leading zero e.g., 00,01,02,…12
%p AM or PM, depending on other time specifiers
%r Time in 12-hour format hh:mm:ss AM or PM
%S Seconds with leading zero 00,01,…59
%s Same as %S
%T Time in 24-hour format hh:mm:ss
%U Week number with leading zero when the first day of week is Sunday e.g., 00,01,02…53
%u Week number with leading zero when the first day of week is Monday e.g., 00,01,02…53
%V Same as %U; it is used with %X
%v Same as %u; it is used with %x
%W Full name of weekday e.g., Sunday, Monday,…, Saturday
%w Weekday in number (0=Sunday, 1= Monday,etc.)
%X Year for the week in four digits where the first day of the week is Sunday; often used with %V
%x Year for the week, where the first day of the week is Monday, four digits; used with %v
%Y Four digits year e.g., 2000, 2001,…etc.
%y Two digits year e.g., 10,11,12, etc.
%% Add percentage (%) character to the output

The following are some commonly used date format strings:

DATE_FORMAT string Formatted date
%Y-%m-%d 7/4/2013
%e/%c/%Y 4/7/2013
%c/%e/%Y 7/4/2013
%d/%m/%Y 4/7/2013
%m/%d/%Y 7/4/2013
%e/%c/%Y %H:%i 4/7/2013 11:20
%c/%e/%Y %H:%i 7/4/2013 11:20
%d/%m/%Y %H:%i 4/7/2013 11:20
%m/%d/%Y %H:%i 7/4/2013 11:20
%e/%c/%Y %T 4/7/2013 11:20
%c/%e/%Y %T 7/4/2013 11:20
%d/%m/%Y %T 4/7/2013 11:20
%m/%d/%Y %T 7/4/2013 11:20
%a %D %b %Y Thu 4th Jul 2013
%a %D %b %Y %H:%i Thu 4th Jul 2013 11:20
%a %D %b %Y %T Thu 4th Jul 2013 11:20:05
%a %b %e %Y Thu Jul 4 2013
%a %b %e %Y %H:%i Thu Jul 4 2013 11:20
%a %b %e %Y %T Thu Jul 4 2013 11:20:05
%W %D %M %Y Thursday 4th July 2013
%W %D %M %Y %H:%i Thursday 4th July 2013 11:20
%W %D %M %Y %T Thursday 4th July 2013 11:20:05
%l:%i %p %b %e, %Y 7/4/2013 11:20
%M %e, %Y 4-Jul-13
%a, %d %b %Y %T Thu, 04 Jul 2013 11:20:05

3) NOW

Returns current date and time. Syntax is:

SELECT NOW();

### Control flow functions

1) IF

Returns the value based on conditions.Syntax is:

IF(expr,if_true_expr,if_false_expr)

Example

SELECT IF(1 = 2,’true’,’false’); — false

SELECT IF(1 = 1,’ true’,’false’); — true

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

## Difference Between ‘= =’ and ‘= = =’ in php

Hi Guys,
In one of the recent web developer interview, one interviewer ask “what is the difference etween == and === in php”. All are face this same situation in interview panel. Nobody knows the theory part. Then everybody shocked.
Based on this situation, I just describe what are the difference between these two. These are the comparison operators in PHP are = = (equal) and = = =(identical).

The = = operator just checks to see if the left and right values are equal. To check if the values of the two operands are equals or not.

‘ = = ‘ Example:

if(“111” == 111) echo “YES”;
else echo “NO”;

The values of the operands are equal, so the above code will print “YES”.

The = = = operator actually checks to see if the left and right values are equals. And also check to see if they are the same variable type like int or string etc. To check the values as well as the type of the operands.

‘= = =’ Example:

if(“111” === 111) echo “YES”;
else echo “NO”;

The values of both operands are same their types are different, “111” (with quotes) is a string while 111 (w/o quotes) is an integer. So the result we get is “NO”.
Change the above code as:

if(“111” === (string)111) echo “YES”;
else echo “NO”;

We changed the type of right operand to a string which is the same as the left operand (i.e. string). Now, the types and values of both left and right operands are the same hence both operands are identical. Then, the result will be “YES”.

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

## Set Operation in SQL- UNION, UNION ALL, INTERSECT,MINUS operator

Hi Guys,
Welcome to sql set operations. SQL support some set of operations to be performed on MYSQL tadabase table. These are used for getting meaningful results from table, based on different conditions.

### Union

MySQL UNION operator to combine two or more SELECT statements into a single result set. UNION operator helps to select rows one after the other from several tables into a single table. In here it will eliminate duplicate rows from its result set.In case of union, number of columns and datatype must be same in both the tables

### SYNTAX

SELECT column1, column2 from tablename1 UNION
SELECT column1, column2 from tablename2

OR

SELECT * from tablename1 UNION SELECT * from tablename2

The UNION operator selects only distinct values by default

Example of UNION

The Employee1 table

Employee_ID Employee_Name
1 Jose
2 Thomas
3 Joseph

The Employee2 table

Employee_ID Employee_Name
1 Jose
2 Thomas
3 Joseph
4 Mathew

Union SQL query will be:

select * from Employee1
UNION
select * from Employee2

The result table will look like,

Employee_ID Employee_Name
3 Joseph
4 Mathew

In UNION eliminate the duplicate values of data.

### Union ALL

The UNION operator shows only distinct values.But in UNION ALL, it also shows duplicate values.

### SYNTAX

SELECT column_name(s) FROM tablename1
UNION ALL
SELECT column_name(s) FROM tablename2

Example of UNION ALL

The Employee1 table

Employee_ID Employee_Name
1 Jose
2 Thomas
3 Joseph

The Employee2 table

Employee_ID Employee_Name
3 Joseph
4 Mathew

Union ALL SQL query will be:

select * from Employee1
UNION ALL
select * from Employee2

The result table will look like,

Employee_ID Employee_Name
1 Jose
2 Thomas
3 Joseph
3 Joseph
4 Mathew

it shows duplicate values also

### INTERSECT

The INTERSECT display only common values from two tables in SELECT query.

### SYNTAX

select * from Tablename1
INTERSECT
select * from Tablename2

Example of INTERSECT

The Employee1 table

Employee_ID Employee_Name
1 Jose
2 Thomas
3 Joseph

The Employee2 table

Employee_ID Employee_Name
3 Joseph
4 Mathew

INTERSECT SQL query will be:

select * from Employee1
INTERSECT
select * from Employee2

The result table will look like,

Employee_ID Employee_Name
3 Joseph

MYSQL doesn’t support INTESECT operator.

### MINUS

Minus operation combines result of two Select statements and return only those result which belongs to first set of result. MySQL does not support INTERSECT operator.

### SYNTAX

select * from tablename1
MINUS
select * from Tablename2

Example of MINUS

The Employee1 table

Employee_ID Employee_Name
1 Jose
2 Thomas

The Employee2 table

Employee_ID Employee_Name
2 Thomas
4 Mathew

MINUS SQL query will be:

select * from Employee1
MINUS
select * from Employee2

The result table will look like,

Employee_ID Employee_Name
1 Jose

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

## Uses of session_id() in PHP

Hi all,
In one of the recent web developer interview, one of them are ask to me “what are the uses of session_id() in php and what are the purpose.”

Session_id() is one of the inbuilt function in PHP. The session_id() will be used to track existence of same user across all the pages of PHP application. The session_id() will be generated whenever the page is calling session_start()function. In realtime example, “Tracking whether same user is navigating from one page to another.”

```	if(!session_id())
{
session_start();
}
```

This code is needed to check if session is already started.If session is started, no need to initialize it again. Trying to call session_start(), when session is already initialized will create E_NOTICE error.

session_id() is used to get the session id for the current session or set the session id for the current session. Session variables are set with the PHP global variable: \$_SESSION.

session_id() returns the session id for the current session or the empty string (“”). If there is no current session (no current session id exists).

### How can I get session id in php and show it?

```		session_start();
echo session_id();
```

There are two methods are available to use session id and sessions in php

1. Auto generate the session ID and get it:
2. ```		session_start();
\$sessionid = session_id();
```
3. Set the session ID manually and then start it:
4. ```		session_id( 'sessionId' );
session_start();
```

If you want to set the session_id() before calling the session_start();.

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

Category: PHP

Hi all,

• It is jQuery specific event
• It is cross browser compactibility

• It’sthe best for onetime initialization.
• \$(document).ready() is not called each and every partial postback of update panel
• code written in \$(document).ready() method will not be initialized each and every partial postback.
• Unable to re-attach the functionality to elements/controls of the page affected by partial postbacks.
• The ready() method specifies what happens when a ready event occurs

SYNTAX

OR

```
});

```

The ready() method can only be used on the current document, so no selector is required:

\$(function)

OR

OR

Example

This is a paragraph.

### Code

```

\$("button").click(function(){
\$("#testing").slideToggle();
});
});

This is a paragraph.

```

When images and all associated resources of the page have been fully loaded, the pageLoad() gets called.

• The \$(window).load() function executes when complete page is fully loaded, including all frames, objects and images.
• Suppose your web page has large image size then until all the images are not fully loaded on the page, here the pageLoad() method will not called
• This is not browser compactibility. Only call on web pages.

SYNTAX

Simple Code

```

(Type a title for your page here)

function my_code(){
}

```

### SUMMARY

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

## Retrieve the unique rows without using the DISTINCT keyword

Good Morning to all,
We know the uses of DISTINCT keyword in SQL. Here I share some usesful information relates to “Retrieve the unique rows without using the DISTINCT keyword.”
The DISTINCT keyword is used to return only distinct or different values. In table, the column may contain many duplicate values, and if you want to display the list the different values by using DISTINCT keyword in select query.

### SQL SELECT DISTINCT Syntax

SELECT DISTINCT column_name,column_name FROM table_name;

Example:Employee table

EmployeeID employee_name
1 Thomas
2 Varghese
3 Jose
4 Varghese

SELECT DISTINCT employee_name FROM Employee;

employee_name
Thomas
Varghese
Jose

### How would you retrieve the unique values for the employee_name without using the DISTINCT keyword

GROUP BY keyword helps to retrieve the unique values in sql

SELECT employee_name from Employee GROUP BY employee_name

### Running this query will return the following results:

employee_name
Thomas
Varghese
Jose

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

## Concatenate two variables or strings – PHP

Hi Friends,
The DOT(.) operator is used to concatenate two variable or string in PHP. The ‘.’ operator which returns the concatenation of its right and left string.
eg:

```
```

And also ‘.=’ operator used append the arguments on the right side to the left side arguments.
eg:

```
```

Example 1

```
';
echo "hello". "World";
echo '';  // Output: helloworld
\$a1 = "Hello ";
\$a1 .= "World!";     // now \$a1 contains "Hello World!"
echo \$a1;
echo "hi". "welcome";
?>
```

Example 2

``` ';
echo'this is ', \$a ,' and ', \$b;  //output: this is hello and world
echo "";
echo"this is \$a and \$b";           // Output:this is hello and world
?>
```

COMMA is not concatenating it just used to echo one by one and in that way complete string appears together.In javascript PLUS(+) is used as concatenation operator.

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.