Common Functions Frequently Used In PHP
STRING FUNCTIONS
String functions are used to manipulate strings for various needs. Here will be discussed some string functions that are often used in creating a web application program.
AddSlashes
Used to add a backslash(\) character to a string. It is important to use the query string for the database, for example in MySQL. Some of the characters to which the backsearches are added are the quotes of one('), double quoted characters("), backslash(\) and NULL characters.
Syntax:
Addslashes(string)
StripSlashes
Used to remove the backslash(\) character on a string.
Syntax:
String stripslashes(string)
Crypt
Used to encrypt the DES method of a string. This function is often used to shuffle password strings before they are stored in the database. In the use of this crypt function can be added string parameter 'salt'. This 'salt' parameter is added to determine the random basis. The 'Salt' string consists of 2 characters. If the 'salt' string is not added to the crypt function then PHP will determine itself 'salt' the string randomly.
Syntax:
Crypt(string [, salt])
Echo
Used to print the contents of a string or argument.
Syntax:
Echo(string argument1, string argument2, ....)
Explode
Used to break up a string based on a specific separator and insert the result into a variable array.
Syntax:
Explode(string separator, string [, int limit])
Example:
$namahari = "Monday monday on Wednesday thursday saturday";
$days = explode("", $namahari);
Implode
The usefulness of this function is the reverse of the explode function. The implode function is used to generate a string of each element of an array. The resulting string is separated by a predefined string.
Syntax:
Implode(string separator, array)
Strip_Tags
Used to remove HTML tag codes on a string.
Syntax:
Striptags(string [, string tags not omitted])
StrLen
Used to count the number of characters of a string.
Syntax:
Strlen(string)
StrPos
Used to find the first position of a sub string on a string. This function is usually used to search for a sub string within a string.
Syntax:
Strlen(string, sub string)
Str_Repeat
Used to repeat the contents of a string.
Syntax:
Str_repeat(string, int loop count)
StrToLower
Used to convert a string into lowercase.
Syntax:
Strtolower(string)
StrToUpper
Used to convert a string into uppercase(uppercase)
Syntax:
Strtoupper(string)
SubStr
Used to retrieve a sub string with a certain length of a string in a certain position as well.
Syntax:
Substr(string, int position, int position)
Example:
Substr("abcdefg", 0.3); // produces string "abc" substr("abcdefg", 3,2); // generate string "de"
SubStr_Count
Used to count the number of sub strings in a string
Syntax:
Substr_count(string, substring string)
Example:
Substr_count("This is a test", "is"); // returns a value of 2
UCFirst
Used to replace the first character on a string to uppercase.
Syntax:
Ucfirst(string)
UCWords
Used to replace the first character of each word in a string to uppercase.
Syntax:
Ucwords(string)
FUNCTION DATE
Used to retrieve date and time. The result of this function is a string containing the date / time according to the desired format. The known formats in this date function are as follows:
a - "am" or "pm"
A - "AM" or "PM"
B - Swatch Internet time
d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"
D - day of the week, textual, 3 letters; i.e. "Fri"
F - month, textual, long; i.e. "January"
g - hour, 12-hour format without leading zeros; i.e. "1" to "12"
G - hour, 24-hour format without leading zeros; i.e. "0" to "23"
h - hour, 12-hour format; i.e. "01" to "12"
H - hour, 24-hour format; i.e. "00" to "23"
i - minutes; i.e. "00" to "59"
I (capital i) - "1" if Daylight Savings Time, "0" otherwise.
j - day of the month without leading zeros; i.e. "1" to "31"
l (lowercase 'L') - day of the week, textual, long; i.e. "Friday"
L - boolean for whether it is a leap year; i.e. "0" or "1"
m - month; i.e. "01" to "12"
M - month, textual, 3 letters; i.e. "Jan"
n - month without leading zeros; i.e. "1" to "12"
s - seconds; i.e. "00" to "59"
S - English ordinal suffix, textual, 2 characters; i.e. "th", "nd"
t - number of days in the given month; i.e. "28" to "31"
T - Timezone setting of this machine; i.e. "MDT"
U - seconds since the epoch
w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
Y - year, 4 digits; i.e. "1999"
y - year, 2 digits; i.e. "99"
z - day of the year; i.e. "0" to "365"
Z - timezone offset in seconds (i.e. "-43200" to "43200")
Syntax:
date(string format)
Example:
Date("Y-m-d"); // generates "2001-07-28" date("l, j F Y"); // generate "Saturday, 28 July 2001" date("H: i: s"); // generate "20:15:07"
FUNCTION MAIL
Used to send e-mail to a specific e-mail address.
Syntax:
Mail(destination string, string subject, string contents [, string header]);
Example:
$sender = "From: me@email.com";
$to = "qody@gmail.com";
$subject = "Notice";
$content = "This is an experiment of sending e-mail using PHP";
mail($to, $subject, $content, $sender);
No comments :
Post a Comment