sangkrit

Free Access

Any mobile device running on android for accessing this homeschool program of internet age SANGKRIT.net, could always get reviewed here if that simplifies the process in the hands of our users.

Contact: System at Sangkrit dot Net

Essential PHP Functions


Ceil()
float ceil (float value)
Rounds up the supplied float value to the next integer and returns the result.

Example:

<?php
$a = 2.444;
echo ceil($a); // 3
?>

Can also be used to check if a number divides evenly with another

Example:

<?php
$num1 = 5;
$num2 = 7;
$div = $num2 / $num1;
if (ceil($div) == $div)
echo “$num2 divides into $num1 evenly.”;
else
echo “$num2 does not divide into $num1 easily ($div).”;
?>

Count()
int count (mixed var)
Returns an integer value of the number of elements in the supplied variable – generally an array, as anything else will return 1.

Example:

<?php
$stuff[0] = 4;
$stuff[1] = 3;
$stuff[2] = 2;
echo count($stuff); // 3
?>

Handling each element of an array:

<?php
$values[] = 5;
$values[] = 3;
$values[] = 6;
$values[] = -1;
$num = count($values);
for ($i=0; $i < $num + 1; $i++) $values[$i] += 2;
?>

This could be done without count() using the foreach case structure.

Die()

void die ([string status])
Terminates the current script execution. If parameter ‘status’ is specified, also displays ‘status’ in an error message.

Example:

<?php
if (!file_get_contents(“stats.txt”)) die(“Could not fetch statistics history.”);
?>

Terminating the script if a database connection cannot be opened:

<?php
mysql_connect(“localhost”,”root”,”") or die(“Could not connect to database server.”);
?>

Echo()
void echo (string outputstring)
Outputs data to the current stream (console, apache request etc.). Is more a language construct than a function, and can therefore be used without the parenthesis.

Example:

<?php
echo(“Some text.”); // Will output ‘Some text.’
echo “Some text.”; // Will output ‘Some text.’ just as above
?>

Example-2:

<?php
if ( $_SESSION['loggedin'] )
{
echo “Welcome, ” . $_SESSION['username'];
}
else
{
echo “Access denied. This area is for members only. Please login.”;
}
?>

Empty()
bool empty (mixed var)
Used to check if a variable is empty. Note: It is used to only check variables, thus empty(trim($var)); will result in a parse error.

Example:

<?php
$var = ‘Hello’;
if (empty($var))
echo ‘The variable is empty’;
else
echo ‘The variable is not empty’; // True in this case
?>

Example-2:

<?php
$name = $_POST['name'];
if (empty($name))
echo ‘You need to enter your name’;
?>

Pitfall
0 is considered empty:

<?php
$var = 0;
if (empty($var))
echo ‘The variable is empty’; // True in this case
else
echo ‘The variable is not empty’;
?>

Exit()
void exit ( [String status] )
void exit ( int status )
Exactly the same as die.

file_get_contents()
String file_get_contents ( String filename [, boolean use_include_path [, resource context [, int offset [, int maxlen]]]] )
Reads a file into a string.

Example:

<?php
echo file_get_contents(‘my_file.txt’);
?>

AND,

<?php
$contents = file_get_contents(‘data.txt’);
echo $contents; // Would output whatever is in the files
?>

file_put_contents()
int file_put_contents ( string filename, mixed data , int flags , resource context )
Writes data to a file. The flags can be FILE_USE_INCLUDE_PATH, FILE_APPEND and/or LOCK_EX.
“This is a PHP5 function only. Will return false on failure, or the number of bytes written to the file.”

Example:

<?php
file_put_contents(‘my_file.txt’, ‘Hello’, FILE_APPEND); // Will append ‘Hello’ to the specified file
?>

Example-2:

<?php
$ip = $_SERVER['REMOTE_ADDR']; // Get client’s IP address
file_put_contents(‘ips.txt’,”n”.$ip, FILE_APPEND);
?>

PHP4 Workaround
This function saves your time (it basically calls fopen, fwrite and fclose). Here is something that can be used for making sure your PHP script works with PHP5 and PHP4:

<?php
if (function_exists(‘file_put_contents’) === false)
{
function file_put_contents($file, $data)
{
$fh = @fopen($file, ‘w’);
if ($fh === false)
return false;
else
{
fwrite($fh, $data);
fclose($fh);
return true;
}
}
}
?>
It is a basic replacement and does not take into account any flags which goes beyond the scope of this document.

getenv()
string getenv ( string varname)
Returns the value of an environment variable

Example:

<?php
echo getenv(‘HTTP_USER_AGENT’); //will output what web browser is currently viewing the page
?>

Allows you to execute commands based on server information:

<?php
if (strpos(getenv(‘HTTP_USER_AGENT’), “Mozilla”) > 0)
{
// Execute Mozilla specific code
}
?>

header()
void header (string string [, bool replace [, int http_response_code]] ) Updates the HTTP headers that are sent to the web browser

Example:

<?php
header(“Cache-Control: no-cache”);
?>

One use for the header function is to redirect the browser to a different web site:

<?php
if ($_POST['username'] == “”)
{
header(“Location: http://my.url.com”);
exit;
}
// Redirects the browser to http://my.url.com
?>

Another is to change how the browser will interpret the data that is sent to it:

<?phpb
header(“Content-Type: text/xml”)
echo ‘<?xml version=”1.0″ encoding=”ISO-8859-1″ ?>’;
// Rest of XML output
?>


Leave a Reply