PHP Programming Lecture 6: Programming in PHP | SCK3633 Web Programming |
11b PHP Programming
Transcript of 11b PHP Programming
-
7/31/2019 11b PHP Programming
1/33
PHP Programming: Functions,
Strings, Arrays, Date and Time
John Ryan B. Lorca
Instructor I
1
-
7/31/2019 11b PHP Programming
2/33
Functions
A function is a block of code that can be executedwhenever we need it.
Creating PHP functions: All functions start with the word "function()"
Name the function - It should be possible to understandwhat the function does by
its name. The name can start with a letter or underscore(not a number)
Add a "{" - The function code starts after the opening curlybrace
Insert the function code
Add a "}" - The function is finished by a closing curly brace
2
-
7/31/2019 11b PHP Programming
3/33
Functions
A simple function that writes my name when it iscalled:
3
-
7/31/2019 11b PHP Programming
4/33
Functions
Now we will use the function in a PHP script:
-
7/31/2019 11b PHP Programming
5/33
Functions
The output of the code will be:
Hello world!
My name is Marcus Oliver Graichen.
That's right, Marcus Oliver Graichen is my name.
5
-
7/31/2019 11b PHP Programming
6/33
Functions
Our first function writeMyName() is a verysimple function. It only writes a static string.
To add more functionality to a function, we
can add parameters. A parameter is just like a variable.
You may have noticed the parentheses
(brackets!) after the function name, like: writeMyName(). The parameters are specified
inside the parentheses.
6
-
7/31/2019 11b PHP Programming
7/33
Functions
The following example will write different firstnames, but the same last name:
7
-
7/31/2019 11b PHP Programming
8/33
Functions
The output of the code will be:
My name is John smith.
My name is Sarah Smith.
My name is Smith Smith.
8
-
7/31/2019 11b PHP Programming
9/33
Functions
The following function has two parameters:
9
-
7/31/2019 11b PHP Programming
10/33
Functions
The output of the code will be:My name is John Smith.
My name is Sarah Smith!
My name is Smith Smith...
10
-
7/31/2019 11b PHP Programming
11/33
Functions
Functions can also be used to return values.
11
-
7/31/2019 11b PHP Programming
12/33
Functions
The output of the code will be:1 + 16 = 17
12
-
7/31/2019 11b PHP Programming
13/33
Exercise
Create a function that:
receives two (2) parameters:x(double) and y(double)
Performs the four fundamental operations: Addition
Subtraction
Muliplication
Division Displays result in this format:
x + y =
13
-
7/31/2019 11b PHP Programming
14/33
Strings
String variables are used for values that contains characterstrings.
We are going to look at some of the most common functionsand operators used to
manipulate strings in PHP. After we create a string we can manipulate it. A string can be
used directly in a
function or it can be stored in a variable.
Below, the PHP script assigns the string "Hello World" to a
string variable called $txt:
14
-
7/31/2019 11b PHP Programming
15/33
Strings
The output of the code will be:Hello World
15
-
7/31/2019 11b PHP Programming
16/33
Strings
The Concatenation Operator
There is only one string operator in PHP.
The concatenation operator (.) is used to put two string values together.
To concatenate two variables together, use the dot (.) operator:
If we look at the code above you see that we used the concatenation
operator two times. This is because we had to insert a third string. Between the two string variables we added a string with a single
character, an empty space, to separate the two variables.
16
-
7/31/2019 11b PHP Programming
17/33
Strings
The output of the code will be:
Hello World 1234
17
-
7/31/2019 11b PHP Programming
18/33
Strings
Using the strlen() function
The strlen() function is used to find the length of a string.
Let's find the length of our string "Hello world!":
The output of the code above will be:
12
The length of a string is often used in loops or otherfunctions, when it is important to know when the stringends. (i.e. in a loop, we would want to stop the loop afterthe last character in the string)
18
-
7/31/2019 11b PHP Programming
19/33
Strings
Using the strpos() function
The strpos() function is used to search for a string or character within astring.
If a match is found in the string, this function will return the position ofthe first match.
If no match is found, it will return FALSE. Let's see if we can find the string "world" in our string:
The output of the code above will be:
6
As you see the position of the string "world" in our string is position 6. Thereason that
it is 6, and not 7, is that the first position in the string is 0, and not 1.
19
-
7/31/2019 11b PHP Programming
20/33
Arrays
An array in PHP is actually an ordered map. A
map is a type that maps values to keys. This
type is optimized in several ways, so you can
use it as a real array, or a list (vector),hashtable (which is an implementation of a
map), dictionary, collection, stack, queue and
probably more. Because you can have anotherPHP array as a value, you can also quite easily
simulate trees.
20
-
7/31/2019 11b PHP Programming
21/33
Arrays
Specifying with array()
An array can be created by the array() language-construct. It takes acertain number of comma-separated key => value pairs.
array( [key=>] value , ... ) // keymay be an integer or string // value maybe any value
A keymay be either an integeror a string. If a key is the standardrepresentation of an integer, it will be interpreted as such (i.e. "8"will beinterpreted as 8, while "08"will be interpreted as "08"). Floats in keyaretruncated to integer. There are no different indexed and associative arraytypes in PHP; there is only one array type, which can both contain integerand string indices.
21
-
7/31/2019 11b PHP Programming
22/33
Arrays
A value can be of any PHP type.
If you do not specify a key for a given value, then the maximum of the integerindices is taken, and the new key will be that maximum value + 1. If you specify akey that already has a value assigned to it, that value will be overwritten.
22
-
7/31/2019 11b PHP Programming
23/33
Arrays
Creating/modifying with square-bracket syntax
You can also modify an existing array by explicitly setting values in it.
This is done by assigning values to the array while specifying the key in brackets. You can also omitthe key, add an empty pair of brackets ("[]") to the variable name in that case. $arr[key] = value;$arr[] = value; // keymay be an integer or string // value may be any valueIf$arrdoesn't exist yet,it will be created. So this is also an alternative way to specify an array. To change a certain value,just assign a new value to an element specified with its key. If you want to remove a key/value pair,you need to unset() it.
23
-
7/31/2019 11b PHP Programming
24/33
ArraysNote that the maximum integer key used for this need not currently exist in the array. It simply must have
existed in the array at some time since the last time the array was re-indexed. The following example
illustrates:
24
-
7/31/2019 11b PHP Programming
25/33
Arrays
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array ( )
Array
(
[5] => 6)
Array
(
[0] => 6
[1] => 7
)25
-
7/31/2019 11b PHP Programming
26/33
Exercise
Create two (2) arrays First Array:
Contains letters from A-J
Second Array:
Contains numbers from 1-10 The result must be a third array containing the values
of the first and second array alternatively starting withthe first array value
Example: $arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} $arr2 = {A, B, C, D, E, F, G, H, I, J}
$arr3 = {1, A, 2, B, 3, C, 4, D, 5, E, 6, F, 7, G, 8, H, 9, I, 10, J}
$arr 3 is the result
26
-
7/31/2019 11b PHP Programming
27/33
Date and Time
format character Description Example returned valuesDay --- ---
dDay of the month, 2 digits with
leading zeros01 to 31
DA textual representation of a day,
three lettersMon through Sun
Day of the month without leading
zeros1 to 31
l (lowercase 'L')A full textual representation of
the day of the weekSunday through Saturday
N
ISO-8601 numeric representation
of the day of the week (added in
PHP 5.1.0)
1 (for Monday) through 7(for
Sunday)
SEnglish ordinal suffix for the day
of the month, 2 charactersst, nd, rdor th. Works well withj
wNumeric representation of the
day of the week
0 (for Sunday) through 6(for
Saturday)
z The day of the year (startingfrom 0)
0 through 365 27
-
7/31/2019 11b PHP Programming
28/33
Date and Time
format character Description Example returned values
Week --- ---
W
ISO-8601 week number of year,
weeks starting on Monday (added in
PHP 4.1.0)
Example: 42 (the 42nd week in the
year)
Month --- ---
FA full textual representation of a
month, such as January or MarchJanuary throughDecember
mNumeric representation of a month,
with leading zeros01 through 12
MA short textual representation of a
month, three lettersJan throughDec
nNumeric representation of a month,
without leading zeros1 through 12
t Number of days in the given month 28 through 31 28
-
7/31/2019 11b PHP Programming
29/33
Date and Time
format character Description Example returned values
Year --- ---
L Whether it's a leap year 1 if it is a leap year, 0 otherwise.
o
ISO-8601 year number. This has
the same value as Y, except that
if the ISO week number (W)
belongs to the previous or next
year, that year is used instead.
(added in PHP 5.1.0)
Examples: 1999 or 2003
YA full numeric representation of
a year, 4 digitsExamples: 1999 or 2003
y
A two digit representation of a
year Examples: 99 or 03 29
-
7/31/2019 11b PHP Programming
30/33
Date and Time
format character Description Example returned values
Time --- ---
aLowercase Ante meridiem and
Post meridiemam orpm
AUppercase Ante meridiem and
Post meridiem
AMor PM
B Swatch Internet time 000 through 999
g12-hour format of an hour
without leading zeros1 through 12
G24-hour format of an hour
without leading zeros0 through 23
h12-hour format of an hour with
leading zeros01 through 12
H24-hour format of an hour with
leading zeros00 through 23
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59 30
-
7/31/2019 11b PHP Programming
31/33
Date and Timeformat character Description Example returned values
Timezone --- ---
eTimezone identifier (added in
PHP 5.1.0)
Examples: UTC, GMT,
Atlantic/Azores
I(capital i)Whether or not the date is in
daylight saving time
1 if Daylight Saving Time, 0
otherwise.
O Difference to Greenwich time(GMT) in hours
Example: +0200
P
Difference to Greenwich time
(GMT) with colon between hours
and minutes (added in PHP 5.1.3)
Example: +02:00
T Timezone setting of this machineExamples:EST,MDT...
Z
Timezone offset in seconds. The
offset for timezones west of UTC
is always negative, and for those
east of UTC is always positive.
-43200 through 43200
31
-
7/31/2019 11b PHP Programming
32/33
Date and Time
format character Description Example returned values
Full Date/Time --- ---
cISO 8601 date (added in PHP
5)2004-02-12T15:19:21+00:00
r RFC 2822 formatted dateExample: Thu, 21 Dec 2000
16:01:07 +0200
U
Seconds since the Unix
Epoch (January 1 1970
00:00:00 GMT)
See also time()
32
http://www.faqs.org/rfcs/rfc2822http://c/Users/MythterNote/Desktop/function.time.htmlhttp://c/Users/MythterNote/Desktop/function.time.htmlhttp://www.faqs.org/rfcs/rfc2822 -
7/31/2019 11b PHP Programming
33/33
Exercise
Produce the following formats:
Today is Sunday, July 26, 2009
Today is the 26th day of July, 2009
July 26, 2009 02:00:00 PM 26 July 2009 02:00 pm
26th of July 2009 2:00 PM
07/26/2009 02:00:00 PM
07-26-2009 02:00 pm
20090726-020000PM
33