0% found this document useful (0 votes)
39 views11 pages

WBDV Midterm Reviewer

Copyright
© © All Rights Reserved
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)
39 views11 pages

WBDV Midterm Reviewer

Copyright
© © All Rights Reserved
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/ 11

WEEK 7: PHP Methods and functions Information sent from a form with the GET method is

visible to everyone (all variable names and values are


SIMPLE HTML FORM HANDLING displayed in the URL). GET also has limits on the
amount of information to send. The limitation is about
The example below displays a simple HTML form with 2000 characters. However, because the variables are
two input fields and a submit button: displayed in the URL, it is possible to bookmark the
page. This can be useful in some cases.

GET may be used for sending non-sensitive data.

WHEN TO USE POST?

FORM HANDLING

Information sent from a form with the POST method is


invisible to others (all names/values are embedded
within the body of the HTTP request) and has no limits
on the amount of information to send.

Moreover POST supports advanced functionality such


as support for multi-part binary input while uploading
files to server. However, because the variables are not
displayed in the URL, it is not possible to bookmark the
page.
GET VS POST
FORM VALIDATION
Both GET and POST create an array (e.g. array( key =>
value, key2 => value2, key3 => value3, ...)). This array The HTML form we will be working at in these chapters,
holds key/value pairs, where keys are the names of the contains various input fields: required and optional text
form controls and values are the input data from the fields, radio buttons, and a submit button:
user.

FORM HANDLING

Both GET and POST are treated as $_GET and $_POST.


These are superglobals, which means that they are
always accessible, regardless of scope - and you can
access them from any function, class or file without
having to do anything special.

GET VS POST

FORM HANDLING TEXT FIELDS

$_GET is an array of variables passed to the current FORM VALIDATION


script via the URL parameters.
The name, email, and website fields are text input
$_POST is an array of variables passed to the current elements, and the comment field is a textarea. The
script via the HTTP POST method. HTML code looks like this:

WHEN TO USE GET?

FORM HANDLING
RADIO BUTTONS RETURN TYPE ARGUMENTS

FORM VALIDATION PHP RETURN TYPE ARGUMENTS

