* Examples for uasort, DateTimeZone::construct, file_get_contents, file_put_contents and ini_set


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@275453 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Ross Masters 2009-02-09 21:54:29 +00:00
parent ae9e6ebc46
commit 4375ee59c9
6 changed files with 269 additions and 7 deletions

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.17 $ -->
<!-- $Revision: 1.18 $ -->
<sect1 xml:id="language.types.string">
<title>Strings</title>
@ -534,7 +534,7 @@ echo "This square is $square->{width}00 centimeters broad.";
<simpara>
In fact, any value in the namespace can be included in a
<type>string</type> with this syntax. Simply write the expression the same
way as it would appeared outside the <type>string</type>, and then wrap it
way as it would have appeared outside the <type>string</type>, and then wrap it
in <literal>{</literal> and <literal>}</literal>. Since
<literal>{</literal> can not be escaped, this syntax will only be
recognised when the <literal>$</literal> immediately follows the

View file

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.11 $ -->
<!-- $Revision: 1.12 $ -->
<refentry xml:id="function.uasort" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>uasort</refname>
<refpurpose>Sort an array with a user-defined comparison function and maintain index association</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
@ -22,6 +23,7 @@
element order is significant.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
@ -46,12 +48,71 @@
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.success;
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Basic <function>uasort</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
// Comparison function
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
print_r($array);
// Sort and print the resulting array
uasort($array, 'cmp');
print_r($array);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[a] => 4
[b] => 8
[c] => -1
[d] => -9
[e] => 2
[f] => 5
[g] => 3
[h] => -4
)
Array
(
[d] => -9
[h] => -4
[c] => -1
[e] => 2
[g] => 3
[a] => 4
[f] => 5
[b] => 8
)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.1 $ -->
<!-- $Revision: 1.2 $ -->
<refentry xml:id="datetimezone.construct" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
@ -33,6 +33,53 @@
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns <classname>DateTimeZone</classname> on success.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
This method throws <classname>Exception</classname> if the timezone supplied is not
recognised as a valid timezone.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Catching errors when instantiating <classname>DateTimeZone</classname></title>
<programlisting role="php">
<![CDATA[
<?php
// Error handling by catching exceptions
$timezones = array('Europe/London', 'Mars/Phobos', 'Jupiter/Europa');
foreach ($timezones as $tz) {
try {
$mars = new DateTimeZone($tz);
} catch(Exception $e) {
echo $e->getMessage() . '<br />';
}
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
DateTimeZone::__construct() [datetimezone.--construct]: Unknown or bad timezone (Mars/Phobos)
DateTimeZone::__construct() [datetimezone.--construct]: Unknown or bad timezone (Jupiter/Europa)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.33 $ -->
<!-- $Revision: 1.34 $ -->
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.file-get-contents">
<refnamediv>
<refname>file_get_contents</refname>
@ -154,6 +154,76 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Get and output the source of the PHP.net homepage</title>
<programlisting role="php">
<![CDATA[
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
]]>
</programlisting>
</example>
<example>
<title>Searching within the include_path</title>
<programlisting role="php">
<![CDATA[
<?php
// <= PHP 5
$file = file_get_contents('./people.txt', true);
// > PHP 5
$file = file_get_contents('./people.txt', FILE_USE_INCLUDE_PATH);
?>
]]>
</programlisting>
</example>
<example>
<title>Reading a section of a file</title>
<programlisting role="php">
<![CDATA[
<?php
// Read 50 characters from the 100th character
$section = file_get_contents('./people.txt', NULL, NULL, 20, 14);
var_dump($section);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
string(14) "lle Bjori Ro"
]]>
</screen>
</example>
<example>
<title>Using stream contexts</title>
<programlisting role="php">
<![CDATA[
<?php
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.24 $ -->
<!-- $Revision: 1.25 $ -->
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.file-put-contents">
<refnamediv>
<refname>file_put_contents</refname>
@ -157,6 +157,68 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Simple usage example</title>
<programlisting role="php">
<![CDATA[
<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>
]]>
</programlisting>
</example>
<example>
<title>Using flags</title>
<programlisting role="php">
<![CDATA[
<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>
]]>
</programlisting>
</example>
<example>
<title>Using stream contexts</title>
<programlisting role="php">
<![CDATA[
<?php
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
$person = "John Smith\n";
// Add content to the file using the HTTP headers set above
file_put_contents('people.txt', $person, NULL, $context);
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.49 $ -->
<!-- $Revision: 1.50 $ -->
<refentry xml:id="function.ini-set" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>ini_set</refname>
@ -55,6 +55,28 @@
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Setting an ini option</title>
<programlisting role="php">
<![CDATA[
<?php
echo ini_get('display_errors');
if (!ini_get('display_errors')) {
ini_set('display_errors', 1);
}
echo ini_get('display_errors');
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>