الأربعاء، 21 سبتمبر 2011

Important PHP functions

 Functions

 

trim() Function

 The trim() function removes whitespaces and other predefined characters from both sides of a string.

Syntax

trim(string,charlist) ;

<?php
$string 
"        منتدى بي اتش بي                    ";
echo 
trim($string);// يتم طباعة منتدى بي اتش بي بدون فراغات قبلها وبعدها
$string2 "montadaphp";
echo 
trim($string2"map");// يتم طباعة ontdh
?>

 _________________________

 is_dir() function

The is_dir() function checks whether the specified file is a directory.
This function returns TRUE if the directory exists.

Syntax

is_dir(file)

Example

<?php
$file = "images";
if(is_dir($file))
  {
  echo ("$file is a directory");
  }
else
  {
  echo ("$file is not a directory");
  }
?>

 _________________________

header() Function

The header() function sends a raw HTTP header to a client.
It is important to notice that header() must be called before any actual output is sent (In PHP 4 and later, you can use output buffering to solve this problem):

<html>
<?php
// This results in an error.
// The output above is before the header() call
header('Location: http://www.example.com/');
?>

Syntax

header(string,replace,http_response_code) 




Parameter Description
string Required. Specifies the header string to send
replace Optional. Indicates whether the header should replace previous or add a second header. Default is TRUE (will replace). FALSE (allows multiple headers of the same type)
http_response_code Optional. Forces the HTTP response code to the specified value (available in PHP 4.3 and higher)



Tips and Notes

Note: Since PHP 4.4 this function prevents more than one header to be sent at once. This is a protection against header injection attacks.

 Example 1

Prevent page caching:
<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>

<html>
<body>

...
...
Note: There are options that users may set to change the browser's default caching settings. By sending the headers above, you should override any of those settings and force the browser to not cache!

Example 2

Let the user be prompted to save a generated PDF file (Content-Disposition header is used to supply a recommended filename and force the browser to display the save dialog box):
<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

<html>
<body>

...
...
Note: There is a bug in Microsoft IE 5.5 that prevents this from working. The bug can be resolved by upgrading to Service Pack 2 or later.

 _________________________

strtoupper() Function

Definition and Usage

The strtoupper() function converts a string to uppercase.

Syntax

strtoupper(string)

Example

<?php
echo strtoupper("Hello WORLD!");
?>
The output of the code above will be:
HELLO WORLD!

 _________________________

 

PHP end() Function

Definition and Usage

The end() function moves the internal pointer to, and outputs, the last element in the array.
This function returns the value of the last element in the array on success.

Syntax

end(array)

Parameter Description
array Required. Specifies the array to use

Example

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");

echo current($people) . "<br />";
echo end($people);
?>
The output of the code above will be:
Peter
Cleveland

 _________________________

mysql_insert_id()

The mysql_insert_id() function returns the AUTO_INCREMENT ID generated from the previous INSERT operation.
This function returns 0 if the previous operation does not generate an AUTO_INCREMENT ID, or FALSE on MySQL connection failure.

Syntax

mysql_insert_id(connection)

Parameter Description
connection Optional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

Tips and Notes

Note: Be sure to call mysql_insert_id() immediately after a query to get the correct value.

Example

<?php
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db",$con);

$sql = "INSERT INTO person VALUES ('Børge','Refsnes','Sandnes','17')";
$result = mysql_query($sql,$con);
echo "ID of last inserted record is: " . mysql_insert_id();

mysql_close($con);
?>
The output of the code above could be:
ID of last inserted record is: 5

 _________________________


 Date() Function

The PHP date() function is used to format a time and/or date.

The PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.
Tip A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.

Syntax

date(format,timestamp)

Parameter Description
format Required. Specifies the format of the timestamp
timestamp Optional. Specifies a timestamp. Default is the current date and time

PHP Date() - Format the Date

The required format parameter in the date() function specifies how to format the date/time.
Here are some characters that can be used:
  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
A list of all the characters that can be used in the format parameter, can be found in our PHP Date reference.
Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting:
<?php
echo date("Y/m/d") . "<br />";
echo date("Y.m.d") . "<br />";
echo date("Y-m-d");
?>
The output of the code above could be something like this:
2009/05/11
2009.05.11
2009-05-11

PHP Date() - Adding a Timestamp

The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used.
The mktime() function returns the Unix timestamp for a date.
The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Syntax for mktime()

mktime(hour,minute,second,month,day,year,is_dst)
To go one day in the future we simply add one to the day argument of mktime():
<?php
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
?>
The output of the code above could be something like this:
Tomorrow is 2009/05/12

 _________________________

ليست هناك تعليقات:

إرسال تعليق