PHP programming - Functions
Frank Yap

In this section we will look at the useful functions of PHP.
 
Resources

 
Lab Exercises

The following are some useful functions.
 
Exercise 1: date() function

date() function - Returns a formatted date/time string.
To obtain correct time the date_default_timezone_set() function must be called to set time zone.
 
<?php date_default_timezone_set("America/New_York"); ?> 
<p>Today is <b><?php echo date("F jS, Y"); ?></b>.</p>

Today is November 18th, 2017.


<?php date_default_timezone_set("America/New_York"); ?> 
<p>Current time is <b><?php echo date("g:ia"); ?></b>.</p>

Current time is 8:51am.

 
date() function parameter details is at:
http://php.net/manual/en/function.date.php
 
Exercise 2: time() function

time() function - Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
 
<?php date_default_timezone_set("America/New_York"); ?> 
<p>Current time in seconds since January 1 1970 00:00:00 GMT is <b><?php echo time(); ?></b>.</p>

Current time in seconds since January 1 1970 00:00:00 GMT is 1511013088.


Exercise 3: explode() function

explode() function - Splits a string by string.
Returns an array of sub-strings formed by splitting.
 
$filePath = "images/Last trip/20120801/img0384.jpg"; 
$dirs = explode ("/", $filePath); 
$count = count ($dirs); 
for ($i = 0; $i < $count; $i++) { 
echo $dirs [$i] . "<br>";
Output ---
images
Last trip
20120801
img0384.jpg
 
Details of explode() function is at:
http://php.net/manual/en/function.explode.php


$filePath = "images\Last trip\20120801\img0384.jpg"; 
$dirs = explode ("\", $filePath); 
$count = count ($dirs); 
for ($i = 0; $i < $count; $i++) { 
echo $dirs [$i] . "<br>"; 
}
Output ---
images
Last trip
20120801
img0384.jpg


When path delimiter is unknown:

$filePath = "images\Last trip/20120801\img0384.jpg"; 
$filePath = str_replace ("\", "/", $filePath); 
$dirs = explode ("/", $filePath); 
$count = count ($dirs); 
for ($i = 0; $i < $count; $i++) { 
echo $dirs [$i] . "<br>"; 
}
Output ---
images
Last trip
20120801
img0384.jpg
 
Exercise 4: implode() function

implode() function - Joins array elements with a glue string.
 
$dirs = array ( 
"images", 
"Last trip", 
"20120801", 
"img0384.jpg" 
); 
 
$filePath = implode ("/", $dirs); // (glue string, array) 
echo $filePath . "<br>";
Output ---
images/Last trip/20120801/img0384.jpg
 
Exercise 5: strpos() function


strpos() function - Finds the position of the first occurrence of a substring in a string.
 
$mystring = 'abc'; 
$findme = 'a'; 
$pos = strpos($mystring, $findme); // (long-string, sub-string) 
 
// Note our use of ===. Simply == would not work as expected 
// because the position of 'a' was the 0th (first) character. 
if ($pos === false) { 
echo "The string '$findme' was not found in the string '$mystring'"; 
} else { 
echo "The string '$findme' was found in the string '$mystring'"; 
echo " and exists at position $pos"; 
}
Output ---
The string 'a' was found in the string 'abc' and exists at position 0



$cart = "<ORDER_CART><ORDER><NAME>Pants</NAME><SIZE>12</SIZE><COLOR>Blue</COLOR></ORDER>..."; 
$orderTag = "<ORDER>"; 
$orderEndTag = "</ORDER>"; 
$posEnd = 0; 
$posStart = $posEnd; 
$posStart = strpos ($cart, $orderTag, $posStart); // (string, sub-string, start index) 
 
if ($posStart !== false) { 
$posEnd = strpos ($cart, $orderEndTag, $posStart); 
if ($posEnd !== false) { 
$posEnd = $posEnd + strlen ($orderEndTag); 
$strOrder = substr ($cart, $posStart, ($posEnd-$posStart)); 
echo htmlentities ($strOrder) . "<br>"; 

}
Output ---
<ORDER><NAME>Pants</NAME><SIZE>12</SIZE><COLOR>Blue</COLOR></ORDER>
 
Exercise 6: stripos() function

stripos() function
stripos() is case insensitive.

$findme    = 'a'; 
$mystring1 = 'xyz'; 
$mystring2 = 'ABC'; 
 
$pos1 = stripos($mystring1, $findme); 
$pos2 = stripos($mystring2, $findme); 
 
// Nope, 'a' is certainly not in 'xyz' 
if ($pos1 === false) { 
echo "The string '$findme' was not found in the string '$mystring1'"; 

 
// Note our use of ===. Simply == would not work as expected 
// because the position of 'a' is the 0th (first) character. 
if ($pos2 !== false) { 
echo "We found '$findme' in '$mystring2' at position $pos2"; 
}
Output ---
The string 'a' was not found in the string 'xyz'
We found 'a' in 'ABC' at position 0
 
Exercise 7: trim() function

trim() function - Strips whitespace (or specified characters) from the beginning and end of a string.
 
echo "1<br>"; 
$text = "\t\tThese are a few words :) ... "; 
var_dump($text); 
$trimmed = trim($text); 
var_dump($trimmed); 
 
echo "2<br>"; 
var_dump($text); 
$trimmed = trim($text, " \t."); 
var_dump($trimmed); 
 
echo "3<br>"; 
$hello = "Hello World"; 
var_dump($hello); 
$trimmed = trim($hello, "Hdle"); 
var_dump($trimmed); 
 
echo "4<br>"; 
var_dump($hello); 
$trimmed = trim($hello, 'HdWr'); 
var_dump($trimmed); 
 
// trim the ASCII control characters at the beginning and end of $binary 
// (from 0 to 31 inclusive) 
echo "5<br>"; 
$binary = "\x09Example string\x0A"; 
var_dump($binary); 
$clean = trim($binary, "\x00..\x1F"); 
var_dump($clean);
Output ---
1
string(32) " These are a few words :) ... " string(28) "These are a few words :) ..." 2
string(32) " These are a few words :) ... " string(24) "These are a few words :)" 3
string(11) "Hello World" string(5) "o Wor" 4
string(11) "Hello World" string(9) "ello Worl" 5
string(16) " Example string " string(14) "Example string"

function trim_value(&$value)  
{  
$value = trim($value);  

 
$fruit = array('apple','banana ', ' cranberry '); 
var_dump($fruit); 
 
array_walk($fruit, 'trim_value'); 
var_dump($fruit);
Output ---
array(3) { [0]=> string(5) "apple" [1]=> string(7) "banana " [2]=> string(11) " cranberry " } array(3) { [0]=> string(5) "apple" [1]=> string(6) "banana" [2]=> string(9) "cranberry" }
 
Exercise 8: glob() function

glob() function - Finds pathnames matching a pattern.
 
$imgDir= "images"; 
 
$str = "$imgDir/{*.jpg,*.jpeg,*.gif,*.png,*.JPG,*.JPEG,*.GIF,*.PNG}"; 
 
$pictures = glob($str, GLOB_BRACE); 
$files = count($pictures); 
 
$begin = 0; 
$n = $files; 
if ($n > 4) $n = 4; 
$list = ""; 
for ($i = $begin; $i < $n; $i++) { 
if ($i == 0) 
$list .= $pictures[$i]; 
else 
$list .= "," . $pictures[$i]; 

echo $list;
Output ---
images/MB900427668.JPG,images/MB900427704.JPG,images/MB900427709.JPG,images/MB900427785.JPG
 
Details of glob() function is at:
http://www.php.net/manual/en/function.glob.php
 
Homework Exercise

Start XAMPP Apache server.
 
Create "images2" directory in your project folder and store several image files in the folder.
Using glob() function get the list of the image files.
And display images of the files instead of printing the list.
 
Submit the code. Do not submit the image files.
The code would not work here when you have submitted, but that is OK.