0% found this document useful (0 votes)
20 views

71419-php Functions 1

Uploaded by

XXX XXX
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

71419-php Functions 1

Uploaded by

XXX XXX
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1- Header function

- We can use the header function to redirect the browser to a specific website :

header("Location: Website URL");

Example:

<html><body><?php
header("Location: http://www.uob-bh.com/forum/forum.php"); ?>
</body></html>

- We can Use it to disable cashing by two different ways:

header ("Cache-Control: no-cache, must-revalidate");


header("Expires: Mon, 26 Jul 2008 05:00:00 GMT");

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>

Note: Use any date in the past

2- PHP Server Variables


 HTTP_REFERER :When a web browser moves from one website to another
and between pages of a website, it can optionally pass the URL it came from.
This is called the HTTP_REFERER.

$_SERVER["HTTP_REFERER"]

Done by: Abeer(www.UOB-BH.com)


Example:

<?php
echo "Referer: " . $_SERVER["HTTP_REFERER"] . "<br />";
?>

 HTTP_USER_AGENT: This can tell us more about the user's operating


system, as well as their browser

$_SERVER["HTTP_USER_AGENT"]

Example:

<?php
echo "Browser: " . $_SERVER["HTTP_USER_AGENT"] . "<br />";
?>

 REMOTE_ADDR : gives the IP address :

$_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

Note: explode function do the same job

split("the special character", variableName);


explode("the special character", variableName);

Done by: Abeer(www.UOB-BH.com)


Example:

<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']

 To create a global variable anywhere in the program write:

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>

Done by: Abeer(www.UOB-BH.com)


5- Push : to add an element at the end of the array :

Array_push ($arrayName, the element);

Example:

<html><body>
<?php
$colors=array('red', 'blue' , 'black');
array_push($colors, 'pink');
print_r($colors);
?>
</body></html>

6- Pop: to remove an element from the end :

Array_pop ($arrayName)

Example:

<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
array_pop($colors);
print_r($colors);
?>
</body></html>

7- Shift: to remove the first element in the array

Array_shift ($arrayName);

Done by: Abeer(www.UOB-BH.com)


Example:

<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
array_shift($colors);
print_r($colors);
?>
</body></html>

8- Unshift: to add an element at the beginning

Array_unshift ($arrayName, the element)

Example:

<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
array_unshift($colors,'pink');
print_r($colors);
?>
</body></html>

9- Implode: we use it to join the elements with a specific character or string:

implode (' the character or string ' ,$arrayName)

Example:

<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
$new=implode(';',$colors);
print_r($new);
?>
</body></html>

Done by: Abeer(www.UOB-BH.com)


10- Sort: to sort the elements alphabetically

Sort ($arrayName,);

Example:

<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
sort($colors);
print_r($colors);
?>
</body></html>

11- rsort: to sort the elements alphabetically in revers:

rsort ($arrayName,);

Example:

<html><body>
<?php
$colors=array('red', 'blue' , 'black' );
rsort($colors);
print_r($colors);
?>
</body></html>

12- is_array : is used to find whether a variable is an array or not

Is_array ($arrayName,);

Done by: Abeer(www.UOB-BH.com)


Example:

<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.

- Isset: it returns true only when the variable is not null.


- Empty: it will return true if the variable is an empty string, false, array (),
NULL, "0"and for an unset variable.
- Is_null: it returns true only when the variable is null. is_null() is opposite of
isset().

Isset ($variableName or ArrayName);

Empty ($variableName or ArrayName);

is_null ($variableName or ArrayName);

Done by: Abeer(www.UOB-BH.com)


Example:

<html><body>
<?php
$n=6;
if ( !isset ($n))
echo ' emplty ' ;
else
echo 'not empty' ;
?>
</body></html>

15- unset: destroys a given variable

unset (variable1, variable2.... )

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)

Done by: Abeer(www.UOB-BH.com)


Example:

<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.

Note: Include and require are identical, except upon failure:

 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>

Done by: Abeer(www.UOB-BH.com)


18- Date : use to format a time or a date

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

Date(" insert the elements with the format" )

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

mktime(hour, minute, second, month, day, year, daylight savings time) ;

Done by: Abeer(www.UOB-BH.com)


Example:

<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>

21- settype: is used to set the type of the variable.

settype (variableName, type)


;

Done by: Abeer(www.UOB-BH.com)


Example:

>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/<

22- die: prints a message and exits the current script.

die(message);

Example:

<html><body>
<?php
if(!file_exists("welcome.txt"))
{
die("File not found");
}
else
{
$file=fopen("welcome.txt","r");
}
?>
</html></body>

Done by: Abeer(www.UOB-BH.com)

You might also like