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

Most Commonly Used

The document lists and describes some of the most commonly used PHP functions including: 1) Conditional functions like if/elseif/else that create conditional clauses and while that performs actions in a loop as long as the condition is satisfied. 2) Basic functions like echo that prints text, include/require that imports files, isset that checks if a variable exists, and mysql_connect/mysql_close that connect to and close MySQL databases. 3) File handling functions such as fopen that opens files, fread that reads file contents, and fwrite that writes to files. 4) Array functions including count that counts elements and implode/explode to combine and separate string elements

Uploaded by

nasirmail
Copyright
© Attribution Non-Commercial (BY-NC)
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)
15 views

Most Commonly Used

The document lists and describes some of the most commonly used PHP functions including: 1) Conditional functions like if/elseif/else that create conditional clauses and while that performs actions in a loop as long as the condition is satisfied. 2) Basic functions like echo that prints text, include/require that imports files, isset that checks if a variable exists, and mysql_connect/mysql_close that connect to and close MySQL databases. 3) File handling functions such as fopen that opens files, fread that reads file contents, and fwrite that writes to files. 4) Array functions including count that counts elements and implode/explode to combine and separate string elements

Uploaded by

nasirmail
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 2

Most commonly used PHP functions

Last updated October 14, 2009. www.rioleo.org

Function name What it does Example


if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
if, elseif, else Creates a conditional clause. echo "a is equal to b";
} else {
echo "a is smaller than b";
}
Performs a clause as long as the
conditional is satisfied. Remember to $i = 1;
while ($i <= 10) {
while ensure that the “while” condition is
echo $i++; // Increments i
broken at some point or else you will }
have an infinite loop.
echo Prints text. echo “Hello World”;
Puts another file into the specified
Include include “file.php”;
location.
Same as include, but breaks if the file
require require “file.php”;
cannot be evaluated or found.
Checks to see if a variable exists or
isset isset($a);
not. Returns TRUE or FALSE.
Makes a connection to a MySQL
database. Returns true if connected.
mysql_connect $link = mysql_connect(‘localhost’, ‘mysql_user’, ‘mysql_password’);
Use in conjunction with
mysql_close($link);
mysql_close Closes a database connection. mysql_close($link);
mysql_select_db Selects the database to connect to. mysql_select_db($db_database,$link));
Runs a SQL query. Returns true or a
mysql_query resource if successful, false if $result = mysql_query(‘SELECT * WHERE 1=1’);
otherwise.
Gets a single row or rows of data from
mysql_fetch_array $row = mysql_fetch_array($result)
a SQL query resource.
get Gets a URL variable. $name = $_GET[“name”];
post Gets a form variable $name = $_POST[“name”];
$filename = “myfile.txt”;
Opens a file for reading (“r” ) or (“w”)
$ini_handle = fopen($filename, “r”);
fopen writing. Use in conjunction with $ini_contents = fread($ini_handle, filesize($filename));
fclose. fclose($ini_handle);
Reads the contents of a file. Requires a
fread limit of the number of bytes to read, $ini_contents = fread($ini_handle, filesize($filename));
which can be set to the size of the file.
Writes a string to a file. Use in $fp = fopen(‘data.txt’, ‘w’);
fwrite($fp, ‘1’);
fwrite conjunction with fopen and fread. fwrite($fp, ‘23’);
Adds anything to the end of the file. fclose($fp);
$array = array(1, 2, 3, 4, 5);
Creates a list of items that can be
arrays $arr = array(“foo” => “bar”, 12 => true);
ordered.
$array = array(‘lastname’, ‘email’, ‘phone');
Finds the number of elements in an
count $result = count($array);
array
Creates an md5 encrypted
hexadecimal number, useful for
md5 $val = md5($password);
storing passwords. (md5 cannot be
decrypted, only compared)
Splits an array of strings into a string $array = array('lastname', 'email', 'phone');
Implode
with an optional delimiter. $comma_separated = implode(",", $array);
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
Splits a string into an array of strings $pieces = explode(" ", $pizza);
explode // $pieces[0] is now “piece1” and
using a delimiter as a way to divide it.
// $pieces[1] is now “piece2”, etc.
$name = "Me"; // Sender’s name
$email = "[email protected]"; // Sender’s e-mail adress
$recipient = "[email protected]"; // Recipient
Sends an email. The header must $mail_body = "The text for the mail"; // Mail body
mail
contain a “From” $subject = "Subject of the email"; // Subject
$header = "From: ". $name . " <" . $email . ">\r\n";

mail($recipient, $subject, $mail_body, $header);


Stores a variable that can be referred $name = $_SESSION['username']; // Saves the variable into the session as
session to multiple times across different username
pages. // Any other page that refers to $_SESSION['favcolor']; will have $name in it.
session_start Creates or resumes a user session. session_start();
session_destroy Destroys a session. session_destroy();

You might also like