PHP Arrays

An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.
There are three different kind of arrays and each array value is accessed using an ID c which is called array index.
  • Numeric array - An array with a numeric index. Values are stored and accessed in linear fashion
  • Associative array - An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
  • Multidimensional array - An array containing one or more arrays and values are accessed using multiple indices

NOTE: Built-in array functions is given in function reference PHP Array Functions

Numeric Array
These arrays can store numbers, strings and any object but their index will be prepresented by numbers. By default array index starts from zero.
Example
Following is the example showing how to create and access numeric arrays.
Here we have used array() function to create array. This function is explained in function reference.
<html>
<body>
<?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value )
{
  echo "Value is $value <br />";
}
/* Second method to create array. */
$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
$numbers[3] = "four";
$numbers[4] = "five";

foreach( $numbers as $value )
{
  echo "Value is $value <br />";
}
?>
</body>
</html>
This will produce following result:
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Value is one
Value is two
Value is three
Value is four
Value is five
Associative Arrays
The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.
To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.
NOTE: Don't keep associative array inside double quote while printing otheriwse it would not return any value.
Example
<html>
<body>
<?php
/* First method to associate create array. */
$salaries = array(
                  "mohammad" => 2000,
                  "qadir" => 1000,
                  "zara" => 500
                 );

echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
echo "Salary of zara is ".  $salaries['zara']. "<br />";

/* Second method to create array. */
$salaries['mohammad'] = "high";
$salaries['qadir'] = "medium";
$salaries['zara'] = "low";

echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
echo "Salary of zara is ".  $salaries['zara']. "<br />";
?>
</body>
</html>
This will produce following result:
Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low
Multidimensional Arrays
A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.
Example
In this example we create a two dimensional array to store marks of three students in three subjects:
This example is an associative array, you can create numeric array in the same fashion.
<html>
<body>
<?php
   $marks = array(
               "mohammad" => array
               (
               "physics" => 35,          
               "maths" => 30,    
               "chemistry" => 39         
               ),
               "qadir" => array
                (
                "physics" => 30,
                "maths" => 32,
                "chemistry" => 29
                ),
                "zara" => array
                (
                "physics" => 31,
                "maths" => 22,
                "chemistry" => 39
                )
             );
   /* Accessing multi-dimensional array values */
   echo "Marks for mohammad in physics : " ;
   echo $marks['mohammad']['physics'] . "<br />";
   echo "Marks for qadir in maths : ";
   echo $marks['qadir']['maths'] . "<br />";
   echo "Marks for zara in chemistry : " ;
   echo $marks['zara']['chemistry'] . "<br />";
?>
</body>
</html>
This will produce following result:
Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39

PHP for loop

PHP for loop executes repeatedly same set of instructions until a termination condition is met. Sometimes we need to work with same types of code hundred to thousands time. In that case we need to write same type of code thousands time. It is a problem. To solve this problem PHP introduce for loop where we set just upper and lower condition.

PHP for loop structure: 


Here is the general form of the for statement

for(initialization; condition; iteration)
{
// Body
}


The for loop operates as follows: 
Initialization: PHP for loop starts with initialization and initialization parts is only executed one time.
Condition: It returns true or false. Condition part test the loop. If it returns true then the body of the loop is executed otherwise it exits from the loop.
Iteration: It increment or decrement the counter value (We a assume counter value which control the loop). First the loop test the condition and then execute the body and then execute the iteration and it repeats until the condition is false.

PHP for loop example: 

Now lets try an example with PHP for loop. Suppose, we want to add each value from 1 to 100. So the loop starts with 1 and ends when the value is 100.
<?php

$sum=0; 
for($count=1; $count<=100; $count++)
{
$sum=$sum+$count;
}
echo $sum;
?>

PHP nested for loop:

Nested means we put a loop inside another loop. Try to understand the following code which describe the PHP nested loop.
<?php
for($count=1; $count<10; $count++){
    for($x=$count; $x<10;$x++){
    echo "#";
    }
    echo "<br>";
}
?>
Output of this code is:
#########
########
#######
######
#####
####
###
##
#

PHP Web Application Development India | Custom Web Development India

We are an offshore web development firm in India offeringWe are a Outsourcing Company based in India. Basically we are developing online web applications like Custom Web Site Design, Outsource Web Development, Offshore Outsourcing, Offshore website Design, Offshore Web Development, PHP Programming Development, ASP.Net Web Development, AJAX Programming, Flash Web Page Design, ASP.Net application development, PhpMySQL Development, ecommerce web site, shopping cart development in PHP, ASP, ASP.Net, AJAX etc.

We deliver cost effective interactive web interface technology and web site development solutions without compromising in its quality. We have a team of high profession, qualified and well experienced web developer and website designers. We design, build and maintain high quality web site design and ecommerce web development solutions.We provides professional-level programmer and customized web development solutions in PHP, ASP, ASP.Net, AJAX etc. to its clients.

Web Development India

Our web developers in India are specialized in building dynamic web sites and high end ecommerce web site application development using ASP.Net, PHP, ASP, AJAX etc. Our customized Web site Design solution enabling company's mission to provide customers with stable and reliable web site development service as a step ahead in this regards. Our company has a group of highly qualified and talented young dot net programmer, asp developer, php programmer, AJAX programmer.We outsource php developer, ajax programmer and asp.net developer on monthly basis and we work closely with the clients and make sure the profitability of each web site developed for them. Our team of ASP Programmer, asp.net (asp dot net) Developer, AJAX Programmer and php programmer has proven experience in the field of high end web programming / web site development and can provide bug free, affordable and cost-effective website design solutions in addition to complex database integrated web site development, e-commerce portals.



