Search This Blog

checking if an element is in array or not using in_array() in php

element_in_array.php :

<?php
// Checking if an Element Is in Array or not

$num=array(1,2,3,4,5,6,7,8,9,10);

$element=$_POST['element'];

if (in_array($element, $num)) {
echo 'element found';
}
else
{
echo 'element not found';
}


?>

============================================================
element_in_array_index.php :

<center><b>here checks data is in array or not</b><br><br>
<center><form action="element_in_array.php" method="POST">
<input type="textbox" name="element">
<input type="submit" name="submit">
</form>

============================================================
input :

1) give the array values in text box

2)  click in button

ex :

array :$num=array(1,2,3,4,5,6,7,8,9,10);

here 0-9 array indexes
and
1-10 array values
 
============================================================
output :

1) element found

2) element not found

check array index or key with user inputs in php

check_array_key.php :

<center>
<?php

$num1=array(1,2,3,4,5,6,7,8,9,10,'a'=>'A');
$key=@$_POST['key'];

if (array_key_exists($key, $num1))
{
echo 'array index/key :<b> '.$key.' </b> found<br>';
}
else
{
echo 'array index/key :<b> '.$key.' </b> not found<br>';
}

?>
==========================================================

check_array_index.php :

<center><b>here checks index is in array or not</b><br><br>
<form action="check_array_key.php" method="POST">
<input type="textbox" name="key">
<input type="submit" name="submit">
</form>

============================================================
input :

1) give the array index value in text box
2) click on button

ex :

$num1=array(1,2,3,4,5,6,7,8,9,10,'a'=>'A');

here "0-9" and "a" are  indexes

"1-10" and "A" are values

============================================================

output :

array index/key : not found

array index/key : 1 found

delete array elements with unset() in php

<?php

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>array before delete elements :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}
echo '<br><br>';

//delete array element

unset($num[3]);
unset($num[0],$num[1],$num[2]);
echo '<b>array after delete elements (1,2,3,4) :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}


?>

output :

array before delete elements : 1 2 3 4 5 6 7 8 9 10

array after delete elements (1,2,3,4) : 5 6 7 8 9 10

change array size using array_pad() in php

<?php

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>array before grow elements :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}
echo '<br><br>';

$num = array_pad($num, 20,'a') ; //only additionally adding elements have 'a' value

// $num = array_pad($num, 20,'') ; additionally adding elements have empty

echo '<b>array after grow elements :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}


?>


output :

array before grow elements : 1 2 3 4 5 6 7 8 9 10

array after grow elements : 1 2 3 4 5 6 7 8 9 10 a a a a a a a a a a

 note :

1) just modify  " $num = array_pad($num, 20,'a') ;  " as "  $num = array_pad($num, 20,'') ; "  for empty
2) that means array index raise but with empty values

array to string in php

<?php

$num1=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>the array :</b>';
foreach($num1 as $num)
{
echo ' '.$num;
}


$string = join(',', $num1);
echo '<br><b>the string :</b>'.$string;

?>

output :

the array : 1 2 3 4 5 6 7 8 9 10

the string :1,2,3,4,5,6,7,8,9,10

array reduce with array_splice() in php

<?php

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>original array :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 1 element :</b>';
$num = array_splice($num, 1) ;
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 2 elements :</b>';
$num = array_splice($num, 2) ;

foreach($num as $num1)
{
echo ' '.$num1;
}

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 3 elements :</b>';
$num = array_splice($num, 3) ;
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 4 elements :</b>';
$num = array_splice($num, 4) ;

foreach($num as $num1)
{
echo ' '.$num1;
}

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 5 elements :</b>';
$num = array_splice($num, 5) ;
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 6 elements :</b>';
$num = array_splice($num, 6) ;

foreach($num as $num1)
{
echo ' '.$num1;
}

?>

output :

original array : 1 2 3 4 5 6 7 8 9 10

array after reduce 1 element : 2 3 4 5 6 7 8 9 10

array after reduce 2 elements : 3 4 5 6 7 8 9 10

array after reduce 3 elements : 4 5 6 7 8 9 10

array after reduce 4 elements : 5 6 7 8 9 10

array after reduce 5 elements : 6 7 8 9 10

array after reduce 6 elements : 7 8 9 10

add one arry to another array or append array in php

<?php
echo '<b>the first array :</b>';
$num1=array(1,2,3,4,5,6,7,8,9,10);
foreach($num1 as $num)
{
echo ' '.$num;
}
echo '<br><b>the second array :</b>';
$num2=array(11,12,13,14,15,16,17,18,19,20);
foreach($num2 as $num)
{
echo ' '.$num;
}
echo '<br><br><b>append the first array to the second array : </b>';

$num3 = array_merge($num1, $num2);

foreach($num3 as $num)
{
echo ' '.$num;
}

?>

output :

the first array : 1 2 3 4 5 6 7 8 9 10
the second array : 11 12 13 14 15 16 17 18 19 20

append the first array to the second array : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

roll dice in php

roll_dice.php :

<center><?php
$rand=rand(1,6);
echo "dice one : $rand".'<br>';
$rand1=rand(1,6);
echo "dice two : $rand1";

?>
<form action="roll_dice.php" method="POST">
<input type="submit" value="roll dice">
</form>


output :

dice one : 3
dice two : 3

dice one : 3
dice two : 5

