71419-php Functions 1
71419-php Functions 1
- We can use the header function to redirect the browser to a specific website :
Example:
<html><body><?php
header("Location: http://www.uob-bh.com/forum/forum.php"); ?>
</body></html>
Examples:
<html><body>
<?php
header("Cache-Control: no-cache, must-revalidate"); ?>
</body></html>
<html><body>
<?php
header("Expires: Mon, 20 May 2009 05:01:00 GMT"); ?>
</body></html>
$_SERVER["HTTP_REFERER"]
<?php
echo "Referer: " . $_SERVER["HTTP_REFERER"] . "<br />";
?>
$_SERVER["HTTP_USER_AGENT"]
Example:
<?php
echo "Browser: " . $_SERVER["HTTP_USER_AGENT"] . "<br />";
?>
$_SERVER["REMOTE_ADDR"]
Example:
<?php
echo "User's IP address: " . $_SERVER["REMOTE_ADDR"] ; ?>
3- Split: we use it If we want to split on any of the characters which are considered
special by regular expressions
<html><body>
<?php
$my="1/22/33";
$arr=split ("/", $my);
echo ($my."<br />");
foreach ($arr as $value)
echo ("Date Portion: " . $value . "<br />");
?>
</body></html>
4- GLOBALS: a global variable can be accessed in any part of the program, just
write:
$GLOBALS['variableName']
global $variableName;
Example:
<html><body>
<?php
$a=1;
$b=2;
$c=3;
function multi()
{
$GLOBALS['a']=$GLOBALS['a']*$GLOBALS['b']*$GLOBALS['c'];
}
multi();
echo $a;
?>
</body></html>
Example:
<html><body>
<?php
$colors=array('red', 'blue' , 'black');
array_push($colors, 'pink');
print_r($colors);
?>
</body></html>
Array_pop ($arrayName)
Example:
<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
array_pop($colors);
print_r($colors);
?>
</body></html>
Array_shift ($arrayName);
<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
array_shift($colors);
print_r($colors);
?>
</body></html>
Example:
<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
array_unshift($colors,'pink');
print_r($colors);
?>
</body></html>
Example:
<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
$new=implode(';',$colors);
print_r($new);
?>
</body></html>
Sort ($arrayName,);
Example:
<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
sort($colors);
print_r($colors);
?>
</body></html>
rsort ($arrayName,);
Example:
<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
rsort($colors);
print_r($colors);
?>
</body></html>
Is_array ($arrayName,);
<html><body>
<?php
$var_name=array('A','B','C');
if (is_array($var_name))
echo 'This is an array....';
else
echo 'This is not an array....';
?>
</body></html>
13- Count: used for count the number or elements in the array
( gives the array size )
count ($arrayName);
Example:
<html><body>
<?php
$var_name=array('A','B','C');
for ($i=0 ; $i<count($var_name) ; $i++)
echo $var_name[$i];
?>
</body></html>
14- isset, empty and is_null: used to test the value of a variable.
<html><body>
<?php
$n=6;
if ( !isset ($n))
echo ' emplty ' ;
else
echo 'not empty' ;
?>
</body></html>
Example:
<html><body>
<?php
$xyz='w3resource.com';
echo 'Before using unset() the value of $xys is : '. $xyz.'<br>';
unset($xyz);
echo 'After using unset() the value of $xys is : '. $xyz;
?>
</html></body>
16- trim: removes whitespaces and other predefined characters from both sides of a
string.
Trim ($variableName)
<html><body>
<html>
<body>
<?php
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br />";
echo "With trim: " .trim($str);
?>
<body>
<html>
</html></body>
17- Include & require :In PHP, you can insert the content of one PHP file into another
PHP file before the server executes it.
include and require statements are used to insert useful codes written in other files, in
the flow of execution.
require will produce a fatal error (E_COMPILE_ERROR) and stop the script
include will only produce a warning (E_WARNING) and the script will continue
include("filename");
require ("filename");
Example:
<html><body>
<?php
include("p1.php");
?>
</body></html>
Date elements:
- D: day code
- d : date of the current day
- m : current month number
- y: current year number
Time elements:
- a: am or pm depending on the time
- A: AM or PM depending on the time
- g: hour without keeping zeroes. Values are 1 through 12
- G: hour in 24-hour format without leading zeros . values are 0 through 23
- h: hour with leading zeroes. Values 01 through 12
- H: hour in 24-hour format with leading zeroes. Values 00 through 23
- i: minute with leading zeroes. Values 00 through 59
- s : seconds with leading zeroes. Values 00 through 59
Example:
<html><body>
<?php
echo date("h:i:s A " );
echo date ( "d/m/y" );
?>
</body></html>
19- mktime: useful for doing date arithmetic and validation. It will automatically
calculate the correct value for out-of-range input
<html><body>
<?php
$tomorrow= mktime(0,0,0,date("m"), date("d")+1, date("y"));
$yesterday= mktime(0,0,0,date("m"), date("d")-1 , date("y"));
$last_year_this_time= mktime(date("h"), date("i"), date("s"),
date("m"),date("d"),date("y")-1);
echo "tomorrow is ".date("d/m/y",$tomorrow) ."<br/>";
echo "yesterday is ".date("d/m/y",$yesterday)."<br/>";
echo "last year at this time is
".date("d/m/y",$last_year_this_time).' '. date("h:i:s A
" );
?>
</html></body>
20- extract: imports variables into the local symbol table from an array
extract ( arrayName ) ;
Example:
<html><body>
<?php
$a = 'Original';
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
?>
</body></html>
>html><body<
<?php
$var1='98 ';
$var2='05 ';
settype($var1, "integer");
settype($var2, "integer");
echo ($var1.'<br>');
echo ($var2.'<br>');
echo ($var1-$var2.'<br>');
>?
>html></body/<
die(message);
Example:
<html><body>
<?php
if(!file_exists("welcome.txt"))
{
die("File not found");
}
else
{
$file=fopen("welcome.txt","r");
}
?>
</html></body>