Friday, February 25, 2011

Mail id verification

function chkmailid($email){
   if (ereg("^.+@.+\\..+$", $email)) {
      echo ("E-mail address is valid.");
   }else{
      echo ("Invalid e-mail address.");
   }
}

Thursday, February 24, 2011

Looping through an Array


One of the most useful features of arrays is that it is possible to loop through them and repeatedly perform processes on the individual elements.


Sequentially Indexed Arrays
The simplest way to loop through array is to use count() to determine the number of elements in an array, and then construct a for() loop:
$countries = array ("cr", "de", "us");
$num_elements = count ($countries);
// $num_elements now has value of 3
for ($idx = 0; $idx < $num_elements; ++$idx) {
   // Print each element on its own line:
   echo ("$countries[$idx] <BR>\n");
}


Non-Sequentially Indexed Arrays
An array has a built in pointer (or iterator). This internal cursor keeps track of which element currently is in focus. For a newly created array, the pointer is on the first element. We can determine the value of the current element using the current() function, and the current element's index using the key() function. To illustrate these, the following code initializes an array and prints the value and index of the current element:

$countries[50] = "cr";
$countries[20] = "de";
$countries[10] = "us";
$countries[] = "uk";
// Has index '51'
$key = key ($countries);
$value = current ($countries);
echo ("Element $key equals $value");

Because the array has just been created, current element is the first, so this code will print Element 50 equals cr. Note that "cr" is the first element in this array, even though "de" and "us" have lower indices, because it was the first element to be assigned to the array. Arrays are not ordered unless we specifically call a function to sort the array; we will see how to do this later.

The two functions each() and list() can be used together to loop through an array, even if the indices are non sequential (or even if they are not numbers at all; non-numeric keys will be covered in the section, 'String Indexed Arrays'). An example of a loop that successfully navigates an array whether or not it is sequentially indexed is demonstrated below:

reset ($countries);
while (list ($key, $value) = each ($countries)) {
echo "Element $key equals $value<BR>\n";
}

In the section on 'String-Indexed Arrays', we'll examine more closely how this works. For now, think of the line:

while (list ($key, $value) = each ($countries)) {

to mean "for each element in the array, set $key equal to the element's key (or index), and $value equal to the value of the element." (Some languages handle this with a for each construct.) The reset() function sets the internal pointer to the first element. Naturally, this isn't needed if the pointer is already on the first element; but it can be a useful habit to include reset() before traversing the array, just to make sure we start at the beginning. The each() function moves the array pointer one element forward every time it is called, so if our loop were nested, reset() would be needed to restore the pointer to the first element (assuming we want to start at the beginning, of course). Using our previously assigned $countries array, the output of the above code would be:

Element 50 equals cr
Element 20 equals de
Element 10 equals us
Element 51 equals uk

Going back


<INPUT TYPE="button" VALUE="Go Back" onClick="self.history.back();">

Try it  

Note : Come here by changing some page. Back button must be enabled 

Useful function for variable

gettype()
   gettype() determines the variable datatype and returns it. e.g. integer, float, string
if(gettype ($user_input) == "integer"){
   echo $user_input;
}


settype()
   settype() explicitly set the variable datatype. If datatype could not be set then a false value returned.
if(settype ($user_input, "integer" ){
   echo "Variable conversion successful";
}
else{
   echo "Variable conversion failed";
}


isset() and unset()
   unset() is used to destroy a variable, freeing all the memory that is associated with the variable so it is then available again. The isset() function is used to determine whether a variable has been given a value. If a value has been set then it returns true.


empty()
   empty() is nearly the opposite of isset(). It returns true if the variable is not set, or has a value of zero or an empty string. Otherwise it returns false.

echo empty($new); // true

$new = 1;
echo empty($new); // false

$new = "";
echo empty($new); // true

$new = 0;
echo empty($new); // true

$new = "Buon giorno";
echo empty($new); // false

unset ($new);
echo empty($new); // true


The is...() functions
   The functions is_int(), is_integer(), and is_long() are all synonymous functions that determine whether a variable is an integer.
   is_double(), is_float(), and is_real() determine whether a variable is a double.
   is_string(), is_array(), and is_object() work similarly for their respective data types.
$ProductID = "432BB";
if (is_string ($ProductID)) {
   echo ("String");
}


The ...val() functions
PHP provides yet another way to explicitly set the data type of a variable: the intval(), doubleval(), and strval() functions. These functions cannot be used to convert arrays or objects.

Using foreach to walk through an array

foreach walks through the array one value at a time. The current key and value of the array can be used in the block of statements each time the block executes. The general format is

foreach( $arrayname as $keyname => $valuename)
{
     block of statements;
}

For instance, the following foreach statement walks through the sample
array of state capitals and echoes a list:

$capitals = array(“CA” => “Sacramento”, “TX” => “Austin”,“OR” => “Salem” );
ksort($capitals);

foreach( $capitals as $state => $city )
{
   echo “$city, $state”;
}


On two dimensional array...


echo “”;
 foreach( $productPrices as $category )
{
   foreach( $category as $product =&gt; $price )
  {
       $f_price = sprintf(“%01.2f”, $price);
       echo “”;
   }
}
echo “<table border="1"><tbody><tr><td>$product:</td> <td>\$$f_price</td></tr></tbody></table>”;