dice one : 4
dice two : 2

dice one : 4
dice two : 3

dice one : 1
dice two : 5

note :
for basics : rand()

session_destroy() or delete or destroy all sessions in php

<?php

session_start();

session_destroy();


?>

output :

note :
1) no display here
2) but it deletes all sessions

unset or delete or destroy session in php

<?php
session_start();
unset($_SESSION['views']);
?>

output :


note :
1) it deletes "views" session
2) if you already stored   value "views" then it deletes "views" otherwise it raises an error

$_SESSION or session displays in php

<?php
session_start();

echo "Pageviews=". $_SESSION['views'];

?>

output :
 Pageviews=2

note :
1) if already the "views" value store 2 then it displays the value "2"

$_SESSION or sessions in php

<?php
//The session_start() function must put before <html> tag
session_start();

// store session value
$_SESSION['views']=2;

?>

output :

note :
1) no displays here, but stores some value

2) "views" value is now 2

delete cookies in php

<?php

// SET THE expiration time to one hour ago

setcookie("visitor", "", time()-3600);
?>

output :

it deletes "visitor" cookie , here no message displays for security

display cookies in php

<?php

// already user , visitor cookies set

// to Print cookies
echo $_COOKIE["user"].'<br>'.$_COOKIE["visitor"];

?>

output :

here displays already set cookies like "user" , "visitor"

set Cookies in php

<?php
/*

symtax:

setcookie(name, value, expire, path, domain);

*/


//it expires after 5 seconds
setcookie("user", "human", time()+5);
//it expires after 60 sec or 1 min
setcookie("user", "human", time()+60);
//it expires after 3600 sec or 1 hour
setcookie("user", "human", time()+3600);


//expire using varable
//it expires after 10 seconds
$time=10;
setcookie("visitor", "humans", time()+$time);

// note : normally "echo" not use in "setcookies" file
?>

output :


note :

1)no output display here
2)but cookies set

search data in table with user inputs in php with mysql

connection.inc.php :

<?php
$host='localhost';
$user_name='root';
$password='';
@$con = mysql_connect("$host","$user_name","$password");
if ($con)
 {}
else
  {
  die('Could not connect ');
  }
?>
=======================================================================

select_form.php :


<center><form action="select_table.php" method="POST">
<table border=0><tr><td>database name</td><td><input type="text" name="db_name" value="test"></td>
<td>table name</td><td><input type="text" name="table_name" value="test"></td></tr>
<tr><td>column1 name</td><td><input type="text" name="col1" value="sno"></td>
<td>column2 name</td><td><input type="text" name="col2" value="name"></td></tr>
<tr><td colspan=4 align="center"><input type="submit" value="submit"> </td></tr></table>
</form>

===================================================================

select_table.php :


<?php

require 'connection.inc.php';

if (@mysql_select_db($_POST['db_name']))
{
     if(isset($_POST['db_name'])&& isset($_POST['table_name'])&& isset($_POST['col1'])&& isset($_POST['col2']))
        {
         if(!empty($_POST['db_name'])&& !empty($_POST['table_name'])&& !empty($_POST['col1'])&& !empty($_POST['col2']))
             {
               $db=$_POST['db_name'];
               $tn=$_POST['table_name'];
               $snum=$_POST['col1'];
               $name=$_POST['col2'];
               $result=mysql_query("select * from $tn ",$con);

                          if(mysql_query("select * from $tn ",$con))
                          {
                              if(@mysql_num_rows($result))
                                {
                               echo ("<br><center><table><tr><th><b>$snum</td><th><b>$name</td></tr>");
                               if(mysql_query("select $snum from $tn") && mysql_query("select $name from $tn"))
                               {
                               while($data = mysql_fetch_array($result))
                                     {
                                      echo "<tr><td>".@$data[$snum] . "</td><td>" .@$data[$name]."</td></tr>";
                                     }
                                    }else {
                                            echo '<b>please verify column name(s)</b>';
                                          }

                         }   else echo '<center><b>empty table';
                  }  else echo ('<center><br><br><b>no table with that name<br><br>');
              }
           else echo '<center>please varify is all data entered ?';
  } else echo '<center>one of the input ont set';
}else echo '<center><br>wrong database selected<br>';

mysql_close($con);
?>
=======================================================================

run " select_form.php "

output :

fill every text box and click on submit button

where in php with mysql

connection.inc.php :

<?php
$host='localhost';
$user_name='root';
$password='';
@$con = mysql_connect("$host","$user_name","$password");
if ($con)
 {echo '<br>host coonected<br>';}
else
  {
  die('Could not connect ');
  }
?>

where_table.php :
<?php

require 'connection.inc.php';

if (mysql_select_db('test'))
if($result=mysql_query("select * from test where sno=2",$con))
  {
     echo "<br><table><tr><td>sno</td><td>name</td></tr>";
    while($data = mysql_fetch_array($result))
  {
  echo "<tr><td>".$data['sno'] . "</td><td>" .$data['name']."</td></tr>";
  }
  echo "</table><br>data selected<br>";
  }
else
  {
  echo '<br>data not selected :'.mysql_error().'<br>';
  }

mysql_close($con);
?>

run " where_table.php "


output :



host coonected

snoname
2human
2human

data selected

update in php with mysql

connection.inc.php :

