[doc-en] master: Enable WASM for book.strings, and tweak examples

From: Date: Tue, 06 May 2025 11:17:11 +0000
Subject: [doc-en] master: Enable WASM for book.strings, and tweak examples
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-22657@lists.php.net to get a copy of this message
Author: Derick Rethans (derickr) Date: 2025-05-06T12:06:07+01:00 Commit: https://github.com/php/doc-en/commit/45042fef652f1b4e904e809fcbfcf31f6c60670b Raw diff: https://github.com/php/doc-en/commit/45042fef652f1b4e904e809fcbfcf31f6c60670b.diff Enable WASM for book.strings, and tweak examples Changed paths: M reference/strings/book.xml M reference/strings/functions/addcslashes.xml M reference/strings/functions/addslashes.xml M reference/strings/functions/chr.xml M reference/strings/functions/chunk-split.xml M reference/strings/functions/echo.xml M reference/strings/functions/explode.xml M reference/strings/functions/fprintf.xml M reference/strings/functions/html-entity-decode.xml M reference/strings/functions/lcfirst.xml M reference/strings/functions/md5-file.xml M reference/strings/functions/number-format.xml M reference/strings/functions/ord.xml M reference/strings/functions/parse-str.xml M reference/strings/functions/print.xml M reference/strings/functions/setlocale.xml M reference/strings/functions/sha1-file.xml M reference/strings/functions/soundex.xml M reference/strings/functions/sprintf.xml M reference/strings/functions/str-getcsv.xml M reference/strings/functions/str-pad.xml M reference/strings/functions/str-replace.xml M reference/strings/functions/stripos.xml M reference/strings/functions/stristr.xml M reference/strings/functions/strlen.xml M reference/strings/functions/strnatcmp.xml M reference/strings/functions/strpbrk.xml M reference/strings/functions/strpos.xml M reference/strings/functions/strrpos.xml M reference/strings/functions/strstr.xml M reference/strings/functions/strtok.xml M reference/strings/functions/strtr.xml M reference/strings/functions/substr-compare.xml M reference/strings/functions/substr-count.xml M reference/strings/functions/substr.xml M reference/strings/functions/ucfirst.xml M reference/strings/functions/ucwords.xml M reference/strings/functions/vfprintf.xml Diff: diff --git a/reference/strings/book.xml b/reference/strings/book.xml index 3f801f3901f6..a1495251cc2b 100644 --- a/reference/strings/book.xml +++ b/reference/strings/book.xml @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<book xml:id="book.strings" xmlns="http://docbook.org/ns/docbook"> +<book xml:id="book.strings" xmlns="http://docbook.org/ns/docbook" annotations="interactive"> <?phpdoc extension-membership="core" ?> <title>Strings</title> diff --git a/reference/strings/functions/addcslashes.xml b/reference/strings/functions/addcslashes.xml index 3b4976439fc9..fbbd1f7ed205 100644 --- a/reference/strings/functions/addcslashes.xml +++ b/reference/strings/functions/addcslashes.xml @@ -46,7 +46,8 @@ When you define a sequence of characters in the <parameter>characters</parameter> argument make sure that you know what characters come between the characters that you set as the start and end of the range. - <informalexample> + <example> + <title><function>addcslashes</function> with Ranges</title> <programlisting role="php"> <![CDATA[ <?php @@ -57,13 +58,14 @@ echo addcslashes('foo[ ]', 'A..z'); ?> ]]> </programlisting> - </informalexample> + </example> Also, if the first character in a range has a higher ASCII value than the second character in the range, no range will be constructed. Only the start, end and period characters will be escaped. Use the <function>ord</function> function to find the ASCII value for a character. - <informalexample> + <example> + <title><function>addcslashes</function> with Characters in Wrong Order</title> <programlisting role="php"> <![CDATA[ <?php @@ -72,7 +74,7 @@ echo addcslashes("zoo['.']", 'z..A'); ?> ]]> </programlisting> - </informalexample> + </example> </para> <para> Be careful if you choose to escape characters 0, a, b, f, n, r, t and @@ -106,7 +108,9 @@ echo addcslashes("zoo['.']", 'z..A'); <programlisting role="php"> <![CDATA[ <?php +$not_escaped = "PHP isThirty\nYears Old!\tYay to the Elephant!\n"; $escaped = addcslashes($not_escaped, "\0..\37!@\177..\377"); +echo $escaped; ?> ]]> </programlisting> diff --git a/reference/strings/functions/addslashes.xml b/reference/strings/functions/addslashes.xml index d57f50dc474c..6fbf07b46da5 100644 --- a/reference/strings/functions/addslashes.xml +++ b/reference/strings/functions/addslashes.xml @@ -25,7 +25,8 @@ <para> A use case of <function>addslashes</function> is escaping the aforementioned characters in a string that is to be evaluated by PHP: - <informalexample> + <example> + <title>Escaping Characters</title> <programlisting role="php"> <![CDATA[ <?php @@ -34,7 +35,7 @@ eval("echo '" . addslashes($str) . "';"); ?> ]]> </programlisting> - </informalexample> + </example> </para> <para> The <function>addslashes</function> is sometimes incorrectly used to try to prevent diff --git a/reference/strings/functions/chr.xml b/reference/strings/functions/chr.xml index 6e0ec18d93fb..ac2d9ff2db15 100644 --- a/reference/strings/functions/chr.xml +++ b/reference/strings/functions/chr.xml @@ -94,14 +94,17 @@ $bytevalue %= 256; $str = "The string ends in escape: "; $str .= chr(27); /* add an escape character at the end of $str */ +echo $str, PHP_EOL; /* Often this is more useful */ - $str = sprintf("The string ends in escape: %c", 27); +echo $str, PHP_EOL; ?> ]]> </programlisting> </example> + </para> + <para> <example> <title>Overflow behavior</title> <programlisting role="php"> @@ -126,7 +129,7 @@ aA <![CDATA[ <?php $str = chr(240) . chr(159) . chr(144) . chr(152); -echo $str; +echo $str, PHP_EOL; ?> ]]> </programlisting> diff --git a/reference/strings/functions/chunk-split.xml b/reference/strings/functions/chunk-split.xml index b15ae65e8155..5f433c6009c3 100644 --- a/reference/strings/functions/chunk-split.xml +++ b/reference/strings/functions/chunk-split.xml @@ -69,8 +69,11 @@ <programlisting role="php"> <![CDATA[ <?php +$data = 'This is quite a long string, which will get broken up because the line is going to be too long after base64 encoding it.'; + // format $data using RFC 2045 semantics $new_string = chunk_split(base64_encode($data)); +echo $new_string, PHP_EOL; ?> ]]> </programlisting> diff --git a/reference/strings/functions/echo.xml b/reference/strings/functions/echo.xml index 0b0f0d400187..167a9b0c30ad 100644 --- a/reference/strings/functions/echo.xml +++ b/reference/strings/functions/echo.xml @@ -105,9 +105,6 @@ echo implode(" and ", $fruits); // lemon and orange and banana // Non-string expressions are coerced to string, even if declare(strict_types=1) is used echo 6 * 7; // 42 -// Because echo does not behave as an expression, the following code is invalid. -($some_var) ? echo 'true' : echo 'false'; - // However, the following examples will work: ($some_var) ? print 'true' : print 'false'; // print is also a construct, but // it is a valid expression, returning 1, @@ -115,6 +112,19 @@ echo 6 * 7; // 42 echo $some_var ? 'true': 'false'; // evaluating the expression first and passing it to echo ?> +]]> + </programlisting> + </example> + </para> + <para> + <example> + <title><literal>echo</literal> is not an expression</title> + <programlisting role="php" annotations="non-interactive"> +<![CDATA[ +<?php +// Because echo does not behave as an expression, the following code is invalid. +($some_var) ? echo 'true' : echo 'false'; +?> ]]> </programlisting> </example> @@ -134,32 +144,44 @@ echo $some_var ? 'true': 'false'; // evaluating the expression first and passing part of the expression being output, not part of the <literal>echo</literal> syntax itself. - <informalexample> + <example> + <title>Using Parentheses</title> <programlisting role="php"> <![CDATA[ <?php -echo "hello"; +echo "hello", PHP_EOL; // outputs "hello" -echo("hello"); +echo("hello"), PHP_EOL; // also outputs "hello", because ("hello") is a valid expression -echo(1 + 2) * 3; +echo(1 + 2) * 3, PHP_EOL; // outputs "9"; the parentheses cause 1+2 to be evaluated first, then 3*3 // the echo statement sees the whole expression as one argument -echo "hello", " world"; +echo "hello", " world", PHP_EOL; // outputs "hello world" -echo("hello"), (" world"); +echo("hello"), (" world"), PHP_EOL; // outputs "hello world"; the parentheses are part of each expression +?> +]]> + </programlisting> + </example> + </para> -echo("hello", " world"); + <para> + <example> + <title>Invalid Expression</title> + <programlisting role="php" annotations="non-interactive"> + <![CDATA[ +<?php +echo("hello", " world"), PHP_EOL; // Throws a Parse Error because ("hello", " world") is not a valid expression ?> ]]> </programlisting> - </informalexample> + </example> </para> </note> diff --git a/reference/strings/functions/explode.xml b/reference/strings/functions/explode.xml index 2a62782fac0d..4e186e77a108 100644 --- a/reference/strings/functions/explode.xml +++ b/reference/strings/functions/explode.xml @@ -127,15 +127,14 @@ // Example 1 $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); -echo $pieces[0]; // piece1 -echo $pieces[1]; // piece2 +echo $pieces[0], PHP_EOL; // piece1 +echo $pieces[1], PHP_EOL; // piece2 // Example 2 $data = "foo:*:1023:1000::/home/foo:/bin/sh"; list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data); -echo $user; // foo -echo $pass; // * - +echo $user, PHP_EOL; // foo +echo $pass, PHP_EOL; // * ?> ]]> </programlisting> diff --git a/reference/strings/functions/fprintf.xml b/reference/strings/functions/fprintf.xml index 72dcace23d31..506074aa761c 100644 --- a/reference/strings/functions/fprintf.xml +++ b/reference/strings/functions/fprintf.xml @@ -70,6 +70,10 @@ if (!($fp = fopen('date.txt', 'w'))) { return; } +$year = 2005; +$month = 5; +$day = 6; + fprintf($fp, "%04d-%02d-%02d", $year, $month, $day); // will write the formatted ISO date to date.txt ?> diff --git a/reference/strings/functions/html-entity-decode.xml b/reference/strings/functions/html-entity-decode.xml index c26f3482ecbc..d90ed9f4f2ab 100644 --- a/reference/strings/functions/html-entity-decode.xml +++ b/reference/strings/functions/html-entity-decode.xml @@ -166,9 +166,9 @@ $a = htmlentities($orig); $b = html_entity_decode($a); -echo $a; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now +echo $a, PHP_EOL; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now -echo $b; // I'll "walk" the <b>dog</b> now +echo $b, PHP_EOL; // I'll "walk" the <b>dog</b> now ?> ]]> </programlisting> diff --git a/reference/strings/functions/lcfirst.xml b/reference/strings/functions/lcfirst.xml index ee75c446d54c..0bd2c607d7d9 100644 --- a/reference/strings/functions/lcfirst.xml +++ b/reference/strings/functions/lcfirst.xml @@ -69,11 +69,11 @@ <![CDATA[ <?php $foo = 'HelloWorld'; -$foo = lcfirst($foo); // helloWorld +echo lcfirst($foo), PHP_EOL; // helloWorld $bar = 'HELLO WORLD!'; -$bar = lcfirst($bar); // hELLO WORLD! -$bar = lcfirst(strtoupper($bar)); // hELLO WORLD! +echo lcfirst($bar), PHP_EOL; // hELLO WORLD! +echo lcfirst(strtoupper($bar)), PHP_EOL; // hELLO WORLD! ?> ]]> </programlisting> diff --git a/reference/strings/functions/md5-file.xml b/reference/strings/functions/md5-file.xml index caf6fbc781ca..323559a66ee4 100644 --- a/reference/strings/functions/md5-file.xml +++ b/reference/strings/functions/md5-file.xml @@ -62,7 +62,7 @@ <programlisting role="php"> <![CDATA[ <?php -$file = 'php-5.3.0alpha2-Win32-VC9-x64.zip'; +$file = '/examples/book.xml'; echo 'MD5 file hash of ' . $file . ': ' . md5_file($file); ?> diff --git a/reference/strings/functions/number-format.xml b/reference/strings/functions/number-format.xml index 5220842b2ca2..dab147dc7470 100644 --- a/reference/strings/functions/number-format.xml +++ b/reference/strings/functions/number-format.xml @@ -130,17 +130,17 @@ $number = 1234.56; // english notation (default) -$english_format_number = number_format($number); +echo number_format($number), PHP_EOL; // 1,235 // French notation -$nombre_format_francais = number_format($number, 2, ',', ' '); +echo number_format($number, 2, ',', ' '), PHP_EOL; // 1 234,56 $number = 1234.5678; // english notation without thousands separator -$english_format_number = number_format($number, 2, '.', ''); +echo number_format($number, 2, '.', ''), PHP_EOL; // 1234.57 ?> diff --git a/reference/strings/functions/ord.xml b/reference/strings/functions/ord.xml index da3931c3a8f7..1f634063b963 100644 --- a/reference/strings/functions/ord.xml +++ b/reference/strings/functions/ord.xml @@ -70,7 +70,6 @@ if (ord($str) == 10) { <programlisting role="php"> <![CDATA[ <?php -declare(encoding='UTF-8'); $str = "🐘"; for ( $pos=0; $pos < strlen($str); $pos ++ ) { $byte = substr($str, $pos); diff --git a/reference/strings/functions/parse-str.xml b/reference/strings/functions/parse-str.xml index 9b03d2e3d4eb..9840d4deafd6 100644 --- a/reference/strings/functions/parse-str.xml +++ b/reference/strings/functions/parse-str.xml @@ -103,15 +103,9 @@ $str = "first=value&arr[]=foo+bar&arr[]=baz"; // Recommended parse_str($str, $output); -echo $output['first']; // value -echo $output['arr'][0]; // foo bar -echo $output['arr'][1]; // baz - -// DISCOURAGED -parse_str($str); -echo $first; // value -echo $arr[0]; // foo bar -echo $arr[1]; // baz +echo $output['first'], PHP_EOL; // value +echo $output['arr'][0], PHP_EOL; // foo bar +echo $output['arr'][1], PHP_EOL; // baz ?> ]]> </programlisting> @@ -127,9 +121,6 @@ echo $arr[1]; // baz <programlisting role="php"> <![CDATA[ <?php -parse_str("My Value=Something"); -echo $My_Value; // Something - parse_str("My Value=Something", $output); echo $output['My_Value']; // Something ?> diff --git a/reference/strings/functions/print.xml b/reference/strings/functions/print.xml index 79ea861b27ec..e5088f183563 100644 --- a/reference/strings/functions/print.xml +++ b/reference/strings/functions/print.xml @@ -61,35 +61,44 @@ <![CDATA[ <?php print "print does not require parentheses."; +print PHP_EOL; // No newline or space is added; the below outputs "helloworld" all on one line print "hello"; print "world"; +print PHP_EOL; print "This string spans multiple lines. The newlines will be output as well"; +print PHP_EOL; print "This string spans\nmultiple lines. The newlines will be\noutput as well."; +print PHP_EOL; // The argument can be any expression which produces a string $foo = "example"; print "foo is $foo"; // foo is example +print PHP_EOL; $fruits = ["lemon", "orange", "banana"]; print implode(" and ", $fruits); // lemon and orange and banana +print PHP_EOL; // Non-string expressions are coerced to string, even if declare(strict_types=1) is used print 6 * 7; // 42 +print PHP_EOL; // Because print has a return value, it can be used in expressions // The following outputs "hello world" if ( print "hello" ) { echo " world"; } +print PHP_EOL; // The following outputs "true" ( 1 === 1 ) ? print 'true' : print 'false'; +print PHP_EOL; ?> ]]> </programlisting> diff --git a/reference/strings/functions/setlocale.xml b/reference/strings/functions/setlocale.xml index 23566562e31a..4b93db2b6b9b 100644 --- a/reference/strings/functions/setlocale.xml +++ b/reference/strings/functions/setlocale.xml @@ -171,7 +171,7 @@ <para> <example> <title><function>setlocale</function> Examples</title> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php /* Set locale to Dutch */ @@ -191,7 +191,7 @@ echo "Preferred locale for german on this system is '$loc_de'"; <para> <example> <title><function>setlocale</function> Examples for Windows</title> - <programlisting role="php"> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php /* Set locale to Dutch */ diff --git a/reference/strings/functions/sha1-file.xml b/reference/strings/functions/sha1-file.xml index 72162394adb8..28ec1327c417 100644 --- a/reference/strings/functions/sha1-file.xml +++ b/reference/strings/functions/sha1-file.xml @@ -60,10 +60,9 @@ <programlisting role="php"> <![CDATA[ <?php -foreach(glob('/home/Kalle/myproject/*.php') as $ent) +foreach (glob('/examples/*.xml') as $ent) { - if(is_dir($ent)) - { + if (is_dir($ent)) { continue; } diff --git a/reference/strings/functions/soundex.xml b/reference/strings/functions/soundex.xml index 093a3a37a372..c20cfa65a75e 100644 --- a/reference/strings/functions/soundex.xml +++ b/reference/strings/functions/soundex.xml @@ -84,6 +84,8 @@ <programlisting role="php"> <![CDATA[ <?php +echo soundex("Euler"), PHP_EOL, soundex("Ellery"), PHP_EOL; + soundex("Euler") == soundex("Ellery"); // E460 soundex("Gauss") == soundex("Ghosh"); // G200 soundex("Hilbert") == soundex("Heilbronn"); // H416 diff --git a/reference/strings/functions/sprintf.xml b/reference/strings/functions/sprintf.xml index 2eb37ca294dd..55a2ac6cf36f 100644 --- a/reference/strings/functions/sprintf.xml +++ b/reference/strings/functions/sprintf.xml @@ -77,54 +77,79 @@ echo sprintf($format, $num, $location); There are 5 monkeys in the tree ]]> </screen> - <para> + </example> + <para> However imagine we are creating a format string in a separate file, commonly because we would like to internationalize it and we rewrite it as: + </para> + <example> + <title>Wrong Argument Order</title> + <para> + The format string supports argument numbering/swapping. </para> <programlisting role="php"> <![CDATA[ <?php +$num = 5; +$location = 'tree'; + $format = 'The %s contains %d monkeys'; echo sprintf($format, $num, $location); ?> ]]> </programlisting> - <para> - We now have a problem. The order of the placeholders in the - format string does not match the order of the arguments in the - code. We would like to leave the code as is and simply indicate - in the format string which arguments the placeholders refer to. - We would write the format string like this instead: - </para> + </example> + <para> + We now have a problem. The order of the placeholders in the + format string does not match the order of the arguments in the + code. We would like to leave the code as is and simply indicate + in the format string which arguments the placeholders refer to. + We would write the format string like this instead: + </para> + + <example> + <title>Use Order Placeholder</title> <programlisting role="php"> <![CDATA[ <?php +$num = 5; +$location = 'tree'; + $format = 'The %2$s contains %1$d monkeys'; echo sprintf($format, $num, $location); ?> ]]> </programlisting> - <para> - An added benefit is that placeholders can be repeated without adding - more arguments in the code. - </para> + </example> + + <para> + An added benefit is that placeholders can be repeated without adding + more arguments in the code. + </para> + + <example> + <title>Repeated Placeholder</title> <programlisting role="php"> <![CDATA[ <?php +$num = 5; +$location = 'tree'; + $format = 'The %2$s contains %1$d monkeys. That\'s a nice %2$s full of %1$d monkeys.'; echo sprintf($format, $num, $location); ?> ]]> </programlisting> - <para> - When using argument swapping, the <literal>n$</literal> - <emphasis>position specifier</emphasis> must come immediately - after the percent sign (<literal>%</literal>), before any other - specifiers, as shown below. - </para> </example> + <para> + When using argument swapping, the <literal>n$</literal> + <emphasis>position specifier</emphasis> must come immediately + after the percent sign (<literal>%</literal>), before any other + specifiers, as shown below. + </para> + <example> <title>Specifying padding character</title> <programlisting role="php"> @@ -143,11 +168,15 @@ echo sprintf("%'.09d\n", 123); ]]> </screen> </example> + <example> <title>Position specifier with other specifiers</title> <programlisting role="php"> <![CDATA[ <?php +$num = 5; +$location = 'tree'; + $format = 'The %2$s contains %1$04d monkeys'; echo sprintf($format, $num, $location); ?> @@ -160,16 +189,23 @@ The tree contains 0005 monkeys ]]> </screen> </example> + <example> <title><function>sprintf</function>: zero-padded integers</title> <programlisting role="php"> <![CDATA[ <?php +$year = 2005; +$month = 5; +$day = 6; + $isodate = sprintf("%04d-%02d-%02d", $year, $month, $day); +echo $isodate, PHP_EOL; ?> ]]> </programlisting> </example> + <example> <title><function>sprintf</function>: formatting currency</title> <programlisting role="php"> @@ -178,10 +214,10 @@ $isodate = sprintf("%04d-%02d-%02d", $year, $month, $day); $money1 = 68.75; $money2 = 54.35; $money = $money1 + $money2; -echo $money; -echo "\n"; +echo $money, PHP_EOL; + $formatted = sprintf("%01.2f", $money); -echo $formatted; +echo $formatted, PHP_EOL; ?> ]]> </programlisting> @@ -200,7 +236,7 @@ echo $formatted; <?php $number = 362525200; -echo sprintf("%.3e", $number); +echo sprintf("%.3e", $number), PHP_EOL; ?> ]]> </programlisting> diff --git a/reference/strings/functions/str-getcsv.xml b/reference/strings/functions/str-getcsv.xml index 94000f944f61..1d380b2b9346 100644 --- a/reference/strings/functions/str-getcsv.xml +++ b/reference/strings/functions/str-getcsv.xml @@ -115,7 +115,7 @@ <?php $string = 'PHP,Java,Python,Kotlin,Swift'; -$data = str_getcsv($string); +$data = str_getcsv($string, escape: '\\'); var_dump($data); ?> @@ -154,7 +154,7 @@ array(5) { <?php $string = ''; -$data = str_getcsv($string); +$data = str_getcsv($string, escape: '\\'); var_dump($data); ?> diff --git a/reference/strings/functions/str-pad.xml b/reference/strings/functions/str-pad.xml index 416e81e6c186..9ee118bd96f1 100644 --- a/reference/strings/functions/str-pad.xml +++ b/reference/strings/functions/str-pad.xml @@ -94,11 +94,11 @@ <![CDATA[ <?php $input = "Alien"; -echo str_pad($input, 10); // produces "Alien " -echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien" -echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___" -echo str_pad($input, 6, "___"); // produces "Alien_" -echo str_pad($input, 3, "*"); // produces "Alien" +echo str_pad($input, 10), PHP_EOL; // produces "Alien " +echo str_pad($input, 10, "-=", STR_PAD_LEFT), PHP_EOL; // produces "-=-=-Alien" +echo str_pad($input, 10, "_", STR_PAD_BOTH), PHP_EOL; // produces "__Alien___" +echo str_pad($input, 6, "___"), PHP_EOL; // produces "Alien_" +echo str_pad($input, 3, "*"), PHP_EOL; // produces "Alien" ?> ]]> </programlisting> diff --git a/reference/strings/functions/str-replace.xml b/reference/strings/functions/str-replace.xml index a7ef6f156ad8..01de92935d4c 100644 --- a/reference/strings/functions/str-replace.xml +++ b/reference/strings/functions/str-replace.xml @@ -107,10 +107,12 @@ <?php // Provides: <body text='black'> $bodytag = str_replace("%body%", "black", "<body text='%body%'>"); +echo $bodytag, PHP_EOL; // Provides: Hll Wrld f PHP $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); +echo $onlyconsonants, PHP_EOL; // Provides: You should eat pizza, beer, and ice cream every day $phrase = "You should eat fruits, vegetables, and fiber every day."; @@ -118,10 +120,11 @@ $healthy = array("fruits", "vegetables", "fiber"); $yummy = array("pizza", "beer", "ice cream"); $newphrase = str_replace($healthy, $yummy, $phrase); +echo $newphrase, PHP_EOL; // Provides: 2 $str = str_replace("ll", "", "good golly miss molly!", $count); -echo $count; +echo $count, PHP_EOL; ?> ]]> </programlisting> @@ -140,13 +143,14 @@ $replace = '<br />'; // Processes \r\n's first so they aren't converted twice. $newstr = str_replace($order, $replace, $str); +echo $newstr, PHP_EOL; // Outputs F because A is replaced with B, then B is replaced with C, and so on... // Finally E is replaced with F, because of left to right replacements. $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); $subject = 'A'; -echo str_replace($search, $replace, $subject); +echo str_replace($search, $replace, $subject), PHP_EOL; // Outputs: apearpearle pear // For the same reason mentioned above @@ -154,7 +158,7 @@ $letters = array('a', 'p'); $fruit = array('apple', 'pear'); $text = 'a p'; $output = str_replace($letters, $fruit, $text); -echo $output; +echo $output, PHP_EOL; ?> ]]> </programlisting> diff --git a/reference/strings/functions/stripos.xml b/reference/strings/functions/stripos.xml index 12789d4464c7..2fbe1aa3dda0 100644 --- a/reference/strings/functions/stripos.xml +++ b/reference/strings/functions/stripos.xml @@ -125,13 +125,13 @@ $pos2 = stripos($mystring2, $findme); // Nope, 'a' is certainly not in 'xyz' if ($pos1 === false) { - echo "The string '$findme' was not found in the string '$mystring1'"; + echo "The string '$findme' was not found in the string '$mystring1'", PHP_EOL; } // Note our use of ===. Simply == would not work as expected // because the position of 'a' is the 0th (first) character. if ($pos2 !== false) { - echo "We found '$findme' in '$mystring2' at position $pos2"; + echo "We found '$findme' in '$mystring2' at position $pos2", PHP_EOL; } ?> ]]> diff --git a/reference/strings/functions/stristr.xml b/reference/strings/functions/stristr.xml index 1c0fde5b9b5d..e66534c97cba 100644 --- a/reference/strings/functions/stristr.xml +++ b/reference/strings/functions/stristr.xml @@ -108,8 +108,8 @@ <![CDATA[ <?php $email = 'USER@EXAMPLE.com'; - echo stristr($email, 'e'); // outputs ER@EXAMPLE.com - echo stristr($email, 'e', true); // outputs US + echo stristr($email, 'e'), PHP_EOL; // outputs ER@EXAMPLE.com + echo stristr($email, 'e', true), PHP_EOL; // outputs US ?> ]]> </programlisting> @@ -122,25 +122,11 @@ <![CDATA[ <?php $string = 'Hello World!'; - if(stristr($string, 'earth') === FALSE) { + if (stristr($string, 'earth') === FALSE) { echo '"earth" not found in string'; } // outputs: "earth" not found in string ?> -]]> - </programlisting> - </example> - </para> - <para> - <example> - <title>Using a non "string" needle</title> - <programlisting role="php"> -<![CDATA[ -<?php - $string = 'APPLE'; - echo stristr($string, 97); // 97 = lowercase a -// outputs: APPLE -?> ]]> </programlisting> </example> diff --git a/reference/strings/functions/strlen.xml b/reference/strings/functions/strlen.xml index ca6f2e357649..b08dc2345a9a 100644 --- a/reference/strings/functions/strlen.xml +++ b/reference/strings/functions/strlen.xml @@ -49,10 +49,10 @@ <![CDATA[ <?php $str = 'abcdef'; -echo strlen($str); // 6 +echo strlen($str), PHP_EOL; // 6 $str = ' ab cd '; -echo strlen($str); // 7 +echo strlen($str), PHP_EOL; // 7 ?> ]]> </programlisting> diff --git a/reference/strings/functions/strnatcmp.xml b/reference/strings/functions/strnatcmp.xml index c6234e66a2b9..3f327408a541 100644 --- a/reference/strings/functions/strnatcmp.xml +++ b/reference/strings/functions/strnatcmp.xml @@ -73,7 +73,8 @@ An example of the difference between this algorithm and the regular computer string sorting algorithms (used in <function>strcmp</function>) can be seen below: - <informalexample> + <example> + <title><function>strcmp</function></title> <programlisting role="php"> <![CDATA[ <?php @@ -109,7 +110,7 @@ Array ) ]]> </screen> - </informalexample> + </example> For more information see: Martin Pool's <link xlink:href="&url.strnatcmp;">Natural Order String Comparison</link> page. diff --git a/reference/strings/functions/strpbrk.xml b/reference/strings/functions/strpbrk.xml index d826538ac5b7..a9d877bdb9eb 100644 --- a/reference/strings/functions/strpbrk.xml +++ b/reference/strings/functions/strpbrk.xml @@ -63,10 +63,10 @@ $text = 'This is a Simple text.'; // this echoes "is is a Simple text." because 'i' is matched first -echo strpbrk($text, 'mi'); +echo strpbrk($text, 'mi'), PHP_EOL; // this echoes "Simple text." because chars are case sensitive -echo strpbrk($text, 'S'); +echo strpbrk($text, 'S'), PHP_EOL; ?> ]]> </programlisting> diff --git a/reference/strings/functions/strpos.xml b/reference/strings/functions/strpos.xml index a3cf1c465d63..a8b1aeed6aef 100644 --- a/reference/strings/functions/strpos.xml +++ b/reference/strings/functions/strpos.xml @@ -159,6 +159,8 @@ if ($pos !== false) { // We can search for the character, ignoring anything before the offset $newstring = 'abcdef abcdef'; $pos = strpos($newstring, 'a', 1); // $pos = 7, not 0 + +echo $pos, PHP_EOL; ?> ]]> </programlisting> diff --git a/reference/strings/functions/strrpos.xml b/reference/strings/functions/strrpos.xml index 70095f7b2455..b2b022aa9986 100644 --- a/reference/strings/functions/strrpos.xml +++ b/reference/strings/functions/strrpos.xml @@ -128,6 +128,7 @@ <programlisting role="php"> <![CDATA[ <?php +$mystring = 'Elephpant'; $pos = strrpos($mystring, "b"); if ($pos === false) { // note: three equal signs diff --git a/reference/strings/functions/strstr.xml b/reference/strings/functions/strstr.xml index daa033188327..591d8fc05bb4 100644 --- a/reference/strings/functions/strstr.xml +++ b/reference/strings/functions/strstr.xml @@ -118,10 +118,10 @@ <?php $email = 'name@example.com'; $domain = strstr($email, '@'); -echo $domain; // prints @example.com +echo $domain, PHP_EOL; // prints @example.com $user = strstr($email, '@', true); -echo $user; // prints name +echo $user, PHP_EOL; // prints name ?> ]]> </programlisting> diff --git a/reference/strings/functions/strtok.xml b/reference/strings/functions/strtok.xml index d6774a980227..e1a3076198ac 100644 --- a/reference/strings/functions/strtok.xml +++ b/reference/strings/functions/strtok.xml @@ -122,7 +122,7 @@ $string = "This is\tan example\nstring"; $tok = strtok($string, " \n\t"); while ($tok !== false) { - echo "Word=$tok<br />"; + echo "Word={$tok}\n"; $tok = strtok(" \n\t"); } ?> diff --git a/reference/strings/functions/strtr.xml b/reference/strings/functions/strtr.xml index 7619b64efdfa..d746a319e4d2 100644 --- a/reference/strings/functions/strtr.xml +++ b/reference/strings/functions/strtr.xml @@ -114,9 +114,12 @@ <programlisting role="php"> <![CDATA[ <?php -//In this form, strtr() does byte-by-byte translation -//Therefore, we are assuming a single-byte encoding here: +$addr = "The river å"; + +// In this form, strtr() does byte-by-byte translation +// Therefore, we are assuming a single-byte encoding here: $addr = strtr($addr, "äåö", "aao"); +echo $addr, PHP_EOL; ?> ]]> </programlisting> diff --git a/reference/strings/functions/substr-compare.xml b/reference/strings/functions/substr-compare.xml index 2adb6354fa45..5acd02a05fae 100644 --- a/reference/strings/functions/substr-compare.xml +++ b/reference/strings/functions/substr-compare.xml @@ -127,13 +127,13 @@ <programlisting role="php"> <![CDATA[ <?php -echo substr_compare("abcde", "bc", 1, 2); // 0 -echo substr_compare("abcde", "de", -2, 2); // 0 -echo substr_compare("abcde", "bcg", 1, 2); // 0 -echo substr_compare("abcde", "BC", 1, 2, true); // 0 -echo substr_compare("abcde", "bc", 1, 3); // 1 -echo substr_compare("abcde", "cd", 1, 2); // -1 -echo substr_compare("abcde", "abc", 5, 1); // warning +echo substr_compare("abcde", "bc", 1, 2), PHP_EOL; // 0 +echo substr_compare("abcde", "de", -2, 2), PHP_EOL; // 0 +echo substr_compare("abcde", "bcg", 1, 2), PHP_EOL; // 0 +echo substr_compare("abcde", "BC", 1, 2, true), PHP_EOL; // 0 +echo substr_compare("abcde", "bc", 1, 3), PHP_EOL; // 1 +echo substr_compare("abcde", "cd", 1, 2), PHP_EOL; // -1 +echo substr_compare("abcde", "abc", 5, 1), PHP_EOL; // -1 ?> ]]> </programlisting> diff --git a/reference/strings/functions/substr-count.xml b/reference/strings/functions/substr-count.xml index ccddb7cae464..34f64136bfb1 100644 --- a/reference/strings/functions/substr-count.xml +++ b/reference/strings/functions/substr-count.xml @@ -119,23 +119,22 @@ <![CDATA[ <?php $text = 'This is a test'; -echo strlen($text); // 14 +echo strlen($text), PHP_EOL; // 14 -echo substr_count($text, 'is'); // 2 +echo substr_count($text, 'is'), PHP_EOL; // 2 // the string is reduced to 's is a test', so it prints 1 -echo substr_count($text, 'is', 3); +echo substr_count($text, 'is', 3), PHP_EOL; // the text is reduced to 's i', so it prints 0 -echo substr_count($text, 'is', 3, 3); - -// generates a warning because 5+10 > 14 -echo substr_count($text, 'is', 5, 10); - +echo substr_count($text, 'is', 3, 3), PHP_EOL; // prints only 1, because it doesn't count overlapped substrings $text2 = 'gcdgcdgcd'; -echo substr_count($text2, 'gcdgcd'); +echo substr_count($text2, 'gcdgcd'), PHP_EOL; + +// throws an exception because 5+10 > 14 +echo substr_count($text, 'is', 5, 10), PHP_EOL; ?> ]]> </programlisting> diff --git a/reference/strings/functions/substr.xml b/reference/strings/functions/substr.xml index f3cb6697074f..2f72222d9a1a 100644 --- a/reference/strings/functions/substr.xml +++ b/reference/strings/functions/substr.xml @@ -59,9 +59,9 @@ <programlisting role="php"> <![CDATA[ <?php -$rest = substr("abcdef", -1); // returns "f" -$rest = substr("abcdef", -2); // returns "ef" -$rest = substr("abcdef", -3, 1); // returns "d" +echo substr("abcdef", -1), PHP_EOL; // returns "f" +echo substr("abcdef", -2), PHP_EOL; // returns "ef" +echo substr("abcdef", -3, 1), PHP_EOL; // returns "d" ?> ]]> </programlisting> @@ -100,10 +100,10 @@ $rest = substr("abcdef", -3, 1); // returns "d" <programlisting role="php"> <![CDATA[ <?php -$rest = substr("abcdef", 0, -1); // returns "abcde" -$rest = substr("abcdef", 2, -1); // returns "cde" -$rest = substr("abcdef", 4, -4); // returns ""; prior to PHP 8.0.0, false was returned -$rest = substr("abcdef", -3, -1); // returns "de" +echo substr("abcdef", 0, -1), PHP_EOL; // returns "abcde" +echo substr("abcdef", 2, -1), PHP_EOL; // returns "cde" +echo substr("abcdef", 4, -4), PHP_EOL; // returns ""; prior to PHP 8.0.0, false was returned +echo substr("abcdef", -3, -1), PHP_EOL; // returns "de" ?> ]]> </programlisting> @@ -160,19 +160,19 @@ $rest = substr("abcdef", -3, -1); // returns "de" <programlisting role="php"> <![CDATA[ <?php -echo substr('abcdef', 1); // bcdef -echo substr("abcdef", 1, null); // bcdef; prior to PHP 8.0.0, empty string was returned -echo substr('abcdef', 1, 3); // bcd -echo substr('abcdef', 0, 4); // abcd -echo substr('abcdef', 0, 8); // abcdef -echo substr('abcdef', -1, 1); // f +echo substr('abcdef', 1), PHP_EOL; // bcdef +echo substr("abcdef", 1, null), PHP_EOL; // bcdef; prior to PHP 8.0.0, empty string was returned +echo substr('abcdef', 1, 3), PHP_EOL; // bcd +echo substr('abcdef', 0, 4), PHP_EOL; // abcd +echo substr('abcdef', 0, 8), PHP_EOL; // abcdef +echo substr('abcdef', -1, 1), PHP_EOL; // f // Accessing single characters in a string // can also be achieved using "square brackets" $string = 'abcdef'; -echo $string[0]; // a -echo $string[3]; // d -echo $string[strlen($string)-1]; // f +echo $string[0], PHP_EOL; // a +echo $string[3], PHP_EOL; // d +echo $string[strlen($string)-1], PHP_EOL; // f ?> ]]> @@ -189,13 +189,13 @@ class apple { } } -echo "1) ".var_export(substr("pear", 0, 2), true).PHP_EOL; -echo "2) ".var_export(substr(54321, 0, 2), true).PHP_EOL; -echo "3) ".var_export(substr(new apple(), 0, 2), true).PHP_EOL; -echo "4) ".var_export(substr(true, 0, 1), true).PHP_EOL; -echo "5) ".var_export(substr(false, 0, 1), true).PHP_EOL; -echo "6) ".var_export(substr("", 0, 1), true).PHP_EOL; -echo "7) ".var_export(substr(1.2e3, 0, 4), true).PHP_EOL; +echo "1) ", var_export(substr("pear", 0, 2), true), PHP_EOL; +echo "2) ", var_export(substr(54321, 0, 2), true), PHP_EOL; +echo "3) ", var_export(substr(new apple(), 0, 2), true), PHP_EOL; +echo "4) ", var_export(substr(true, 0, 1), true), PHP_EOL; +echo "5) ", var_export(substr(false, 0, 1), true), PHP_EOL; +echo "6) ", var_export(substr("", 0, 1), true), PHP_EOL; +echo "7) ", var_export(substr(1.2e3, 0, 4), true), PHP_EOL; ?> ]]> </programlisting> diff --git a/reference/strings/functions/ucfirst.xml b/reference/strings/functions/ucfirst.xml index 5ba5efe8e680..1b1a91dc9029 100644 --- a/reference/strings/functions/ucfirst.xml +++ b/reference/strings/functions/ucfirst.xml @@ -69,11 +69,11 @@ <![CDATA[ <?php $foo = 'hello world!'; -$foo = ucfirst($foo); // Hello world! +echo ucfirst($foo), PHP_EOL; // Hello world! $bar = 'HELLO WORLD!'; -$bar = ucfirst($bar); // HELLO WORLD! -$bar = ucfirst(strtolower($bar)); // Hello world! +echo ucfirst($bar), PHP_EOL; // HELLO WORLD! +echo ucfirst(strtolower($bar)), PHP_EOL; // Hello world! ?> ]]> </programlisting> diff --git a/reference/strings/functions/ucwords.xml b/reference/strings/functions/ucwords.xml index fd20dbf2ff73..8db161bb92dc 100644 --- a/reference/strings/functions/ucwords.xml +++ b/reference/strings/functions/ucwords.xml @@ -88,11 +88,11 @@ <![CDATA[ <?php $foo = 'hello world!'; -$foo = ucwords($foo); // Hello World! +echo ucwords($foo), PHP_EOL; // Hello World! $bar = 'HELLO WORLD!'; -$bar = ucwords($bar); // HELLO WORLD! -$bar = ucwords(strtolower($bar)); // Hello World! +echo ucwords($bar), PHP_EOL; // HELLO WORLD! +echo ucwords(strtolower($bar)), PHP_EOL; // Hello World! ?> ]]> </programlisting> @@ -106,9 +106,9 @@ $bar = ucwords(strtolower($bar)); // Hello World! <![CDATA[ <?php $foo = 'hello|world!'; -$bar = ucwords($foo); // Hello|world! +echo ucwords($foo), PHP_EOL; // Hello|world! -$baz = ucwords($foo, "|"); // Hello|World! +echo ucwords($foo, "|"), PHP_EOL; // Hello|World! ?> ]]> </programlisting> @@ -122,9 +122,9 @@ $baz = ucwords($foo, "|"); // Hello|World! <![CDATA[ <?php $foo = "mike o'hara"; -$bar = ucwords($foo); // Mike O'hara +echo ucwords($foo), PHP_EOL; // Mike O'hara -$baz = ucwords($foo, " \t\r\n\f\v'"); // Mike O'Hara +echo ucwords($foo, " \t\r\n\f\v'"), PHP_EOL; // Mike O'Hara ?> ]]> </programlisting> diff --git a/reference/strings/functions/vfprintf.xml b/reference/strings/functions/vfprintf.xml index 7174dbe50511..97a4d50297b0 100644 --- a/reference/strings/functions/vfprintf.xml +++ b/reference/strings/functions/vfprintf.xml @@ -75,6 +75,9 @@ if (!($fp = fopen('date.txt', 'w'))) return; +$year = 2025; +$month = 5; +$day = 6; vfprintf($fp, "%04d-%02d-%02d", array($year, $month, $day)); // will write the formatted ISO date to date.txt ?>

« previous php.doc.cvs (#22657) next »