The gender fields are radio buttons and the HTML code PHP 7 also supports Type Declarations for
looks like this: the return statement. Like with the type declaration for
function arguments, by enabling the strict requirement,
it will throw a "Fatal Error" on a type mismatch.
To declare a type for the function return, add a colon
( : ) and the type right before the opening curly
( { )bracket when declaring the function.
USER DEFINE FUNCTION In the following example we specify the return type for
the function:
PHP FUNCTION

Besides the built-in PHP functions, it is possible to


create your own functions.
A function is a block of statements that can be used
repeatedly in a program.
A function will not execute automatically when a page
loads.
A function will be executed by a call to the function WEEK 8: PHP Date functions
A user-defined function declaration starts with the
word function: Date() function

Syntax The date() function formats a local date and time, and
returns the formatted date string

PARAMETER VALUES

FUNCTION ARGUMENTS Parameter (format)


● Required. Specifies the format of the outputted date
PHP FUNCTION ARGUMENTS string. The following characters can be used:d - The
day of the month (from 01 to 31)
Information can be passed to functions through ● D - A textual representation of a day (three letters)
arguments. An argument is just like a variable. ● j - The day of the month without leading zeros (1 to
Arguments are specified after the function name, inside 31)
the parentheses. You can add as many arguments as ● l (lowercase 'L') - A full textual representation of a
you want, just separate them with a comma. day
● N - The ISO-8601 numeric representation of a day (1
The following example has a function with one for Monday, 7 for Sunday)
argument ($fname). When the familyName() function is ● S - The English ordinal suffix for the day of the month
called, we also pass along a name (e.g. Jani), and the (2 characters st, nd, rd or th. Works well with j)
name is used inside the function, which outputs several ● w - A numeric representation of the day (0 for
different first names, but an equal last name: Sunday, 6 for Saturday)
● z - The day of the year (from 0 through 365)
● W - The ISO-8601 week number of year (weeks
starting on Monday)
● F - A full textual representation of a month (January
through December)
● m - A numeric representation of a month (from 01 to
12)
● M - A short textual representation of a month (three
letters)
● n - A numeric representation of a month, without
leading zeros (1 to 12)  DATE_W3C - World Wide Web Consortium
● t - The number of days in the given month (example: 2013-04-12T15:52:01+00:00)
● L - Whether it's a leap year (1 if it is a leap year, 0
otherwise) checkdate() function
● o - The ISO-8601 year number
The checkdate() function is used to validate a
Y - A four digit representation of a year Gregorian date
y - A two digit representation of a year
a - Lowercase am or pm <?php
A - Uppercase AM or PM var_dump(checkdate(12,31,-400));
B - Swatch Internet time (000 to 999) echo "<br>";
g - 12-hour format of an hour (1 to 12) var_dump(checkdate(2,29,2003));
G - 24-hour format of an hour (0 to 23) echo "<br>";
h - 12-hour format of an hour (01 to 12) var_dump(checkdate(2,29,2004));
H - 24-hour format of an hour (00 to 23) ?>
i - Minutes with leading zeros (00 to 59)
s - Seconds, with leading zeros (00 to 59)
u - Microseconds (added in PHP 5.2.2)
e - The timezone identifier (Examples: UTC, GMT,
Atlantic/Azores)
I (capital i) - Whether the date is in daylights savings
time (1 if Daylight Savings Time, 0 otherwise)
O - Difference to Greenwich time (GMT) in hours
(Example: +0100)
P - Difference to Greenwich time (GMT) in
hours:minutes (added in PHP 5.1.3)
Date_add() function
T - Timezone abbreviations (Examples: EST, MDT)
Z - Timezone offset in seconds. The offset for
The date_add() function adds some days, months, years,
timezones west of UTC is negative (-43200 to 50400)
hours, minutes, and seconds to a date.
c - The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
r - The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013
12:01:05 +0200)
U - The seconds since the Unix Epoch (January 1 1970
00:00:00 GMT)

 DATE_ATOM - Atom (example: 2013-04-


12T15:52:01+00:00)
 DATE_COOKIE - HTTP Cookies (example: Friday,
date_create_from_format() function
12-Apr-13 15:52:01 UTC)
 DATE_ISO8601 - ISO-8601 (example: 2013-04-
The date_create_from_format() function returns a new
12T15:52:01+0000)
DateTime object formatted according to the specified
 DATE_RFC822 - RFC 822 (example: Fri, 12 Apr
format.
13 15:52:01 +0000)
 DATE_RFC850 - RFC 850 (example: Friday, 12-
date_create() function
Apr-13 15:52:01 UTC)
 DATE_RFC1036 - RFC 1036 (example: Fri, 12
The date_create() function returns a new DateTime
Apr 13 15:52:01 +0000)
object.
 DATE_RFC1123 - RFC 1123 (example: Fri, 12
Apr 2013 15:52:01 +0000)
 DATE_RFC2822 - RFC 2822 (Fri, 12 Apr 2013
15:52:01 +0000)
 DATE_RFC3339 - Same as DATE_ATOM (since
PHP 5.1.3)
 DATE_RSS - RSS (Fri, 12 Aug 2013 15:52:01
+0000)
date_create() function echo date_format($date,"Y/m/d H:i:s");
The date_create() function returns a new DateTime ?>
object.
date() function
<?php
$date=date_create("2013-03-15"); The date() function formats a local date and time, and
echo date_format($date,"Y/m/d"); returns the formatted date string.
?>
<?php
// Prints the day
echo date("l") . "<br>";

// Prints the day, date, month, year, time, AM or PM


echo date("l jS \of F Y h:i:s A");
date_default_timezone_get() function ?>
The date_default_timezone_get() function returns the
default timezone used by all date/time functions in the getdate() function
script.
The getdate() function returns date/time information of
<?php a timestamp or the current local date/time.
date_default_timezone_set("Asia/Bangkok");
echo date_default_timezone_get(); <?php
?> print_r(getdate());
?>

Date_diff() function

The date_diff() function returns the difference between


two DateTime objects.
idate() function
<?php
$date1=date_create("2013-03-15"); The idate() function formats a local time and/or date as
$date2=date_create("2013-12-12"); integer.
$diff=date_diff($date1,$date2);
?> "; echo idate("d") . "
"; echo idate("h") . "
"; echo idate("H") . "
"; echo idate("i") . "
"; echo idate("I") . "
"; echo idate("L") . "
"; echo idate("m") . "
"; echo idate("s") . "
"; echo idate("t") . "
date_format() function "; echo idate("U") . "
"; echo idate("w") . "
The date_format() function returns a date formatted ";?>
according to the specified format.
localtime() function
<?php
$date=date_create("2013-03-15"); The localtime() function returns the local time.
<?php
print_r(localtime());
echo "<br><br>";
print_r(localtime(time(),true));
?>

mktime() function

The gmmktime() function returns the Unix timestamp


for a date.

<?php
// Prints: October 3, 1975 was on a Friday
echo "Oct 3, 1975 was on a ".date("l",
mktime(0,0,0,10,3,1975));
?>

strftime() function