<?php
$host='localhost';
$user_name='root';
$password='';
@$con = mysql_connect("$host","$user_name","$password");
if ($con)
 {echo '<br>host coonected<br>';}
else
  {
  die('Could not connect ');
  }
?>

update_table.php :

<?php

require 'connection.inc.php';

if (mysql_select_db('test'))
if(mysql_query("update test set name='humans'where sno=1",$con))
  {
    $result=mysql_query("select * from test where name='humans'",$con);
     echo "<br><table><tr><td>sno</td><td>name</td></tr>";
    while($data = mysql_fetch_array($result))
  {
  echo "<tr><td>".$data['sno'] . "</td><td>" .$data['name']."</td></tr>";
  }
  echo "</table><br>data updated<br>";
  }
else
  {
  echo '<br>data not updated :'.mysql_error().'<br>';
  }

mysql_close($con);
?>

run " update_table.php "

output:


host coonected

snoname

data updated

select in php with mysql

connection.inc.php :

<?php
$host='localhost';
$user_name='root';
$password='';
@$con = mysql_connect("$host","$user_name","$password");
if ($con)
 {echo '<br>host coonected<br>';}
else
  {
  die('Could not connect ');
  }
?>

select_table.php :
<?php

require 'connection.inc.php';

if (mysql_select_db('test'))
if($result=mysql_query("select * from test ",$con))
  {
     echo "<br><table><tr><td>sno</td><td>name</td></tr>";
    while($data = mysql_fetch_array($result))
  {
  echo "<tr><td>".$data['sno'] . "</td><td>" .$data['name']."</td></tr>";
  }
  echo "</table><br>data selected<br>";
  }
else
  {
  echo '<br>data not selected :'.mysql_error().'<br>';
  }

mysql_close($con);
?>

run "select_table.php "

output :


host coonected

snoname
2human
2human

data selected

order by in php with mysql

connection.inc.php :

<?php
$host='localhost';
$user_name='root';
$password='';
@$con = mysql_connect("$host","$user_name","$password");
if ($con)
 {echo '<br>host coonected<br>';}
else
  {
  die('Could not connect ');
  }
?>

order_by_table.php :

<?php

require 'connection.inc.php';

if (mysql_select_db('test'))
if($result=mysql_query("select * from test order by name",$con))
  {
     echo "<br><table><tr><td>sno</td><td>name</td></tr>";
    while($data = mysql_fetch_array($result))
  {
  echo "<tr><td>".$data['sno'] . "</td><td>" .$data['name']."</td></tr>";
  }
  echo "</table><br>data selected<br>";
  }
else
  {
  echo '<br>data not selected :'.mysql_error().'<br>';
  }

mysql_close($con);
?>

run " order_by_table.php "

output :


host coonected

snoname
2human
2human

data selected


insert data into table in php with mysql

connection.inc.php :
<?php
$host='localhost';
$user_name='root';
$password='';
@$con = mysql_connect("$host","$user_name","$password");
if ($con)
 {echo '<br>host coonected<br>';}
else
  {
  die('Could not connect ');
  }
?>



insert_table.php :
<?php

require 'connection.inc.php';

if (mysql_select_db('test'))
echo '<br>database selected<br>';
if(mysql_query("insert into test(sno,name)values (2,'human')",$con))
  {
  echo "<br>data inserted<br>";
  }
else
  {
  echo '<br>data not inserted :'.mysql_error().'<br>';
  }

mysql_close($con);
?>

run " insert_table.php "

output :


host coonected

database selected

data inserted

delete table in php with mysql

connection.inc.php :

<?php
$host='localhost';
$user_name='root';
$password='';
@$con = mysql_connect("$host","$user_name","$password");
if ($con)
 {echo '<br>host coonected<br>';}
else
  {
  die('Could not connect ');
  }
?>

del_table.php :

<?php

require 'connection.inc.php';

if (mysql_select_db('test'))
if(mysql_query("delete from test where sno = 1",$con))
  {  $result=mysql_query("select * from test where sno = 1",$con);
     echo "<br><table><tr><td>sno</td><td>name</td></tr>";
     while($data = mysql_fetch_array($result))
  {
  echo "<tr><td>".$data['sno'] . "</td><td>" .$data['name']."</td></tr>";
  }
  echo "</table><br>data deleted<br>";
  }
else
  {
  echo '<br>data not selected :'.mysql_error().'<br>';
  }

mysql_close($con);
?>



run " del_table.php "

output :


host coonected

snoname

data deleted

database create in php with mysql

connection.inc.php :

<?php
$host='localhost';
$user_name='root';
$password='';
@$con = mysql_connect("$host","$user_name","$password");
if ($con)
 {echo '<br>host coonected<br>';}
else
  {
  die('Could not connect ');
  }
?>
=========================================================
 database_creation.php :

<?php

require 'connection.inc.php';
if(isset($_POST['db'])&&!empty($_POST['db']))
{
$db1=$_POST['db'];
if (mysql_query("create database $db1",$con))
  {
  echo "<br>Database created<br>";
  }
else
  {
  echo '<br>database not created<br> :'.mysql_error().'<br>';
  }
}
else
{ echo '<br>please write database name below<br><br>' ; }
mysql_close($con);
?>

<form action="database_creation.php" method="POST">
<input type="text" name="db">
<input type="submit" name="submit">
</form>

===============================================================
run " database_creation.php "

output :


host coonected

