Values and Types
Strings
Strings store text, concatenate with ., and can be measured with strlen.
string length
`strlen` returns the number of bytes in a simple ASCII string.
Strings
strings.php
Replay: real traced execution (multi-file project)
<?php
$language = "PHP";
$message = "Hello, " . $language;
$length = strlen($message);
echo $message . "\n";
echo "length=" . $length . "\n";
<?php
$language = "Python";
$message = "Hello, " . $language;
$length = strlen($message);
echo $message . "\n";
echo "length=" . $length . "\n";
<?php
$language = "Ruby";
$message = "Hello, " . $language;
$length = strlen($message);
echo $message . "\n";
echo "length=" . $length . "\n";
$language ← PHP, $message ← Hello, PHP, $length ← 10
1<?php2$language→ PHP = "PHP"; //@language="Python", "Ruby"3$message→ Hello, PHP = "Hello, " . $languagePHP;4$length→ 10 = strlen($messageHello, PHP);56echo $messageHello, PHP . "\n";7echo "length=" . $length10 . "\n";outputHello, PHP length=10
$language ← Python, $message ← Hello, Python, $length ← 13
1<?php2$language→ Python = "Python";3$message→ Hello, Python = "Hello, " . $languagePython;4$length→ 13 = strlen($messageHello, Python);56echo $messageHello, Python . "\n";7echo "length=" . $length13 . "\n";outputHello, Python length=13
$language ← Ruby, $message ← Hello, Ruby, $length ← 11
1<?php2$language→ Ruby = "Ruby";3$message→ Hello, Ruby = "Hello, " . $languageRuby;4$length→ 11 = strlen($messageHello, Ruby);56echo $messageHello, Ruby . "\n";7echo "length=" . $length11 . "\n";outputHello, Ruby length=11
Follow the String
$languagestarts asPHP.$messagebecomesHello, PHP.strlen($message)returns10.- The program prints
Hello, PHPandlength=10. | language | message | length | | --- | --- | --- | | PHP | Hello, PHP | 10 | | Python | Hello, Python | 13 | | Ruby | Hello, Ruby | 11 |
Exercise: strings.php
Reproduce Hello, PHP and length=10, then use the pinned languages Python and Ruby to predict each message length.