Saturday, October 6, 2012

foreach loop in php example

foreach loop in php 

 

Syntax

foreach (array as value)
{
    code to be executed;

}
 
 
<?php
 
 
 $arr = array( 10, 20, 3, 40, 150);
foreach( $arr as $value )
{
  echo " $value <br />";
} 


//


$lang = array('php','java','c','c#','.net','perl');
echo "Your output here <br>"; 
 foreach( $lang as $val )
{
  echo " $val <br />";
} 

Do while loop in php

// Do While loop in php

Syntax

do
{
   code to be executed;
}while (condition); 
 
 
<?php
//print 1 to 10 number
$a = 1; 
do
{ 
echo "<br>".$a; 
 $a++; 
}while( $a < 10 );

?>
 

While loop in php examples



//  How to use while loop in php.


Syntax

while (condition)
{
    code to be executed;
}
 
 
<?php
// print  1 to 5  
$a=1;
while ($a<=5)
{
echo "\t ".$a;
$a=$a+1;
 
} 
 
?> 

Type of loop in php

There is mainly four type of loops.
Loops  are used to execute the same block of code a specified number of times in php.
 
1. for loop.
2. while loop.
3. Do-while loop.
3. foreach loop.


Syntax


for (initialization; condition; increment)
{
  code to be executed;
}


<?php 
// print  1 to 10 number 
for( $i=0; $i<10; $i++ )
{
echo "<br>".$i;
 
}
///////////////////
 
 
//Sum of ten number 
 
 
$sum = 0;
for( $i=0; $i<10; $i++ )
{
echo "<br>".$i;
$sum=$sum+$i;
}

echo " Total of 10 number =".$sum; 
 
?> 



Saturday, August 11, 2012

Remove whitespace from both sides of a string with php

//Remove whitespace from both sides of a string in php

function removeSideSpace($str)
{

return trim($str);

}

$yourString="  This  is test for the string    ";

echo " <br> Before remove the space == ".strlen($yourString);

$yourString= removeSideSpace($yourstring);

echo " <br> After remove the space == ".strlen($yourString);


?>

How to Redirect any page with php

<?php

//how  to Redirect any page with php




function  reDirect2Url($pageUrl) //  Write the function code Here
{
    if ($pageUrl!="")
    {
        header("location: $pageUrl");
    }
}



reDirect2Url("yourpageurlhere");   // called the function

?>

Tuesday, July 3, 2012

Find if url(current address) contains "?" or not with php

//  how to find the current url contains  the "?" or not
// to verify the current url has the one/single query string variable or many  string variable
function  get_urlsigntype($current_url) {
    if(strpos($current_url, '?') === false) {
        return "?";
    } else {
        return "&";
    }
}

Monday, July 2, 2012

How to get client's IP address with PHP

<?php
//  get the  client ip address and store into the variable.

$client_ipaddress = $_SERVER["REMOTE_ADDR"];
echo "Client IP is :".$client_ipaddress;
?>

Get Current file Name in php

<?php

//  get the current file name
$currentFile_Name = $_SERVER["SCRIPT_NAME"];
echo basename($currentFile_Name); 
 
?>

Comments in PHP

1. Single Line Comment

<?php
 //here is any code
//echo "this  is test";

?>



2. Multiple Line Comment

<?php
/* Here is your  code 
 
echo " this is test..."; 
echo "this is not display at screen";
*/
?>

Php tags

1.  php short tag  


<? = ?>

2. php full tag


<?php
// write your code here

?>

Thursday, June 28, 2012

PHP: Hypertext Preprocessor

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. The php  code is interpreted by a Web server with a PHP processor module which generates the resulting Web page.