please write database name below

host coonected

please write database name below

   "here button is there"

create table in php with mysql

connection.inc.php :

<?php
$host='localhost';
$user_name='root';
$password='';
@$con = mysql_connect("$host","$user_name","$password");
if ($con)
 {echo '<br>host coonected<br>';}
else
  {
  die('Could not connect ');
  }
?>

creation_table.php :
<?php

require 'connection.inc.php';

if (mysql_select_db('test'))
echo '<br>database selected<br>';
if(mysql_query("create table test(sno varchar(3),name varchar(15))",$con))
  {
  echo "<br>table created<br>";
  }
else
  {
  echo '<br><br>table not created :'.mysql_error();
  }

mysql_close($con);
?>

run " creation_table.php"

output :

host coonected

database selected


table created

connect with database in php with mysql

connection.inc.php :

<?php
$host='localhost';
$user_name='rootgf';
$password='';
@$con = mysql_connect("$host","$user_name","$password");
if ($con)
 {echo '<br>host coonected<br>';}
else
  {
  die('Could not connect ');
  }
?>

connection.php :

<?php

require 'connection.inc.php';



mysql_close($con);
?>

run  " connection.php"
output :
host coonected



array
arrays display
associative arrays in php
multi dimensional arrays in php
for each statements with arrays in php
add one arry to another array or append
array reduce with array_splice() in php
array to string in php
change array size using array_pad() in php
delete array elements with unset() in php
check array index or key with user inputs in php
checking if an element is in array or not using in php

file write in php

<?php

if(isset($_POST['name'])&&isset($_POST['age'])&&isset($_POST['fname']))
{
  $name=$_POST['name'];
  $age=$_POST['age'];
  $fname=$_POST['fname'];
  if(!empty($name)&&!empty($age)&&!empty($fname))
  {
    $hand=fopen($fname.'.txt','w');
    fwrite($hand,$name." ");
    fwrite($hand,$age."\r\n");
    fclose($hand);
  }
 else
    echo 'fill all fields';
}
?>

<form action="file-write.php" method="POST">
name :<input type="text" name="name">
age :<input type="text" name="age">
file name : <input type="text" name="fname">
.txt
<input type="submit" value="submit">
</form>

output :


note :
1) enter all fields
2) the last field is file name
3) first it clears all data from that file and write you entered data into that file
4)if that file not exists then it creates and write that file

read a file in php

<?php

if(isset($_POST['filename']))
{
  if(!empty($_POST['filename']))
  {
    $file=$_POST['filename'].'.txt';
    if(file_exists($file))
    {
         $filename = fopen("$file", "r") ;
         while(!feof($filename))
               {
                 echo fgets($filename)."<br>";
               }
         fclose($filename);
    }
    else
    echo 'file not exits';
  }
 else
echo 'empty';

}
?>

<form action="file-read.php" method="POST">
<input type="text" name="filename">
.txt
<input type="submit" value="submit">
</form>

output :
1)enter the file name for read that file
2)that file must be text file

ex :        test.txt

delete a file in php

<?php

if(isset($_POST['fname']))
{
  if(!empty($_POST['fname']))
  {
       $name=$_POST['fname'].'.txt';
  if(@!unlink($name))
  {
      echo 'there is no file to delete';
  }
  else
      echo 'file successfully deleted';
     }
}
?>

<form action="file-delete.php" method="POST">
file name : <input type="text" name="fname">
.txt
<input type="submit" value="submit">
</form>

output :
 note :
1) enter the file name  for delete that text file

file append in php

<?php

if(isset($_POST['name'])&&isset($_POST['age'])&&isset($_POST['fname']))
{
  $name=$_POST['name'];
  $age=$_POST['age'];
  $fname=$_POST['fname'];
  if(!empty($name)&&!empty($age)&&!empty($fname))
  {
      $file=$_POST['fname'].'.txt';
      if(file_exists("$file"))
      {
      $hand=fopen("$fname".'.txt','a');
      fwrite($hand,$name." ");
      fwrite($hand,$age."\r\n");
      fclose($hand);
      }
      else
      echo 'file not exist';
  }
  else
      echo 'all fielsd are not filled';

}

?>

<form action="file-append.php" method="POST">
name : <input type="text" name="name">
age  : <input type="text" name="age">
file name : <input type="text" name="fname">
.txt
<input type="submit" value="submit">
</form>

output :

note :
1) try with no input datat and click submit button
2) try with don't fill all text boxes and click on submit button
3) try with fill all text boxes and click on submit button
4)check  " test.txt " file

$_POST() in php

<?php

if(isset($_POST['name'])&&isset($_POST['age']))
{
  $name=$_POST['name'];
  $age=$_POST['age'];
  if(!empty($name)&&!empty($age))
  {
   echo '<center>you filled both fields<br><br></center>';
  }
  else
  {
   echo '<center>you not filled both fields<br><br></center>';
  }

}

?>
<center><form action="post.php" method="POST">

               NAME :<input type="text" name="name">

               AGE  :<input type="text" name="age">

                <input type="submit" value="submit">
</form></center>

output :


output :

after filled all fields you clicked on submit button just vies on address bar
it not displays you entered data

$_get() in php

<?php