The strftime() function formats a local time and/or date


according to locale settings.

<?php
echo(strftime("%B %d %Y, %X %Z",mktime(20,0,0,12,31,
98))."<br>");
setlocale(LC_ALL,"hu_HU.UTF8");
echo(strftime("%Y. %B %d. %A. %X %Z"));
?>

localtime() function

The localtime() function returns the local time.

<?php
print_r(localtime());
echo "<br><br>";
print_r(localtime(time(),true));
?>

WEEK 9: PHP String functions

string() function
The PHP string functions are part of the PHP core. No
installation is required to use these functions
If you have a list of items (a list of courses, for example),
storing the course in single variables could look like
this:

$courses1="BSIT";$courses2="BSHRM";$courses3="ACT";

How to Create Function

WHAT IS ARRAY?

However, what if you want to loop through the list of


courses and find a specific one? And what if you had
not 3 courses, but 10?

The solution is to create an array!

An array can hold many values under a single name,


and you can access the values by referring to an index
number.

ARRAY

CREATING AN ARRAY

In PHP, the array() function is used to create an array:

array();

In PHP, there are three types of arrays:

 Indexed arrays - Arrays with numeric index


 Associative arrays - Arrays with named keys
 Multidimensional arrays - Arrays containing
one or more arrays

INDEX ARRAY

The index can be assigned automatically (index always


starts at 0):

$courses=array("BSIT","BSHRM","ACT");

or the index can be assigned manually:


WEEK 10: Illustration of Array
$cars[0]="Volvo";$cars[1]="BMW";$cars[2]="Toyota";
 Associative Array
INDEX ARRAY
 Multidimensional Array
The following example creates an indexed array named
How to Create Array $courses, assigns three elements to it, and then prints a
text containing the array values:
WHAT IS ARRAY?
<?php$courses=array("BSIT","BSHRM","ACT");echo "List
An array is a special variable, which can hold more than of courses " . $courses[0] . ", " . $courses[1] . "
and " . $courses[2] . ".";?>
one value at a time.
INDEX ARRAY - Looping
Example:
To loop through and print all the values of an indexed
<?phpecho $cars[0][0].": In stock: ".$cars[0][1].",
array, you could use a for loop, like this: sold: ".$cars[0][2].". <br>";echo $cars[1][0].": In
stock: ".$cars[1][1].", sold: ".$cars[1][2].".
<?php$courses=array("BSIT","BSHRM","ACT");$arrlength <br>";echo $cars[2][0].": In stock: ".$cars[2][1].",
=count($courses); sold: ".$cars[2][2].". <br>";echo $cars[3][0].": In
for($x=0;$x<$arrlength;$x++) stock: ".$cars[3][1].", sold: ".$cars[3][2].".
{ <br>";?>
echo $courses[$x];
echo "<br>";
}?> WEEK 10: Illustration of Array
The array functions allow you to access and manipulate
ASSOCIATIVE ARRAY arrays. Simple and multi-dimensional arrays are
supported.
Associative arrays are arrays that use named keys that The array functions are part of the PHP core. There is no
you assign to them. installation needed to use these functions.

There are two ways to create an associative array: <?php


