Aims:
echo "\"Marry me,\" he said.\n\"No way,\" she replied."; echo "\"In that case, 1\\2 a pound of cheese, please," he said.";
$surname = "Bond";
$firstname = "James";
echo 'My name is ';
echo $surname;
echo ', ';
echo $firstname;
echo ' ';
echo $surname;
echo "My name is {$surname}, {$firstname} {$surname}";
Interpolation: in strings written using double quotes,
variables in braces are replaced by their values
echo 'My name is {$surname}, {$firstname} {$surname}';
<?php $quantity = 1000; $pricePerBanana = 0.14; $discountPerCent = 25; $fullPrice = $quantity * $pricePerBanana; $discount = $fullPrice * $discountPerCent / 100; $actualPrice = $fullPrice - $discount; echo "<table>"; echo "<tr><th>Full price:</th><td>{$fullPrice}</td></tr>"; echo "<tr><th>Less discount:</th><td>{$discount}</td></tr>"; echo "<tr><th>Actual price:</th><td>{$actualPrice}</td></tr>"; echo "</table>"; ?>
$price = $price + 5;
$price += 5;
Similarly using -=, *=, /=, %=
$sectionNum = $sectionNum + 1;
$sectionNum += 1;
++$sectionNum;
And $sectionNum++
is the same in many cases
--$timeRemaining
and $timeRemaining--
$firstname = 'Hugh';
$surname = 'Jeego';
echo $firstname . $surname;
echo $firstname . ' ' . $surname;
.=
, similar to +=
Class exercise: What is the output from the following?
$message = 'Ha'; echo $message . $message; echo $message . $message . $message . 'Ho' . 'Ho' . 'Ho'; $message .= 'ppy'; echo $message . ' Xmas'; $age = 6; echo $message . ' ' . $age . 'th Birthday';
trim($s)
returns a copy of string $s
with
leading and trailing whitespace removedstrtolower($s)
returns a copy of $s
changed to lowercase
(strtoupper
similarly)strpos($s1, $s2)
returns the position of the first occurrence of string
$s2
in string $s1
or false
if not found
strpos("goodbye", "b")
returns 4substr($s, $offset, $len)
returns the substring of string $s
that starts from position $offset
and (optionally)
is of length $len
substr("goodbye", 4, 2)
returns "by"
substr("goodbye", 4)
returns "bye"
$myURL
$myURL
contains exactly
three parts (scheme, hostname and pathname)
"http://www.cs.ucc.ie/cs1/index.html"
strpos
and substr
to extract the following:
parse_url
) for doing all this
properly!