if(isset($_GET['name'])&&isset($_GET['age']))
{
  $name=$_GET['name'];
  $age=$_GET['age'];
  if(!empty($name)&&!empty($age))
  {
   echo '<center>you filled both fields<br><br></center>';
  }
  else
  {
   echo '<center>you not filled both fields<br><br></center>';
  }

}

?>
<center><form action="get.php" method="get">

               NAME :<input type="text" name="name">

               AGE  :<input type="text" name="age">

                <input type="submit" value="submit">
</form></center>

output :

after filled all fields you clicked on submit button just vies on address bar
it displays you entered data

form with html entities in php

<?php

if(isset($_POST['name'])&&isset($_POST['age']))
{
  $name=$_POST['name'];
  $age=$_POST['age'];
  if(!empty($name)&&!empty($age))
  {
   echo '<center>name :<b>'.$name.'</b> age :<b>' .$age.'</b><br><br></center>';
  }
  else
  {
   echo '<center><b>you not filled both fields</b><br><br></center>';
  }

}

?>
<center><form action="form.php" method="POST">

               NAME :<input type="text" name="name">

               AGE  :<input type="text" name="age">

                <input type="submit" value="submit">
</form></center>

output :

1)if you  filled all fields output displays that is  you entered data

other wise it gives a message as " you not filled both fields "
2) if you give html entities then it also displays as it is

get browser data of visitor in php

<?php
$browser = get_browser(null,true);
print_r($browser);
?>

output :

Array ( [browser_name_regex] => §^.*$§ [browser_name_pattern] => * [browser] => Default Browser [version] => 0 [majorver] => 0 [minorver] => 0 [platform] => unknown [alpha] => [beta] => [win16] => [win32] => [win64] => [frames] => 1 [iframes] => [tables] => 1 [cookies] => [backgroundsounds] => [cdf] => [vbscript] => [javaapplets] => [javascript] => [activexcontrols] => [isbanned] => [ismobiledevice] => [issyndicationreader] => [crawler] => [cssversion] => 0 [supportscss] => [aol] => [aolversion] => 0 )

ckeck ip address is in blocked list or not in php

ip-addr.php :

<?php

include 'ip.inc.php';
foreach($ip_block as $ipb)
 if($ipb==$ip_addr)
 {
   die('<br><br><center><b>your ip is bloacked</center></b>').'<br>';
 }

?>
<center><b><br><br>welcome world</center></b>

=========================================================
ip.inc.php :

<?php

$ip_addr=$_SERVER['REMOTE_ADDR'];
$ip_block=array('222.32.36.25');
?>
===========================================================
run " ip-addr.php "

output :
welcome world

note :
1) if your ip addsess is in block list then is displays  " your ip is bloacked "


header() in php


<?php
header('location:'.'http://http://some-php-programs.blogspot.com/');

?>


output :

automatically http://some-php-programs.blogspot.com/ opened




======================================================


<?php
$webpage='http://some-php-programs.blogspot.com/';
header('location:'.$webpage);

?>

output :
automatically http://some-php-programs.blogspot.com/ opened

$_server() in php

<?php

echo $_SERVER ['SERVER_NAME'].'<br>';
echo $_SERVER ['SERVER_PROTOCOL'].'<br>';
echo $_SERVER ['REQUEST_METHOD'].'<br>';
echo $_SERVER ['HTTP_HOST'];

?>

output :

localhost
HTTP/1.1
GET
localhost



note :

1)some are listed above

modify timestamp in php

<?php

$time=time();

echo 'today date is '.date('d : m : y',$time).'<br>'.'today time is '.date('H:i:s',$time).'<br>';
echo 'today date is '.date('D : M : Y',$time).'<br>'.'today time is '.date('h:i:s',$time).'<br>';

?>

output :
today date is 18 : 07 : 12
today time is 17:35:10
today date is Wed : Jul : 2012
today time is 05:35:10

note :
for every refresh of the browser ( after a second ) the seconds are changing 

rand() in php

<?php

$random=rand();
$maximum_ramdom_number=getrandmax();
echo 'rendom number :'.$random.'<br>';
echo 'maximum rendom number :'.$maximum_ramdom_number.'<br>';

//   or

echo 'rendom number :'. rand().'<br>';
echo 'maximum rendom number :'.getrandmax();

?>

output :
rendom number :30224
maximum rendom number :32767
rendom number :21769
maximum rendom number :32767

note:

1) for every refresh of the browser the random number is changing . but , the maximum random number is not change

timestamp in php

<?php

$time=time();

echo 'today date is '.date('d : m : Y',$time).'<br>'.'today time is '.date('H:i:s',$time);

?>

output :
today date is 18 : 07 : 2012
today time is 17:28:43

preg_match() or search a string / substring in php

<?php