$courses=array(“BSIT",“BSHRM",“ACT");
$age=array("John"=>"25","Fred"=>"27","Max"=>"63");
$age['John']="25";$age['Fred']="27";$age['Max']="63"; echo count($courses);
?>
ASSOCIATIVE ARRAY

Example

<?php$age=array("John"=>"25","Fred"=>"27","Max"=>"63
");echo "Peter is " . $age['Peter'] . " years
old.";?>

LOOP THROUGH ASSOCIATIVE ARRAY

To loop through and print all the values of an


associative array, you could use a foreach loop, like this:

<?php$age=array("John"=>"25","Fred"=>"27","Max"=>"63
");foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}?>

MULTIDIMENSIONAL ARRAY

A multidimensional array is an array containing one or


more arrays.

PHP supports multidimensional arrays that are two,


three, four, five, or more levels deep. However, arrays
more than three levels deep are hard to manage for
most people.

The dimension of an array indicates the number of


indices you need to select an element.

 For a two-dimensional array you need two


indices to select an element
 For a three-dimensional array you need three
indices to select an element
array(
'id' => 5698,
'first_name' => 'Peter',
'last_name' => 'Griffin', ),
array(
'id' => 4767,
'first_name' => 'Ben',
'last_name' => 'Smith', )
);
$last_names = array_column($a, 'last_name');
print_r($last_names);
?>

Array change key case Array combine


Changes all keys in an array to lowercase or uppercase The array_combine() function creates an array by using
the elements from one "keys" array and one "values"
<?php array.
$age=array(“student1"=>“90",“student2"=>“87",“studen
t3"=>“93"); <?php
print_r(array_change_key_case($age,CASE_UPPER)); $fname=array("Peter","Ben","Joe");
?> $age=array("35","37","43");

$c=array_combine($fname,$age);
print_r($c);
?>

Array chunk function


The array_chunk() function splits an array into chunks of
new arrays
Array count
<?php
The array_count_values() function counts all the values
$cars=array("Volvo","BMW","Toyota","Honda","Merced
of an array
es","Opel");
print_r(array_chunk($cars,2));
<?php
?>
$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a));
?>

Array column Array diff


The array_column() function returns the values from a The array_diff() function compares the values of two (or
single column in the input array. more) arrays, and returns the differences.
This function compares the values of two (or more)
<?php arrays, and return an array that contains the entries
$a = array( from array1 that are not present in array2 or array3, etc.
<?php Array merge
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"y The array_merge() function merges one or more arrays
ellow"); into one array.
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
<?php
$result=array_diff($a1,$a2); $a1=array("red","green");
print_r($result); $a2=array("blue","yellow");
?> print_r(array_merge($a1,$a2));
?>
Array intersect
he array_intersect() function compares the values of two
(or more) arrays, and returns the matches.
This function compares the values of two or more arrays,
and return an array that contains the entries
from array1 that are present in array2, array3, etc.

<?php Array multisort


$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"y The array_multisort() function returns a sorted array.
ellow"); You can assign one or more arrays. The function sorts
$a2=array("e"=>"red","f"=>"green","g"=>"blue"); the first array, and the other arrays follow, then, if two
or more values are the same, it sorts the next array, and
$result=array_intersect($a1,$a2); so on.
print_r($result);
?> <?php
$a=array("Dog","Cat","Horse","Bear","Zebra");
Array key array_multisort($a);
The array_key_exists() function checks an array for a print_r($a);
specified key, and returns true if the key exists and false ?>
if the key does not exist.
ARRAY
<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5"); PHP - SORT FUNCTIONS FOR ARRAYS
if (array_key_exists("Volvo",$a)) The elements in an array can be sorted in alphabetical or
{ echo "Key exists!";} numerical order, descending or ascending.
else
{echo "Key does not exist!";} sort() - sort arrays in ascending order
?> rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order,
according to the value
ksort() - sort associative arrays in ascending order,
according to the key
arsort() - sort associative arrays in descending order,
according to the value
krsort() - sort associative arrays in descending order,
Array map according to the key
The array_map() function sends each value of an array to
a user-made function, and returns an array with new SORT ARRAY IN ASCENDING ORDER - SORT()
values, given by the user-made function. The following example sorts the elements of the $letters
array in ascending alphabetical order:
<?php
function myfunction($v) <?php
{ $letters=array(“C",“W",“A");
return($v*$v); sort($letters);
} ?>
$a=array(1,2,3,4,5);
print_r(array_map("myfunction",$a)); The following example sorts the elements of the $grade
?> array in ascending numerical order:
<?php SORT ARRAY IN DESCENDING ORDER
$grade=array(86,78,66,75,98); ACCORDING TO KEY – ksort()
sort($grade); The following example sorts an associative array in
?> descending order, according to the key:

SORT ARRAY IN DESCENDING ORDER - RSORT() <?php


The following example sorts the elements of the $letters $score=array(“Math"=>“55",“English"=>“100",“FIL"=>“6
array in descending alphabetical order: 5");
krsort($score);
<?php ?>
$letters=array(“C",“W",“A");
rsort($letters);
?>

The following example sorts the elements of the $grade


array in descending numerical order:

<?php
$grade=array(86,78,66,75,98);
rsort($grade);
?>

SORT ARRAY IN ASCENDING ORDER ACCORDING TO


VALUE – asort()
The following example sorts an associative array in
ascending order, according to the value:

<?php
$score=array(“Math"=>“55",“English"=>“100",“FIL"=>“6
5");
asort($score);
?>

SORT ARRAY IN ASCENDING ORDER


ACCORDING TO KEY – ksort()
The following example sorts an associative array in
ascending order, according to the key:

<?php
$score=array(“Math"=>“55",“English"=>“100",“FIL"=>“6
5");
ksort($score);
?>

SORT ARRAY IN DESCENDING ORDER


ACCORDING TO VALUE – asort()
The following example sorts an associative array in
descending order, according to the value:

<?php
$score=array(“Math"=>“55",“English"=>“100",“FIL"=>“6
5");
arsort($score);
?>

You might also like