<?php
phpinfo();
?>
<?php
//whether ip is from share internet
if (!emptyempty($_SERVER['HTTP_CLIENT_IP']))
{
$ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
//whether ip is from proxy
elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//whether ip is from remote address
else
{
$ip_address = $_SERVER['REMOTE_ADDR'];
}
echo $ip_address;
?>
< ?php
session_register($name_your_session_here);
?>
The following code can be used for it,
header("Location:page_to_redirect.php");
exit();
echo $_COOKIE ["cookie_name"];
preg_split — Split string by a regular expression
< ?php
// split the phrase by any number of commas or space characters,
// which include " ", \r, \t, \n and \f
$keywords = preg_split("/[\s,]+/", "hypertext language, programming");
print_r($keywords);
?>
< ?php
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>
$arr = array('apple', 'grape', 'lemon');
$host = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($host, $username, $password);
//connect to server
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$host = "localhost";
$username = "root";
$password = "";
$conn = new PDO("mysql:host=$host;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
unset($_SESSION['object']);
$my_qry = mysql_query("SELECT * FROM `users` WHERE `u_id`='1'; ");
$result = mysql_fetch_array($my_qry);
echo $result['First_name'];
session_destroy();
$file="full_path/filename.php" unlink($file); //make sure you have enough permission to do delete the file.
$string="cakephp and zend"; echo strtoupper($string);
$string="CAKEPHP AND ZEND"; echo strtolower($string);
$string="cakephp and zend"; echo ucwords($string);
array_merge example
$array1 = array('one','two');
$array2 = array(1,2);
$result = array_merge($array1,$array2);
print_r($result);
array_combine example
$array1 = array('one','two');
$array2 = array(1,2);
$result = array_combine($array1,$array2);
print_r($result);
$array = array('cakephp','zend');
sizeof($array);
count($array);
It is used to display the data-type and values of variable. For Example.
$name='WTEN'; var_dump($name);
It will convert an object to array.
$obj = new stdClass(); $obj->key1 ='Value1'; $obj->key2 ='Value3'; $obj->key3 ='Value3'; $obj->key4 ='Value4'; $array = (Array)$obj; print_r($array);//Array ( [key1] => Value1 [key2] => Value3 [key3] => Value3 [key4] => Value4 ) /pre>
file_exists is used to check directory exist OR NOT.
mkdir is used to create the new directory.
$path="/path/to/dir";
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
func_num_args () used to get the number of arguments passed to the function .
parent::constructor()
array_flip exchange the keys with their associated values in array ie. Keys becomes values and values becomes keys.
<?php
$arrayName = array("course1"=>"php","course2"=>"html");
$new_array = array_flip($arrayName);
print_r($new_array);
?>
OUTPUT :
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
List() function is used to assign values to a list of variable in one operation and list only works on numerical arrays.
<?php
$varArray = array("PHP","MYsql","Jquery");
list($a, $b, $c) = $varArray;
echo "I have knowledge of $a,$b and $c.";
?>
<?php
if (version_compare(PHP_VERSION, '6.0.0') >= 0) {
echo 'I am at least PHP version 6.0.0, my version: ' . PHP_VERSION . "\n";
}
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
echo 'I am at least PHP version 5.3.0, my version: ' . PHP_VERSION . "\n";
}
if (version_compare(PHP_VERSION, '5.0.0', '>=')) {
echo 'I am using PHP 5, my version: ' . PHP_VERSION . "\n";
}
if (version_compare(PHP_VERSION, '5.0.0', '<')) {
echo 'I am using PHP 4, my version: ' . PHP_VERSION . "\n";
}
?>
<?php
$file = basename($_SERVER['PHP_SELF']);
$no_of_lines = count(file($file));
echo "There are $no_of_lines lines in $file"."\n";
?>
<?php
// current time
echo date('h:i:s') . "\n";
// sleep for 5 seconds
sleep(5);
// wake up
echo date('h:i:s')."\n";
?>
<?php
function trinary_Test($n){
$r = $n > 30
? "greater than 30"
: ($n > 20
? "greater than 20"
: ($n >10
? "greater than 10"
: "Input a number atleast greater than 10!"));
echo $n." : ".$r."\n";
}
trinary_Test(32);
trinary_Test(21);
trinary_Test(12);
trinary_Test(4);
?>
<?php $full_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; echo $full_url."\n"; ?>
<?php
echo php_uname()."\n";
echo PHP_OS."\n";
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo 'This is a server using Windows!';
} else {
echo 'This is a server not using Windows!'."\n";
}
?>
<!DOCTYPE html> <html> <head> <title>My credits page </head> <body> <?php // some code of your own phpcredits(CREDITS_ALL - CREDITS_FULLPAGE); // some more code ?> </body>