Monday, July 9, 2012

Remort IP Adress,Array,Require& Include php



Q:-How do I get the remote user IP address by php ?

Answer

Codes
<?php
$ip= $REMOTE_ADDR;
echo "<br> Your IP Address : " . GetHostByName($ip);
?>
Result

Your IP Address :
12.215.42.19

Q:-What is the difference between require and include in php ? 
Answer:-
The require() statement includes and evaluates a specific file. The require() and include() statements are similar in every way except how they handle errors. When there is an error the include() statement produces a warning but the require() statement results in a fatal error. The require statement is used when you want a missing file to halt the processing of the page. In this case, if include() is used even then the script will keep on running regardless of the missing file. 


Q:-How do I delete an array element?
Answer:- This could be done with unset()
Example of an array.
<?php
$ar=array(1,2,3,4,5,6,7,8,9,10);

echo $ar[0]."</br>";
echo $ar[1]."</br>";
echo $ar[2]."</br>";
echo $ar[3]."</br>";
echo $ar[4]."</br>";
echo $ar[5]."</br>";
echo $ar[6]."</br>";
echo $ar[7]."</br>";
echo $ar[8]."</br>";
echo $ar[9];
?>
 Out put
1
2
3
4
5
6
7
8
9
10
After unset the index5
<?php
$ar=array(1,2,3,4,5,6,7,8,9,10);
unset($ar[5]);
echo $ar[0]."</br>";
echo $ar[1]."</br>";
echo $ar[2]."</br>";
echo $ar[3]."</br>";
echo $ar[4]."</br>";
echo $ar[5]."</br>";
echo $ar[6]."</br>";
echo $ar[7]."</br>";
echo $ar[8]."</br>";
echo $ar[9];
?>
 Out put
1
2
3
4
5

7
8
9
10

Q:-How do I write an array to a text file?
 Answer:-this would be displayed in the following example.

<?php
$data=array(
  "First name"=>"Angelina",
  "Last name"=>"Jolie",
);
$filename = 'data.txt';
$string   = '';

foreach($data as $key => $val) {
    $string .= "$key = $val\n";
}

file_put_contents($filename, $string);
?>
First name = Angelina
Last name = Jolie

Q:-How do I redirect to a different pages?
  Answer:-this would be displayed in the following example.
<?php

   header( 'Location: http://wintekweb.blogspot.com/2012/05/php-scripts-home.html' ) ;

?>

No comments:

Post a Comment