3 php basic snytanx.php
3 php basic snytanx.php
Arrays
Strings and regular expressions
CS380
Arrays
2
PHP
CS380
Multidimensional Arrays
7
(cont.)
<?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
find string/char within a
strstr, strchr
string
find numerical position of
strpos
string
str_replace,
replace string
substr_replace
String compare functions
9
examples
$offensive = array( offensive word1, offensive
word2);
$feedback = str_replace($offcolor, “%!@*”,
$feedback);
PHP
$test = “Hello World! \n”;
print strpos($test, “o”);
print strpos($test, “o”, 5);
PHP
$toaddress = “[email protected]”;
if(strstr($feedback, “shop”)
$toaddress = “[email protected]”;
else if(strstr($feedback, “delivery”)
$toaddress = “[email protected]”;
CS380
PHP
Regular expressions
10
CS380
Printing HTML tags in PHP =
12
bad style
<?php
print "<!DOCTYPE html PUBLIC n";
print "<html xmlns\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
The answer is 42
output
PHP expression block: a small piece of PHP that
evaluates and embeds an expression's value into
HTML
<?php<?= expression
print ?> is?>
expression; equivalent to:
PHP
CS380
Expression block example
14
CS380
Functions
18
function name(parameterName, ..., parameterName) {
statements;
} PHP
CS380
Default Parameter Values
19
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
CS380
PHP Arrays Ex. 1
20