For further queries please contact at:webdeveloper04@gmail.com

PHP Constants



What is Operator?Simple answer can be given using expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called operator. PHP language supports following type of operators.
  • Arithmetic Operators
  • Comparision Operators
  • Logical (or Relational) Operators
  • Assignment Operators
  • Conditional (or ternary) Operators
Lets have a look on all operators one by one.
Arithmetic Operators:
There are following arithmetic operators supported by PHP language:
Assume variable A holds 10 and variable B holds 20 then:
Operator
Description
Example
+
Adds two operands
A + B will give 30
-
Subtracts second operand from the first
A - B will give -10
*
Multiply both operands
A * B will give 200
/
Divide numerator by denumerator
B / A will give 2
%
Modulus Operator and remainder of after an integer division
B % A will give 0
++
Increment operator, increases integer value by one
A++ will give 11
--
Decrement operator, decreases integer value by one
A-- will give 9
Comparison Operators:
There are following comparison operators supported by PHP language
Assume variable A holds 10 and variable B holds 20 then:
Operator
Description
Example
==
Checks if the value of two operands are equal or not, if yes then condition becomes true.
(A == B) is not true.
!=
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.
(A != B) is true.
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
(A > B) is not true.
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
(A < B) is true.
>=
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
(A >= B) is not true.
<=
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
(A <= B) is true.
Logical Operators:
There are following logical operators supported by PHP language
Assume variable A holds 10 and variable B holds 20 then:
Operator
Description
Example
and
Called Logical AND operator. If both the operands are true then then condition becomes true.
(A and B) is true.
or
Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true.
(A or B) is true.
&&
Called Logical AND operator. If both the operands are non zero then then condition becomes true.
(A && B) is true.
||
Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true.
(A || B) is true.
!
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
!(A && B) is false.
Assignment Operators:
There are following assignment operators supported by PHP language:
Operator
Description
Example
=
Simple assignment operator, Assigns values from right side operands to left side operand
C = A + B will assigne value of A + B into C
+=
Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
C += A is equivalent to C = C + A
-=
Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand
C -= A is equivalent to C = C - A
*=
Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand
C *= A is equivalent to C = C * A
/=
Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand
C /= A is equivalent to C = C / A
%=
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand
C %= A is equivalent to C = C % A
Conditional Operator
There is one more operator called conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation. The conditional operator has this syntax:
Operator
Description
Example
? :
Conditional Expression
If Condition is true ? Then value X : Otherwise value Y
Operators Categories:
All the operators we have discussed above can be categorised into following categories:
  • Unary prefix operators, which precede a single operand.
  • Binary operators, which take two operands and perform a variety of arithmetic and logical operations.
  • The conditional operator (a ternary operator), which takes three operands and evaluates either the second or third expression, depending on the evaluation of the first expression.
  • Assignment operators, which assign a value to a variable.
Precedence of PHP Operators:
Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:
For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher precedence than + so it first get multiplied with 3*2 and then adds into 7.
Here operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Category 
Operator 
Associativity 
Unary 
! ++ -- 
Right to left 
Multiplicative  
* / % 
Left to right 
Additive  
+ - 
Left to right 
Relational  
< <= > >= 
Left to right 
Equality  
== != 
Left to right 
Logical AND 
&& 
Left to right 
Logical OR 
|| 
Left to right 
Conditional 
?: 
Right to left 
Assignment 
= += -= *= /= %=
Right to left 

Cerating Firefox Extension

Keen about Developing/building firefox extensions?.Well ! It's easy to develop extensions for the firefox.You need to have understanding of XUL and Javascript.
Basically, this POST will introduce you how to create your first firefox extension.

Extensions are packaged and distributed in ZIP files, with the XPI (pronounced “zippy”) file extension.
The basic structure of a XPI package is as follows.

emptySidebar.xpi:
/install.rdf
/install.js
/chrome
|-
emptySidebar.jar

Folder Structure is as follows.
emptySideBar
/install.rdf
/install.js
/chrome
|- content
| |-login.xul
| |-helloworldOverlay.js
| |-content.rdf
|- skin
|- locale

A folder and two files. install.js was all you used to need for the installation, but now that the extension manager has changed (since Firefox 0.9), the install.rdf is used instead.

Chrome foleder contains
another archived file, emptyBar.jar.which has three folder.
content - contains all XUL and related ,JavaScript files and content.rdf file.
skin - contains all layout files like CSS and images used in the XUL files.
locale - contains language specific information.

Once this is done, you are ready to create launcher for your extension.
Step1
go to emptySideBar/chrome/ directory.
zip -r emptySideBar.jar content/ skin/ /locale
cd ..
zip emptySideBar.xpi install.* chrome/captain.jar


Once xpi is created upload it to you server and call it from the firefox mozila.

For more information click here






Security

The proportion of insecure software written in PHP, out of the total of all common software vulnerabilities, amounted to: 12% in 2003, 20% in 2004, 28% in 2005, 43% in 2006, 36% in 2007, and 33.8% for the first quarter of 2008. More than a third of these PHP software vulnerabilities are listed recently. Most of these software vulnerabilities can be exploited remotely, that is without being logged on the computer hosting the vulnerable application. The most common vulnerabilities are caused by not following best practice programming rules and vulnerabilities related to software written in old PHP versions. One very common security concern is register_globals which was disabled by default since 2002 in PHP 4.2.

There are advanced protection patches such as Suhosin and Hardening-Patch, especially designed for web hosting environments. Installing PHP as a CGI binary rather than as an Apache module is the preferred method for added security.

Get the visitor IP anyhow

<?
function SafIP() {
if (
getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}else if (
getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}else if (
getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}else if (
getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}else if (
getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return
$ip;
}
?>