$string='hello world';
if(preg_match("/hel/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>
output :word or characted found


<?php

$string='hello world';
if(preg_match("/z/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>

output :word or characted not found

<?php

$string='hello world';
$searchstring='hel';
if(preg_match("/$searchstring/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>
output :word or characted found
<?php

$string='hello world';
$searchstring='z';
if(preg_match("/$searchstring/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>

output :word or characted not found
<?php

$string='hello world';
$searchstring='hello';
if(preg_match("/$searchstring/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>
output : word or characted found

required() in php

index.php :
<?php

@require 'required-page.php';


echo '<br>';
echo 'home page';


?>
output :

database connected

home page
===========================================================
 required-page.php :
<?php

mysql_connect('localhost','root','') or die('not connected');
echo '<center>database connected</center>';

?>
==========================================================
sub-page.php :

<?php

@require 'required-page.php';
echo 'sub page';

?>

output :

database connected
sub page
==========================================================
note :

1) ' @ ' use for don't  display errors related to php

2) if there is no  ' include-page.php ' page then nothing is display in included pages like ' index.php  ' , 'sub-page.php '

3) here ' index.php ' , ' sub-page.php ' pages  are run

include() in php

liclude.php :

<?php

mysql_connect('localhost','root','') or die('not connected');
echo '<center>database connected</center>';

?>
=================================================================

index.php :

<?php

@include 'include-page.php';

echo '<br>';
echo 'home page';


?>
output :

database connected

home page=================================================================

sub-page.php :

<?php

@include 'include-page.php';
echo 'sub page';

?>

output :


database connected
sub page
=================================================================

note :

 ' @ ' use for don't  display errors related to php

if there is no  ' include-page.php ' page then on problem , remaining all are worked

here ' index.php ' , ' sub-page.php ' pages  are run


for each statements with arrays in php

<?php


$birds= array('Parrots' =>
array('Kakapo','Kea ','Vernal Hanging Parrot'),

'Cuckoos and Turacos'=>
array('Purple-crested Turaco','Grey Go-away-bird','Great Blue Turaco'));
//display all
print_r($birds);


//for new line
echo '<br><br><br>';

// with foreach() display only main array
foreach($birds as $category => $sub){

echo '<b>'.$category.'<br></b>';
}

//for new line
echo '<br><br>';

// with foreach() display main array and sub arrays
foreach($birds as $category => $sub1){
echo '<b>'.$category.'<br></b>';
foreach($sub1 as $sub2)
echo $sub2.'<br>';
}
?>

output ;

Array ( [Parrots] => Array ( [0] => Kakapo [1] => Kea [2] => Vernal Hanging Parrot ) [Cuckoos and Turacos] => Array ( [0] => Purple-crested Turaco [1] => Grey Go-away-bird [2] => Great Blue Turaco ) )


Parrots
Cuckoos and Turacos


Parrots
Kakapo
Kea
Vernal Hanging Parrot
Cuckoos and Turacos
Purple-crested Turaco
Grey Go-away-bird
Great Blue Turaco

multi dimensional arrays in php

<?php


$birds= array('Parrots' =>
array('Kakapo','Kea ','Vernal Hanging Parrot'),

'Cuckoos and Turacos'=>
array('Purple-crested Turaco','Grey Go-away-bird','Great Blue Turaco'));

print_r($birds);

echo '<br><br><br>';

echo $birds['Parrots'][0].'<br>';
echo $birds['Parrots'][1].'<br>';
echo $birds['Parrots'][2].'<br>';


echo '<br><br><br>';

echo $birds['Cuckoos and Turacos'][0].'<br>';
echo $birds['Cuckoos and Turacos'][1].'<br>';
echo $birds['Cuckoos and Turacos'][2].'<br>';


?>


output :
Array ( [Parrots] => Array ( [0] => Kakapo [1] => Kea [2] => Vernal Hanging Parrot ) [Cuckoos and Turacos] => Array ( [0] => Purple-crested Turaco [1] => Grey Go-away-bird [2] => Great Blue Turaco ) )


Kakapo
Kea
Vernal Hanging Parrot



Purple-crested Turaco
Grey Go-away-bird
Great Blue Turaco


associative arrays in php

<?php

$alphabate=array('A'=>'APPLE','a'=>'apple','B'=>'BALL','b'=>'ball','C'=>'CAT','c'=>'cat','D'=>'DOLL','d'=>'doll','E'=>'ELEPHANT','e'=>'elephant');


print_r($alphabate);

// '<BR>' USE FOR NEW LINE
// 'A' is not equal to 'a' in arrays index
// '.' use for concatenation

echo '<br><br><br>'.
$alphabate['A'].'<br>';
echo $alphabate['a'].'<br>';
echo $alphabate['B'].'<br>';
echo $alphabate['b'].'<br>';
echo $alphabate['C'].'<br>';
echo $alphabate['c'].'<br>';
echo $alphabate['D'].'<br>';
echo $alphabate['d'].'<br>';
echo $alphabate['E'].'<br>';
echo $alphabate['e'].'<br>';

?>

output :

Array ( [A] => APPLE [a] => apple [B] => BALL [b] => ball [C] => CAT [c] => cat [D] => DOLL [d] => doll [E] => ELEPHANT [e] => elephant )


APPLE
apple
BALL
ball
CAT
cat
DOLL
doll
ELEPHANT
elephant

arrays display in php

<?php

$alphabate=array('A','B','C','D','E');

print_r($alphabate);
// '<BR>' USE FOR NEW LINE
echo '<br>'.$alphabate[0].'<br>';
echo $alphabate[1].'<br>';
echo $alphabate[2].'<br>';
echo $alphabate[3].'<br>';
echo $alphabate[4];

?>

output :
Array ( [0] => A [1] => B [2] => C [3] => D [4] => E )
A
B
C
D
E

function with return in php

<?php

$num1=10;
$num2=5;

function add($num1,$num2)
{
return $num1+$num2;
}


function multi($num1,$num2)
{
return $num1*$num2;
}

echo 'add : '.add($num1,$num2).'<br>';

echo 'multiply : '.multi($num1,$num2).'<br>';

echo 'function in function : '.add(multi($num1,$num2),multi($num1,$num2)).'<br>';


?>

output :

add : 15
multiply : 50
function in function : 100

global variable in php

<?php

$num1=10;
$num2=5;

function add()
{
  global $num1,$num2;
  return $num1+$num2;
}

function sub($num1,$num2)
{
  return $num1-$num2;
}

echo 'addition  with out global variables : '. add().'<br>';

echo 'subtraction with global variables : '.sub($num1,$num2);

?>output :

addition with out global variables : 15
subtraction with global variables : 5

triple Equals or === in php

<?php

echo "<b>compare both outputs</b><br><br>";
if(1=='1')
echo "equal";

//for new line
echo '<br>';

if(1==='1')
    echo "equal";
else
    echo "not equal";

?>

output :

compare both outputs

equal
not equal

switch in php

<?php

$input="1";

switch($input)
{
case 1       : echo 'one';
break;

case 2       : echo 'two';
break;

case 3       : echo 'three';
break;

case 4       :echo 'four';
break;

case 5       : echo 'five';
break;

case 'hello' : echo 'hello how are you?';
break;

default:
echo '<b>required number or string is not found</b>';
break;
}
?>

output :
one
=============================================

input : 2
output: two


input :3
output:three

input :4
output:four

input : hello
output: hello how are you?



function with parameters in php

<?php
 
function hai($age){

if($age==21)
{
  echo 'you are eligible for vote';
}
else{
  echo 'you are not eligible for vote';
}
}
$age=21;
echo hai($age);
?>
outputt :

you are eligible for vote

simple function in php

<?php

function hai(){

echo 'hello world';

}
echo hai().', i am a human';
?>

output :

hello world, i am a human

note  :  " . "  use for concatenation

die() and exit() Functions in php

<?php

@mysql_connect('localhost','root','') or die("not connect");


echo 'connected';

?>

outout  : connected
 
<?php


@mysql_connect('localhost','root','n') or die("not connect");

echo 'connected';

?>

outout  : not connect

<?php

//   " @ " use for errors from php not display

@mysql_connect('localhost','root','') or exit("not connect");

echo 'connected';

?>

outout  : connected

<?php


@mysql_connect('localhost','root','n') or exit("not connect");

echo 'connected';

?>

outout  : not connect

note : 1)     can use " || " in the place of " or " 
         2)    " @ " use for errors not display from php

concatenation in php

<?php

$name='php';
$version='5.4';
$use='for web development';

echo  'hai the ';
echo   ' ';
echo  $name;
echo   ' ';
echo  $version;
echo   ' ';
echo  $use;

 // for new line

echo '<br>';


//      "." use for concatenation

echo 'hai the '.$name.' '.$version.' '.$use;
?>


output :

hai the php 5.4 for web development
hai the php 5.4 for web development

foreach() in php

<?php

$food=array("APPLE","PINEAPPLE","POMEGRANATE","TOMATO");

foreach($food as $item){
 
  echo $item.'<br>';

}

?>



output :

APPLE
PINEAPPLE
POMEGRANATE
TOMATO

array() in php

<?php

$food=array("pizza","rice");

echo "$food[0]".'<br>';

echo "$food[1]";

?>

output :

pizza
rice

arithmetic operators in php


                                    arithmetic operators in php : + , - , * , / , % , ++ , --


<?php


$a=10;


echo $a+10;


?>


output : 20



<?php


$a=10;


echo $a-10;


?>
output :0 


<?php


$a=10;


echo $a*10;


?>
output : 100

<?php


$a=10;


echo $a/10;


?>
output : 1

<?php


$a=10;


echo $a+10;


?>
output : 20


<?php


$a=10;


echo $a--;


?>
output : 10

<?php


$a=10;


echo $a++;


?>


output : 10




<?php


$a=10;


echo --$a;


?>
output :  9

<?php


$a=10;


echo ++$a;


?>


output : 11





logical operators in php

                                              logical operators in php :  && , || , !




<?php


$a=10;
$b=20;
if($a<15 && $b>15)
echo  "$a is less than 15 and $b is grater than 15";


?>
output : 10 is less than 15 and 20 is grater than 15






<?php



$a=10;
$b=20;
if($a<15 || $b<15)
echo  "$a is less than 15 and $b is grater than 15";


?>
output : 10 is less than 15 or 20 is grater than 15





<?php



$a=10;
$b=20;
if(!($a<15 && $b>15))
echo  "$a is less than 15 and $b is grater than 15";


?>
output : 


nothing to display , because , inner value is true and we apply ! to that so , the value of if statement is false







comparison operators in php

                                           comparison operators :   == , <= , >= , != , < , > 



<?php
$num1=10;
$num2='10';


if($num1==$num2)
echo "$num1 is equal to $num2";


?>
output : 


nothing to display , $num1 is integer and  $num2 is string , string is not equal to integer


<?php
$num1=10;
$num2=20;


if($num1==$num2)
echo "$num1 is equal to $num2";


?>
output : 


nothing to display , because both are not equal





<?php

$num1=10;
$num2=20;


if($num1<=$num2)
echo "$num1 is less than or equal to $num2";


?>
output : 10 is less than or equal to 20



<?php
$num1=10;
$num2=20;


if($num1>=$num2)
echo "$num1 is grater than or equal to $num2";


?>

output : 


nothing to display , because 10  is not grater than or equal to 20






<?php

$num1=10;
$num2=20;


if($num1!=$num2)
echo "$num1 is not equal to $num2";


?>
output : 10 is not equal to 20



<?php
$num1=10;
$num2=20;


if($num1<$num2)
echo "$num1 is less than to $num2";


?>
output : 10 is less than to 20





<?php

$num1=10;
$num2=20;


if($num1>$num2)
echo "$num1 is grater than $num2";


?>
output :
nothing to display , because10  is not grater than to 20
















assignment operators in php



                            assignment operators in php     :      = , += , -= , *= , /= , .= , %=

<?php

$num1=10;
$num2=20;
echo ('<br>'. $num1." " .$num2);
?>


output :10 20




<?php
$num1=10;
$num2=20;
echo ('<br>'.$num2+=$num1);
?>


output :
30 





<?php


$num1=10;
$num2=20;
echo ('<br>'.$num2-=$num1
);?>


output :
10



<?php


$num1=10;
$num2=20;
echo ('<br>'.$num2*=$num1);
?>


output :
200





<?php


$num1=10;
$num2=20;


echo ('<br>'.$num2/=$num1);?>

output :
2



<?php
$num1=10;
$num2=20;
echo ('<br>'.$num2.=$num1);?>


output :
2010




<?php

$num1=10;
$num2=20;
echo ('<br>'.$num2%=$num1);

?>

output :
0








compound data types in php

                               array data types and object data types are comes under this category




special data types in php

                                     resource data types and null data types




resource data types refers the external resources  like data connection , file pointer , FTP connection ...ect


<?php

mysql_connect("hostname",username","password");

?>

<?php

$con=mysql_connect("hostname",username","password");
echo $con;

?>

here con is external resource type

output :    resource #id2
                                                                  null data type



null is not a value

the variable not initialized with any  value
the variable initialized with null value
if the value of the variable is removed using unset function

based on by that 3 conditions we consider a variable has null value


<?php

$a=100;
unset($a);
echo is_null($a);

?>

output :  1

if is it null returns 1 else returns nothing




comments in php

<?php

// write your single line comments here


/*

write
multiple line
comments
here

*/


?>

scalar data types in php

boolean , integer , float,string are scalar data types in php

                                                    boolean : it holds boolean values

"true" returns 1 and "false" does not returns any value.
<?php

$a=true;
echo $a;

?>

output : 1

is_bool(x) checks is it bool or not , if bool then return 1  else it is nothing anything



<?php

$a=true;
echo (bool)$a;

?>

output :1


<?php

$a=false;
echo (bool)$a;

?>

output : 1



<?php

$a=5;
echo (bool)$a;

?>

output :

nothing above display , because it is not a boolean value . So, false return . So, onthing to display


                                                           integer : it holds integer values

<?php

$a=5;
echo $a;


?>


output : 5


to convert input value into integer datatype  we use "is_int"


<?php


$a=100;
echo is_int($a);


?>


output : 1


<?php

$a='world';
echo is_int($a);

?>

output :

at above nothing is display
reason : "world" is not an integer
 So, false returned

                                                          float :  it holds floating point value

<?php

$a=10.8;
echo $a;

?>

output : 10.8

                                                     string :  it holds string value

<?php

$a="world";
echo $a;

?>

output : world

<?php

$a="world";
$atr="welcome to $a";
echo $atr;
?>
output  :  welcome to world
<?php


$a="world";
$atr="welcome to $a".'<br>';   // here <br> for new line
$atr='welcome to $a';
echo $atr;
echo 'atr1;



?>


output :
welcome to world
welcome to $a




print_r() in php

<?php

$num=array(10,20,30);
echo '<strong>echo $num[0]</strong>   :   '.$num[0].'<br>';
echo '<strong>print_r($num) </strong>    :    ';
print_r($num);


?>

output :


echo $num[0] : 10
print_r($num) : Array ( [0] => 10 [1] => 20 [2] => 30 )

echo in php

<?php

$num=21;

echo  " the number is $num ".' <br> ';

echo '  the number is $num<br>  ';

echo ' the number is '.' <br> '.$num;


?>

output :


the number is 21
the number is $num
the number is
21

if-else-if statement or if else if statement in php

<?php

$sno=101;
$name='hari kishore';
$marks=300;

if($sno==101)
  {
     if($name=='hari kishore')
        {
           if($marks>=300)
               {
                  echo 'you are second class';

                }
             else
                {
                    echo 'you are not second class';
                 }
         }
       else 
         {
               echo 'you are not hari kishore';
          }
    }

?>

output : you are second class

if-else statement in php

<?php

$num=21;

if($num==20)
  {
     echo 'number is 20';
   }

else
   {
     echo 'number is not 20';
   }

?>

output : number is not 20

if statement or single if statement in php

<?php

$num=21;

if($num==21)
   {
      echo 'number is 21<br>';
    }


if($num==20)
   {
       echo 'numbet is 20<br>';
    }
?>


output : number is 21

first php file

<?php


//add code here
 echo 'welcome to http://some-php-programs.blogspot.in/';

?>


output : welcome to http://some-php-programs.blogspot.in/


Share Links

Related Posts Plugin for WordPress, Blogger...