SlideShare a Scribd company logo
1 of 21
Arrays
Strings and regular expressions
Basic PHP Syntax
CS380
1
Arrays
2
 Append: use bracket notation without specifying an
index
 Element type is not specified; can mix types
$name = array(); # create
$name = array(value0, value1, ..., valueN);
$name[index] # get element value
$name[index] = value; # set element value
$name[] = value; # append PHP
CS380
$a = array(); # empty array (length 0)
$a[0] = 23; # stores 23 at index 0 (length 1)
$a2 = array("some", "strings", "in", "an", "array");
$a2[] = "Ooh!"; # add string to end (at index 5)
PHP
Array functions
3
function name(s) description
count
number of elements in the
array
print_r print array's contents
array_pop, array_push,
array_shift, array_unshift
using array as a stack/queue
in_array, array_search,
array_reverse,
sort, rsort, shuffle
searching and reordering
array_fill, array_merge,
array_intersect,
array_diff, array_slice, range
creating, filling, filtering
array_sum, array_product,
array_unique, processing elements
Array function example
4
 the array in PHP replaces many other collections in Java
 list, stack, queue, set, map, ...
$tas = array("MD", "BH", "KK", "HM", "JP");
for ($i = 0; $i < count($tas); $i++) {
$tas[$i] = strtolower($tas[$i]);
}
$morgan = array_shift($tas);
array_pop($tas);
array_push($tas, "ms");
array_reverse($tas);
sort($tas);
$best = array_slice($tas, 1, 2);
PHP
CS380
foreach loop
5
foreach ($array as $variableName) {
...
} PHP
CS380
$fellowship = array(“Frodo", “Sam", “Gandalf",
“Strider", “Gimli", “Legolas", “Boromir");
print “The fellowship of the ring members are: n";
for ($i = 0; $i < count($fellowship); $i++) {
print "{$fellowship[$i]}n";
}
print “The fellowship of the ring members are: n";
foreach ($fellowship as $fellow) {
print "$fellown";
} PHP
Multidimensional Arrays
6
<?php $AmazonProducts = array( array(“BOOK",
"Books", 50),
array("DVDs",
“Movies", 15),
array(“CDs", “Music",
20)
);
for ($row = 0; $row < 3; $row++) {
for ($column = 0; $column < 3; $column++) { ?>
<p> | <?=
$AmazonProducts[$row][$column] ?>
<?php } ?>
</p>
<?php } ?>
PHP
CS380
Multidimensional Arrays (cont.)
7
<?php $AmazonProducts = array( array(“Code” =>“BOOK",
“Description” => "Books", “Price” => 50),
array(“Code” => "DVDs",
“Description” => “Movies", “Price” => 15),
array(“Code” => “CDs",
“Description” => “Music", “Price” => 20)
);
for ($row = 0; $row < 3; $row++) { ?>
<p> | <?= $AmazonProducts[$row][“Code”] ?> | <?=
$AmazonProducts[$row][“Description”] ?> | <?=
$AmazonProducts[$row][“Price”] ?>
</p>
<?php } ?>
PHP
CS380
String compare functions
8
Name Function
strcmp compareTo
strstr, strchr
find string/char within a
string
strpos
find numerical position of
string
str_replace, substr_replace replace string Comparison can be:
 Partial matches
 Others
 Variations with non case sensitive functions
 strcasecmp
String compare functions
examples9
$offensive = array( offensive word1, offensive
word2);
$feedback = str_replace($offcolor, “%!@*”,
$feedback);
PHP
CS380
$test = “Hello World! n”;
print strpos($test, “o”);
print strpos($test, “o”, 5); PHP
$toaddress = “feedback@example.com”;
if(strstr($feedback, “shop”)
$toaddress = “shop@example.com”;
else if(strstr($feedback, “delivery”)
$toaddress = “fulfillment@example.com”;
PHP
Regular expressions
10
[a-z]at #cat, rat, bat…
[aeiou]
[a-zA-Z]
[^a-z] #not a-z
[[:alnum:]]+ #at least one alphanumeric char
(very) *large #large, very very very large…
(very){1, 3} #counting “very” up to 3
^bob #bob at the beginning
com$ #com at the end PHPRegExp
 Regular expression: a pattern in a piece of text
 PHP has:
 POSIX
 Perl regular expressions
CS380
Embedded PHP11
CS380
Printing HTML tags in PHP =
bad style12
<?php
print "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.1//EN"n";
print "
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">n";
print "<html xmlns="http://www.w3.org/1999/xhtml">n";
print " <head>n";
print " <title>Geneva's web page</title>n";
...
for ($i = 1; $i <= 10; $i++) {
print "<p> I can count to $i! </p>n";
}
?> HTML
 best PHP style is to minimize print/echo statements in
embedded PHP code
 but without print, how do we insert dynamic content into
the page?
PHP expression blocks
13
 PHP expression block: a small piece of PHP that
evaluates and embeds an expression's value into HTML
 <?= expression ?> is equivalent to:
<?= expression ?> PHP
CS380
<h2> The answer is <?= 6 * 7 ?> </h2>
PHP
The answer is 42
output
<?php print expression; ?>
PHP
Expression block example
14
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>CSE 190 M: Embedded PHP</title></head>
<body>
<?php
for ($i = 99; $i >= 1; $i--) {
?>
<p> <?= $i ?> bottles of beer on the wall, <br />
<?= $i ?> bottles of beer. <br />
Take one down, pass it around, <br />
<?= $i - 1 ?> bottles of beer on the wall. </p>
<?php
}
?>
</body>
</html> PHP
Common errors: unclosed
braces, missing = sign15
...
<body>
<p>Watch how high I can count:
<?php
for ($i = 1; $i <= 10; $i++) {
?>
<? $i ?>
</p>
</body>
</html> PHP
 if you forget to close your braces, you'll see an error
about 'unexpected $end'
 if you forget = in <?=, the expression does not produce
any output
CS380
Complex expression blocks
16
...
<body>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<h<?= $i ?>>This is a level <?= $i ?>
heading.</h<?= $i ?>>
<?php
}
?>
</body> PHP
CS380
This is a level 1 heading.
This is a level 2 heading.
This is a level 3 heading. output
Functions
Advanced PHP Syntax17
CS380
Functions
18
function name(parameterName, ..., parameterName) {
statements;
} PHP
CS380
function quadratic($a, $b, $c) {
return -$b + sqrt($b * $b - 4 * $a * $c) / (2
* $a);
} PHP
 parameter types and return types are not written
 a function with no return statements implicitly returns
NULL
Default Parameter Values
19
function print_separated($str, $separator = ", ") {
if (strlen($str) > 0) {
print $str[0];
for ($i = 1; $i < strlen($str); $i++) {
print $separator . $str[$i];
}
}
} PHP
CS380
print_separated("hello"); # h, e, l, l, o
print_separated("hello", "-"); # h-e-l-l-o
PHP
 if no value is passed, the default will be used
PHP Arrays Ex. 1
 Arrays allow you to assign multiple values to one
variable. For this PHP exercise, write an array variable
of weather conditions with the following values: rain,
sunshine, clouds, hail, sleet, snow, wind. Using the array
variable for all the weather conditions, echo the following
statement to the browser:
We've seen all kinds of weather this month. At the
beginning of the month, we had snow and wind. Then
came sunshine with a few clouds and some rain. At least
we didn't get any hail or sleet.
 Don't forget to include a title for your page, both in the
header and on the page itself.
CS380
20
PHP Arrays Ex. 2
 For this exercise, you will use a list of ten of the largest
cities in the world. (Please note, these are not the ten
largest, just a selection of ten from the largest cities.)
Create an array with the following values: Tokyo, Mexico
City, New York City, Mumbai, Seoul, Shanghai, Lagos,
Buenos Aires, Cairo, London.
 Print these values to the browser separated by commas,
using a loop to iterate over the array. Sort the array, then
print the values to the browser in an unordered list,
again using a loop.
 Add the following cities to the array: Los Angeles,
Calcutta, Osaka, Beijing. Sort the array again, and print
it once more to the browser in an unordered list.CS380
21

More Related Content

What's hot

Descobrindo a linguagem Perl
Descobrindo a linguagem PerlDescobrindo a linguagem Perl
Descobrindo a linguagem Perlgarux
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev AssistantDave Cross
 
Crafting Custom Interfaces with Sub::Exporter
Crafting Custom Interfaces with Sub::ExporterCrafting Custom Interfaces with Sub::Exporter
Crafting Custom Interfaces with Sub::ExporterRicardo Signes
 
Refactor like a boss
Refactor like a bossRefactor like a boss
Refactor like a bossgsterndale
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and PatternsHenry Osborne
 
Climbing the Abstract Syntax Tree (php[world] 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)Climbing the Abstract Syntax Tree (php[world] 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)James Titcumb
 
Functional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures editionFunctional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures editionosfameron
 
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)James Titcumb
 
Achieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangAchieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangSean Cribbs
 
The bones of a nice Python script
The bones of a nice Python scriptThe bones of a nice Python script
The bones of a nice Python scriptsaniac
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arraysKumar
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in SwiftSeongGyu Jo
 
Climbing the Abstract Syntax Tree (phpDay 2017)
Climbing the Abstract Syntax Tree (phpDay 2017)Climbing the Abstract Syntax Tree (phpDay 2017)
Climbing the Abstract Syntax Tree (phpDay 2017)James Titcumb
 
String variable in php
String variable in phpString variable in php
String variable in phpchantholnet
 

What's hot (18)

Descobrindo a linguagem Perl
Descobrindo a linguagem PerlDescobrindo a linguagem Perl
Descobrindo a linguagem Perl
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Php array
Php arrayPhp array
Php array
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev Assistant
 
Crafting Custom Interfaces with Sub::Exporter
Crafting Custom Interfaces with Sub::ExporterCrafting Custom Interfaces with Sub::Exporter
Crafting Custom Interfaces with Sub::Exporter
 
Refactor like a boss
Refactor like a bossRefactor like a boss
Refactor like a boss
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and Patterns
 
Climbing the Abstract Syntax Tree (php[world] 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)Climbing the Abstract Syntax Tree (php[world] 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)
 
Functional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures editionFunctional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures edition
 
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
 
Achieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangAchieving Parsing Sanity In Erlang
Achieving Parsing Sanity In Erlang
 
The bones of a nice Python script
The bones of a nice Python scriptThe bones of a nice Python script
The bones of a nice Python script
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in Swift
 
Climbing the Abstract Syntax Tree (phpDay 2017)
Climbing the Abstract Syntax Tree (phpDay 2017)Climbing the Abstract Syntax Tree (phpDay 2017)
Climbing the Abstract Syntax Tree (phpDay 2017)
 
String variable in php
String variable in phpString variable in php
String variable in php
 
Intoduction to php arrays
Intoduction to php arraysIntoduction to php arrays
Intoduction to php arrays
 

Similar to Presentaion

PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshotsrichambra
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Dhivyaa C.R
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.netPhp my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.netProgrammer Blog
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeDhivyaa C.R
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04Hassen Poreya
 
06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx20521742
 
PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array. PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array. wahidullah mudaser
 

Similar to Presentaion (20)

07-PHP.pptx
07-PHP.pptx07-PHP.pptx
07-PHP.pptx
 
07-PHP.pptx
07-PHP.pptx07-PHP.pptx
07-PHP.pptx
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
 
UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.netPhp my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.net
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
 
PHP Tutorial (funtion)
PHP Tutorial (funtion)PHP Tutorial (funtion)
PHP Tutorial (funtion)
 
Php
PhpPhp
Php
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
 
Php
PhpPhp
Php
 
06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx
 
PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array. PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array.
 
08 php-files
08 php-files08 php-files
08 php-files
 

Recently uploaded

2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 E...
2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 E...2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 E...
2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 E...AmitSherawat2
 
Low Rate Call Girls Nashik Mahima 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Mahima 7001305949 Independent Escort Service NashikLow Rate Call Girls Nashik Mahima 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Mahima 7001305949 Independent Escort Service Nashikranjana rawat
 
Assessment on SITXINV007 Purchase goods.pdf
Assessment on SITXINV007 Purchase goods.pdfAssessment on SITXINV007 Purchase goods.pdf
Assessment on SITXINV007 Purchase goods.pdfUMER979507
 
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...dollysharma2066
 
Prepare And Cook Meat.pptx Quarter II Module
Prepare And Cook Meat.pptx Quarter II ModulePrepare And Cook Meat.pptx Quarter II Module
Prepare And Cook Meat.pptx Quarter II Modulemaricel769799
 
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THAT
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THATFUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THAT
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THATBHIKHUKUMAR KUNWARADIYA
 
VIP Kolkata Call Girl Jadavpur 👉 8250192130 Available With Room
VIP Kolkata Call Girl Jadavpur 👉 8250192130  Available With RoomVIP Kolkata Call Girl Jadavpur 👉 8250192130  Available With Room
VIP Kolkata Call Girl Jadavpur 👉 8250192130 Available With Roomdivyansh0kumar0
 
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?t6tjlrih
 
Gwal Pahari Call Girls 9873940964 Book Hot And Sexy Girls
Gwal Pahari Call Girls 9873940964 Book Hot And Sexy GirlsGwal Pahari Call Girls 9873940964 Book Hot And Sexy Girls
Gwal Pahari Call Girls 9873940964 Book Hot And Sexy Girlshram8477
 
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...srsj9000
 
Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一Fi sss
 
咨询办理南卡罗来纳大学毕业证成绩单SC毕业文凭
咨询办理南卡罗来纳大学毕业证成绩单SC毕业文凭咨询办理南卡罗来纳大学毕业证成绩单SC毕业文凭
咨询办理南卡罗来纳大学毕业证成绩单SC毕业文凭o8wvnojp
 
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130Suhani Kapoor
 
VIP Call Girls Service Secunderabad Hyderabad Call +91-8250192130
VIP Call Girls Service Secunderabad Hyderabad Call +91-8250192130VIP Call Girls Service Secunderabad Hyderabad Call +91-8250192130
VIP Call Girls Service Secunderabad Hyderabad Call +91-8250192130Suhani Kapoor
 
HIGH PRESSURE PROCESSING ( HPP ) .pptx
HIGH PRESSURE  PROCESSING ( HPP )  .pptxHIGH PRESSURE  PROCESSING ( HPP )  .pptx
HIGH PRESSURE PROCESSING ( HPP ) .pptxparvin6647
 
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...Suhani Kapoor
 

Recently uploaded (20)

2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 E...
2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 E...2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 E...
2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 Endocrine System.ppt2.6 E...
 
Low Rate Call Girls Nashik Mahima 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Mahima 7001305949 Independent Escort Service NashikLow Rate Call Girls Nashik Mahima 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Mahima 7001305949 Independent Escort Service Nashik
 
Assessment on SITXINV007 Purchase goods.pdf
Assessment on SITXINV007 Purchase goods.pdfAssessment on SITXINV007 Purchase goods.pdf
Assessment on SITXINV007 Purchase goods.pdf
 
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...
 
9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR
9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR
9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR
 
Prepare And Cook Meat.pptx Quarter II Module
Prepare And Cook Meat.pptx Quarter II ModulePrepare And Cook Meat.pptx Quarter II Module
Prepare And Cook Meat.pptx Quarter II Module
 
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THAT
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THATFUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THAT
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THAT
 
VIP Kolkata Call Girl Jadavpur 👉 8250192130 Available With Room
VIP Kolkata Call Girl Jadavpur 👉 8250192130  Available With RoomVIP Kolkata Call Girl Jadavpur 👉 8250192130  Available With Room
VIP Kolkata Call Girl Jadavpur 👉 8250192130 Available With Room
 
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?
 
Gwal Pahari Call Girls 9873940964 Book Hot And Sexy Girls
Gwal Pahari Call Girls 9873940964 Book Hot And Sexy GirlsGwal Pahari Call Girls 9873940964 Book Hot And Sexy Girls
Gwal Pahari Call Girls 9873940964 Book Hot And Sexy Girls
 
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...
 
Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝
 
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一
 
咨询办理南卡罗来纳大学毕业证成绩单SC毕业文凭
咨询办理南卡罗来纳大学毕业证成绩单SC毕业文凭咨询办理南卡罗来纳大学毕业证成绩单SC毕业文凭
咨询办理南卡罗来纳大学毕业证成绩单SC毕业文凭
 
Cut & fry Potato is Not FRENCH FRIES ..
Cut & fry Potato is Not FRENCH FRIES  ..Cut & fry Potato is Not FRENCH FRIES  ..
Cut & fry Potato is Not FRENCH FRIES ..
 
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130
 
VIP Call Girls Service Secunderabad Hyderabad Call +91-8250192130
VIP Call Girls Service Secunderabad Hyderabad Call +91-8250192130VIP Call Girls Service Secunderabad Hyderabad Call +91-8250192130
VIP Call Girls Service Secunderabad Hyderabad Call +91-8250192130
 
young Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort service
young Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort serviceyoung Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort service
young Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort service
 
HIGH PRESSURE PROCESSING ( HPP ) .pptx
HIGH PRESSURE  PROCESSING ( HPP )  .pptxHIGH PRESSURE  PROCESSING ( HPP )  .pptx
HIGH PRESSURE PROCESSING ( HPP ) .pptx
 
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...
 

Presentaion

  • 1. Arrays Strings and regular expressions Basic PHP Syntax CS380 1
  • 2. Arrays 2  Append: use bracket notation without specifying an index  Element type is not specified; can mix types $name = array(); # create $name = array(value0, value1, ..., valueN); $name[index] # get element value $name[index] = value; # set element value $name[] = value; # append PHP CS380 $a = array(); # empty array (length 0) $a[0] = 23; # stores 23 at index 0 (length 1) $a2 = array("some", "strings", "in", "an", "array"); $a2[] = "Ooh!"; # add string to end (at index 5) PHP
  • 3. Array functions 3 function name(s) description count number of elements in the array print_r print array's contents array_pop, array_push, array_shift, array_unshift using array as a stack/queue in_array, array_search, array_reverse, sort, rsort, shuffle searching and reordering array_fill, array_merge, array_intersect, array_diff, array_slice, range creating, filling, filtering array_sum, array_product, array_unique, processing elements
  • 4. Array function example 4  the array in PHP replaces many other collections in Java  list, stack, queue, set, map, ... $tas = array("MD", "BH", "KK", "HM", "JP"); for ($i = 0; $i < count($tas); $i++) { $tas[$i] = strtolower($tas[$i]); } $morgan = array_shift($tas); array_pop($tas); array_push($tas, "ms"); array_reverse($tas); sort($tas); $best = array_slice($tas, 1, 2); PHP CS380
  • 5. foreach loop 5 foreach ($array as $variableName) { ... } PHP CS380 $fellowship = array(“Frodo", “Sam", “Gandalf", “Strider", “Gimli", “Legolas", “Boromir"); print “The fellowship of the ring members are: n"; for ($i = 0; $i < count($fellowship); $i++) { print "{$fellowship[$i]}n"; } print “The fellowship of the ring members are: n"; foreach ($fellowship as $fellow) { print "$fellown"; } PHP
  • 6. Multidimensional Arrays 6 <?php $AmazonProducts = array( array(“BOOK", "Books", 50), array("DVDs", “Movies", 15), array(“CDs", “Music", 20) ); for ($row = 0; $row < 3; $row++) { for ($column = 0; $column < 3; $column++) { ?> <p> | <?= $AmazonProducts[$row][$column] ?> <?php } ?> </p> <?php } ?> PHP CS380
  • 7. Multidimensional Arrays (cont.) 7 <?php $AmazonProducts = array( array(“Code” =>“BOOK", “Description” => "Books", “Price” => 50), array(“Code” => "DVDs", “Description” => “Movies", “Price” => 15), array(“Code” => “CDs", “Description” => “Music", “Price” => 20) ); for ($row = 0; $row < 3; $row++) { ?> <p> | <?= $AmazonProducts[$row][“Code”] ?> | <?= $AmazonProducts[$row][“Description”] ?> | <?= $AmazonProducts[$row][“Price”] ?> </p> <?php } ?> PHP CS380
  • 8. String compare functions 8 Name Function strcmp compareTo strstr, strchr find string/char within a string strpos find numerical position of string str_replace, substr_replace replace string Comparison can be:  Partial matches  Others  Variations with non case sensitive functions  strcasecmp
  • 9. String compare functions examples9 $offensive = array( offensive word1, offensive word2); $feedback = str_replace($offcolor, “%!@*”, $feedback); PHP CS380 $test = “Hello World! n”; print strpos($test, “o”); print strpos($test, “o”, 5); PHP $toaddress = “feedback@example.com”; if(strstr($feedback, “shop”) $toaddress = “shop@example.com”; else if(strstr($feedback, “delivery”) $toaddress = “fulfillment@example.com”; PHP
  • 10. Regular expressions 10 [a-z]at #cat, rat, bat… [aeiou] [a-zA-Z] [^a-z] #not a-z [[:alnum:]]+ #at least one alphanumeric char (very) *large #large, very very very large… (very){1, 3} #counting “very” up to 3 ^bob #bob at the beginning com$ #com at the end PHPRegExp  Regular expression: a pattern in a piece of text  PHP has:  POSIX  Perl regular expressions CS380
  • 12. Printing HTML tags in PHP = bad style12 <?php print "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"n"; print " "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">n"; print "<html xmlns="http://www.w3.org/1999/xhtml">n"; print " <head>n"; print " <title>Geneva's web page</title>n"; ... for ($i = 1; $i <= 10; $i++) { print "<p> I can count to $i! </p>n"; } ?> HTML  best PHP style is to minimize print/echo statements in embedded PHP code  but without print, how do we insert dynamic content into the page?
  • 13. PHP expression blocks 13  PHP expression block: a small piece of PHP that evaluates and embeds an expression's value into HTML  <?= expression ?> is equivalent to: <?= expression ?> PHP CS380 <h2> The answer is <?= 6 * 7 ?> </h2> PHP The answer is 42 output <?php print expression; ?> PHP
  • 14. Expression block example 14 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title>CSE 190 M: Embedded PHP</title></head> <body> <?php for ($i = 99; $i >= 1; $i--) { ?> <p> <?= $i ?> bottles of beer on the wall, <br /> <?= $i ?> bottles of beer. <br /> Take one down, pass it around, <br /> <?= $i - 1 ?> bottles of beer on the wall. </p> <?php } ?> </body> </html> PHP
  • 15. Common errors: unclosed braces, missing = sign15 ... <body> <p>Watch how high I can count: <?php for ($i = 1; $i <= 10; $i++) { ?> <? $i ?> </p> </body> </html> PHP  if you forget to close your braces, you'll see an error about 'unexpected $end'  if you forget = in <?=, the expression does not produce any output CS380
  • 16. Complex expression blocks 16 ... <body> <?php for ($i = 1; $i <= 3; $i++) { ?> <h<?= $i ?>>This is a level <?= $i ?> heading.</h<?= $i ?>> <?php } ?> </body> PHP CS380 This is a level 1 heading. This is a level 2 heading. This is a level 3 heading. output
  • 18. Functions 18 function name(parameterName, ..., parameterName) { statements; } PHP CS380 function quadratic($a, $b, $c) { return -$b + sqrt($b * $b - 4 * $a * $c) / (2 * $a); } PHP  parameter types and return types are not written  a function with no return statements implicitly returns NULL
  • 19. Default Parameter Values 19 function print_separated($str, $separator = ", ") { if (strlen($str) > 0) { print $str[0]; for ($i = 1; $i < strlen($str); $i++) { print $separator . $str[$i]; } } } PHP CS380 print_separated("hello"); # h, e, l, l, o print_separated("hello", "-"); # h-e-l-l-o PHP  if no value is passed, the default will be used
  • 20. PHP Arrays Ex. 1  Arrays allow you to assign multiple values to one variable. For this PHP exercise, write an array variable of weather conditions with the following values: rain, sunshine, clouds, hail, sleet, snow, wind. Using the array variable for all the weather conditions, echo the following statement to the browser: We've seen all kinds of weather this month. At the beginning of the month, we had snow and wind. Then came sunshine with a few clouds and some rain. At least we didn't get any hail or sleet.  Don't forget to include a title for your page, both in the header and on the page itself. CS380 20
  • 21. PHP Arrays Ex. 2  For this exercise, you will use a list of ten of the largest cities in the world. (Please note, these are not the ten largest, just a selection of ten from the largest cities.) Create an array with the following values: Tokyo, Mexico City, New York City, Mumbai, Seoul, Shanghai, Lagos, Buenos Aires, Cairo, London.  Print these values to the browser separated by commas, using a loop to iterate over the array. Sort the array, then print the values to the browser in an unordered list, again using a loop.  Add the following cities to the array: Los Angeles, Calcutta, Osaka, Beijing. Sort the array again, and print it once more to the browser in an unordered list.CS380 21

Editor's Notes

  1. Maybe delete the comments that give out the output # ("md", "bh", "kk", "hm", "jp") # ("bh", "kk", "hm") # ("bh", "kk", "hm", "ms") # ("ms", "hm", "kk", "bh") # ("bh", "hm", "kk", "ms") # ("hm", "kk")
  2. One bad thing with foreach: you cannot change the value of the variable after “as”, you can just assign it a value, it does not have the value in the table
  3. Maybe delete the comments that give out the output
  4. useful for embedding a small amount of PHP (a variable's or expression's value) in a large block of HTML without having to switch to "PHP-mode"