1
0
mirror of https://github.com/php/doc-en.git synced 2026-03-23 23:32:18 +01:00

fix double space in various language pages (#4439)

This commit is contained in:
Philippe DELENTE
2025-02-04 20:26:08 +01:00
committed by GitHub
parent 262ca5a532
commit f4f96ef8b2
9 changed files with 138 additions and 138 deletions

View File

@@ -54,7 +54,7 @@
<para>
If a file contains only PHP code, it is preferable to omit the PHP closing tag
at the end of the file. This prevents accidental whitespace or new lines
at the end of the file. This prevents accidental whitespace or new lines
being added after the PHP closing tag, which may cause unwanted effects
because PHP will start output buffering when there is no intention from
the programmer to send any output at that point in the script.
@@ -79,7 +79,7 @@ echo "Last statement";
<title>Escaping from HTML</title>
<para>
Everything outside of a pair of opening and closing tags is ignored by the
PHP parser which allows PHP files to have mixed content. This allows PHP
PHP parser which allows PHP files to have mixed content. This allows PHP
to be embedded in HTML documents, for example to create templates.
<informalexample>
<programlisting role="php">
@@ -233,7 +233,7 @@ But newline now
</para>
<simpara>
'C' style comments end at the first <literal>*/</literal> encountered.
Make sure you don't nest 'C' style comments. It is easy to make this
Make sure you don't nest 'C' style comments. It is easy to make this
mistake if you are trying to comment out a large block of code.
</simpara>
<para>

View File

@@ -253,7 +253,7 @@ echo ANIMALS[1]; // outputs "cat"
<title>Magic constants</title>
<para>
There are a few magical constants that change depending on
where they are used. For example, the value of
where they are used. For example, the value of
<constant>__LINE__</constant> depends on the line that it's
used on in a script. All these "magical" constants are resolved
at compile time, unlike regular constants, which are resolved at runtime.
@@ -286,7 +286,7 @@ echo ANIMALS[1]; // outputs "cat"
<row xml:id="constant.dir">
<entry><constant>__DIR__</constant></entry>
<entry>
The directory of the file. If used inside an include,
The directory of the file. If used inside an include,
the directory of the included file is returned. This is equivalent
to <literal>dirname(__FILE__)</literal>. This directory name
does not have a trailing slash unless it is the root directory.

View File

@@ -7,14 +7,14 @@
<para>
Enumerations, or "Enums", allow a developer to define a custom type that is limited to one
of a discrete number of possible values. That can be especially helpful when defining a
of a discrete number of possible values. That can be especially helpful when defining a
domain model, as it enables "making invalid states unrepresentable."
</para>
<para>
Enums appear in many languages with a variety of different features. In PHP,
Enums are a special kind of object. The Enum itself is a class, and its possible
cases are all single-instance objects of that class. That means Enum cases are
Enums are a special kind of object. The Enum itself is a class, and its possible
cases are all single-instance objects of that class. That means Enum cases are
valid objects and may be used anywhere an object may be used, including type checks.
</para>
@@ -111,7 +111,7 @@ $a instanceof Suit; // true
<para>
It also means that enum values are never <literal>&lt;</literal> or <literal>&gt;</literal> each other,
since those comparisons are not meaningful on objects. Those comparisons will always return
since those comparisons are not meaningful on objects. Those comparisons will always return
&false; when working with enum values.
</para>
@@ -121,7 +121,7 @@ $a instanceof Suit; // true
</para>
<para>
All Pure Cases are implemented as instances of their enum type. The enum type is represented internally as a class.
All Pure Cases are implemented as instances of their enum type. The enum type is represented internally as a class.
</para>
<para>
@@ -177,18 +177,18 @@ enum Suit: string
<para>
A case that has a scalar equivalent is called a Backed Case, as it is "Backed"
by a simpler value. An Enum that contains all Backed Cases is called a "Backed Enum."
A Backed Enum may contain only Backed Cases. A Pure Enum may contain only Pure Cases.
by a simpler value. An Enum that contains all Backed Cases is called a "Backed Enum."
A Backed Enum may contain only Backed Cases. A Pure Enum may contain only Pure Cases.
</para>
<para>
A Backed Enum may be backed by types of <literal>int</literal> or <literal>string</literal>,
and a given enumeration supports only a single type at a time (that is, no union of <literal>int|string</literal>).
If an enumeration is marked as having a scalar equivalent, then all cases must have a unique
scalar equivalent defined explicitly. There are no auto-generated scalar equivalents
(e.g., sequential integers). Backed cases must be unique; two backed enum cases may
not have the same scalar equivalent. However, a constant may refer to a case, effectively
creating an alias. See <link linkend="language.enumerations.constants">Enumeration constants</link>.
scalar equivalent defined explicitly. There are no auto-generated scalar equivalents
(e.g., sequential integers). Backed cases must be unique; two backed enum cases may
not have the same scalar equivalent. However, a constant may refer to a case, effectively
creating an alias. See <link linkend="language.enumerations.constants">Enumeration constants</link>.
</para>
<para>
@@ -216,7 +216,7 @@ print Suit::Clubs->value;
<para>
In order to enforce the <literal>value</literal> property as read-only, a variable cannot
be assigned as a reference to it. That is, the following throws an error:
be assigned as a reference to it. That is, the following throws an error:
</para>
<programlisting role="php">
@@ -238,13 +238,13 @@ $ref = &$suit->value;
<simplelist>
<member>
<literal>from(int|string): self</literal> will take a scalar and return the corresponding
Enum Case. If one is not found, it will throw a <classname>ValueError</classname>. This is mainly
Enum Case. If one is not found, it will throw a <classname>ValueError</classname>. This is mainly
useful in cases where the input scalar is trusted and a missing enum value should be
considered an application-stopping error.
</member>
<member>
<literal>tryFrom(int|string): ?self</literal> will take a scalar and return the
corresponding Enum Case. If one is not found, it will return <literal>null</literal>.
corresponding Enum Case. If one is not found, it will return <literal>null</literal>.
This is mainly useful in cases where the input scalar is untrusted and the caller wants
to implement their own error handling or default-value logic.
</member>
@@ -252,11 +252,11 @@ $ref = &$suit->value;
<para>
The <literal>from()</literal> and <literal>tryFrom()</literal> methods follow standard
weak/strong typing rules. In weak typing mode, passing an integer or string is acceptable
and the system will coerce the value accordingly. Passing a float will also work and be
coerced. In strict typing mode, passing an integer to <literal>from()</literal> on a
weak/strong typing rules. In weak typing mode, passing an integer or string is acceptable
and the system will coerce the value accordingly. Passing a float will also work and be
coerced. In strict typing mode, passing an integer to <literal>from()</literal> on a
string-backed enum (or vice versa) will result in a <classname>TypeError</classname>,
as will a float in all circumstances. All other parameter types will throw a TypeError
as will a float in all circumstances. All other parameter types will throw a TypeError
in both modes.
</para>
@@ -337,7 +337,7 @@ print Suit::Diamonds->shape(); // prints "Rectangle"
<para>
In this example, all four instances of <literal>Suit</literal> have two methods,
<literal>color()</literal> and <literal>shape()</literal>. As far as calling code
<literal>color()</literal> and <literal>shape()</literal>. As far as calling code
and type checks are concerned, they behave exactly the same as any other object instance.
</para>
@@ -447,8 +447,8 @@ final class Suit implements UnitEnum, Colorful
<title>Enumeration static methods</title>
<para>
Enumerations may also have static methods. The use for static methods on the
enumeration itself is primarily for alternative constructors. E.g.:
Enumerations may also have static methods. The use for static methods on the
enumeration itself is primarily for alternative constructors. E.g.:
</para>
<programlisting role="php">
@@ -513,7 +513,7 @@ enum Size
<para>Enumerations may leverage traits, which will behave the same as on classes.
The caveat is that traits <literal>use</literal>d in an enum must not contain properties.
They may only include methods, static methods, and constants. A trait with properties will
They may only include methods, static methods, and constants. A trait with properties will
result in a fatal error.
</para>
@@ -649,8 +649,8 @@ $foo = new Foo();
<member>Enums may implement any number of interfaces.</member>
<member>
Enums and cases may have <link linkend="language.attributes">attributes</link> attached
to them. The <constant>TARGET_CLASS</constant> target
filter includes Enums themselves. The <constant>TARGET_CLASS_CONST</constant> target filter
to them. The <constant>TARGET_CLASS</constant> target
filter includes Enums themselves. The <constant>TARGET_CLASS_CONST</constant> target filter
includes Enum Cases.
</member>
<member>
@@ -662,14 +662,14 @@ $foo = new Foo();
<para>
The <literal>::class</literal> magic constant on an Enum type evaluates to the type
name including any namespace, exactly the same as an object. The <literal>::class</literal>
name including any namespace, exactly the same as an object. The <literal>::class</literal>
magic constant on a Case instance also evaluates to the Enum type, as it is an
instance of that type.
</para>
<para>
Additionally, enum cases may not be instantiated directly with <literal>new</literal>, nor with
<methodname>ReflectionClass::newInstanceWithoutConstructor</methodname> in reflection. Both will result in an error.
<methodname>ReflectionClass::newInstanceWithoutConstructor</methodname> in reflection. Both will result in an error.
</para>
<programlisting role="php">
@@ -691,7 +691,7 @@ $horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor(
<para>
Both Pure Enums and Backed Enums implement an internal interface named
<interfacename>UnitEnum</interfacename>. <literal>UnitEnum</literal> includes a static method
<interfacename>UnitEnum</interfacename>. <literal>UnitEnum</literal> includes a static method
<literal>cases()</literal>. <literal>cases()</literal> returns a packed array of
all defined Cases in the order of declaration.
</para>
@@ -713,9 +713,9 @@ Suit::cases();
<title>Serialization</title>
<para>
Enumerations are serialized differently from objects. Specifically, they have a new serialization code,
<literal>"E"</literal>, that specifies the name of the enum case. The deserialization routine is then
able to use that to set a variable to the existing singleton value. That ensures that:
Enumerations are serialized differently from objects. Specifically, they have a new serialization code,
<literal>"E"</literal>, that specifies the name of the enum case. The deserialization routine is then
able to use that to set a variable to the existing singleton value. That ensures that:
</para>
<programlisting role="php">
@@ -735,9 +735,9 @@ print serialize(Suit::Hearts);
value a warning will be issued and &false; returned.</para>
<para>
If a Pure Enum is serialized to JSON, an error will be thrown. If a Backed Enum
If a Pure Enum is serialized to JSON, an error will be thrown. If a Backed Enum
is serialized to JSON, it will be represented by its scalar value only, in the
appropriate type. The behavior of both may be overridden by implementing
appropriate type. The behavior of both may be overridden by implementing
<classname>JsonSerializable</classname>.
</para>
@@ -900,7 +900,7 @@ function query($fields, $filter, SortOrder $order = SortOrder::Asc)
<para>
The <literal>query()</literal> function can now proceed safe in the knowledge that
<literal>$order</literal> is guaranteed to be either <literal>SortOrder::Asc</literal>
or <literal>SortOrder::Desc</literal>. Any other value would have resulted in a
or <literal>SortOrder::Desc</literal>. Any other value would have resulted in a
<classname>TypeError</classname>, so no further error checking or testing is needed.
</para>
</example>
@@ -939,7 +939,7 @@ enum UserStatus: string
<para>
In this example, a user's status may be one of, and exclusively, <literal>UserStatus::Pending</literal>,
<literal>UserStatus::Active</literal>, <literal>UserStatus::Suspended</literal>, or
<literal>UserStatus::CanceledByUser</literal>. A function can type a parameter against
<literal>UserStatus::CanceledByUser</literal>. A function can type a parameter against
<literal>UserStatus</literal> and then only accept those four values, period.
</para>

View File

@@ -4,7 +4,7 @@
<title>Exceptions</title>
<para>
PHP has an exception model similar to that of other programming
languages. An exception can be &throw;n, and caught ("&catch;ed") within
languages. An exception can be &throw;n, and caught ("&catch;ed") within
PHP. Code may be surrounded in a &try; block, to facilitate the catching
of potential exceptions. Each &try; must have at least one corresponding
&catch; or &finally; block.
@@ -12,7 +12,7 @@
<para>
If an exception is thrown and its current function scope has no &catch;
block, the exception will "bubble up" the call stack to the calling
function until it finds a matching &catch; block. All &finally; blocks it encounters
function until it finds a matching &catch; block. All &finally; blocks it encounters
along the way will be executed. If the call stack is unwound all the way to the
global scope without encountering a matching &catch; block, the program will
terminate with a fatal error unless a global exception handler has been set.
@@ -29,7 +29,7 @@
<sect1 annotations="chunk:false" xml:id="language.exceptions.catch">
<title><literal>catch</literal></title>
<para>
A &catch; block defines how to respond to a thrown exception. A &catch;
A &catch; block defines how to respond to a thrown exception. A &catch;
block defines one or more types of exception or error it can handle, and
optionally a variable to which to assign the exception. (The variable was
required prior to PHP 8.0.0.) The first &catch; block a thrown exception
@@ -85,9 +85,9 @@
<title>Global exception handler</title>
<para>
If an exception is allowed to bubble up to the global scope, it may be caught
by a global exception handler if set. The <function>set_exception_handler</function>
by a global exception handler if set. The <function>set_exception_handler</function>
function can set a function that will be called in place of a &catch; block if no
other block is invoked. The effect is essentially the same as if the entire program
other block is invoked. The effect is essentially the same as if the entire program
were wrapped in a &try;-&catch; block with that function as the &catch;.
</para>
</sect1>

View File

@@ -3,27 +3,27 @@
<chapter xml:id="language.expressions" xmlns="http://docbook.org/ns/docbook">
<title>Expressions</title>
<simpara>
Expressions are the most important building blocks of PHP. In PHP,
almost anything you write is an expression. The simplest yet
Expressions are the most important building blocks of PHP. In PHP,
almost anything you write is an expression. The simplest yet
most accurate way to define an expression is "anything that has a
value".
</simpara>
<simpara>
The most basic forms of expressions are constants and variables.
When you type <code>$a = 5</code>, you're assigning <code>5</code> into
<varname>$a</varname>. <code>5</code>, obviously,
<varname>$a</varname>. <code>5</code>, obviously,
has the value 5, or in other words <code>5</code> is an expression with the
value of 5 (in this case, <code>5</code> is an integer constant).
</simpara>
<simpara>
After this assignment, you'd expect <varname>$a</varname>'s value to be 5 as
well, so if you wrote <code>$b = $a</code>, you'd expect it to behave just as
if you wrote <code>$b = 5</code>. In other words, <varname>$a</varname> is an expression with the
value of 5 as well. If everything works right, this is exactly
if you wrote <code>$b = 5</code>. In other words, <varname>$a</varname> is an expression with the
value of 5 as well. If everything works right, this is exactly
what will happen.
</simpara>
<para>
Slightly more complex examples for expressions are functions. For
Slightly more complex examples for expressions are functions. For
instance, consider the following function:
<informalexample>
<programlisting role="php">
@@ -43,14 +43,14 @@ function foo ()
not, take a look at the chapter about <link
linkend="language.functions">functions</link>), you'd assume
that typing <code>$c = foo()</code> is essentially just like
writing <code>$c = 5</code>, and you're right. Functions
are expressions with the value of their return value. Since <code>foo()</code>
returns 5, the value of the expression '<code>foo()</code>' is 5. Usually
writing <code>$c = 5</code>, and you're right. Functions
are expressions with the value of their return value. Since <code>foo()</code>
returns 5, the value of the expression '<code>foo()</code>' is 5. Usually
functions don't just return a static value but compute something.
</simpara>
<simpara>
Of course, values in PHP don't have to be integers, and very often
they aren't. PHP supports four scalar value types: <type>int</type>
they aren't. PHP supports four scalar value types: <type>int</type>
values, floating point values (<type>float</type>), <type>string</type>
values and <type>bool</type> values (scalar values are values that you
can't 'break' into smaller pieces, unlike arrays, for instance). PHP also
@@ -59,34 +59,34 @@ function foo ()
</simpara>
<simpara>
PHP takes expressions much further, in the same way many other languages
do. PHP is an expression-oriented language, in the
sense that almost everything is an expression. Consider the
example we've already dealt with, <code>$a = 5</code>. It's easy to see that
do. PHP is an expression-oriented language, in the
sense that almost everything is an expression. Consider the
example we've already dealt with, <code>$a = 5</code>. It's easy to see that
there are two values involved here, the value of the integer
constant <code>5</code>, and the value of <varname>$a</varname> which is being updated to 5 as
well. But the truth is that there's one additional value involved
here, and that's the value of the assignment itself. The
well. But the truth is that there's one additional value involved
here, and that's the value of the assignment itself. The
assignment itself evaluates to the assigned value, in this case 5.
In practice, it means that <code>$a = 5</code>, regardless of what it does,
is an expression with the value 5. Thus, writing something like
is an expression with the value 5. Thus, writing something like
<code>$b = ($a = 5)</code> is like writing
<code>$a = 5; $b = 5;</code> (a semicolon
marks the end of a statement). Since assignments are parsed in a
marks the end of a statement). Since assignments are parsed in a
right to left order, you can also write <code>$b = $a = 5</code>.
</simpara>
<simpara>
Another good example of expression orientation is pre- and
post-increment and decrement. Users of PHP and many other
post-increment and decrement. Users of PHP and many other
languages may be familiar with the notation of <code>variable++</code> and
<code>variable--</code>. These are <link linkend="language.operators.increment">
increment and decrement operators</link>. In PHP, like in C, there
<code>variable--</code>. These are <link linkend="language.operators.increment">
increment and decrement operators</link>. In PHP, like in C, there
are two types of increment - pre-increment and post-increment.
Both pre-increment and post-increment essentially increment the
variable, and the effect on the variable is identical. The
variable, and the effect on the variable is identical. The
difference is with the value of the increment expression.
Pre-increment, which is written <code>++$variable</code>, evaluates to the
incremented value (PHP increments the variable before reading its
value, thus the name 'pre-increment'). Post-increment, which is
value, thus the name 'pre-increment'). Post-increment, which is
written <code>$variable++</code> evaluates to the original value of
<varname>$variable</varname>, before it was incremented (PHP increments the variable
after reading its value, thus the name 'post-increment').
@@ -104,27 +104,27 @@ function foo ()
</simpara>
<simpara>
The last example of expressions we'll deal with here is combined
operator-assignment expressions. You already know that if you
operator-assignment expressions. You already know that if you
want to increment <varname>$a</varname> by 1, you can simply write
<code>$a++</code> or <code>++$a</code>.
But what if you want to add more than one to it, for instance 3?
You could write <code>$a++</code> multiple times, but this
is obviously not a very efficient or comfortable way. A much more
is obviously not a very efficient or comfortable way. A much more
common practice is to write <code>$a =
$a + 3</code>. <code>$a + 3</code> evaluates
$a + 3</code>. <code>$a + 3</code> evaluates
to the value of <varname>$a</varname> plus 3, and is assigned back
into <varname>$a</varname>, which results in incrementing <varname>$a</varname>
by 3. In PHP, as in several other languages like C, you can write this
by 3. In PHP, as in several other languages like C, you can write this
in a shorter way, which with time would become clearer and quicker to
understand as well. Adding 3 to the current value of <varname>$a</varname>
can be written <code>$a += 3</code>. This means exactly
can be written <code>$a += 3</code>. This means exactly
"take the value of <varname>$a</varname>, add 3 to it, and assign it
back into <varname>$a</varname>". In addition to being shorter and
clearer, this also results in faster execution. The value of
clearer, this also results in faster execution. The value of
<code>$a += 3</code>, like the value of a regular assignment, is
the assigned value. Notice that it is NOT 3, but the combined value
of <varname>$a</varname> plus 3 (this is the value that's
assigned into <varname>$a</varname>). Any two-place operator can be used
assigned into <varname>$a</varname>). Any two-place operator can be used
in this operator-assignment mode, for example <code>$a -= 5</code>
(subtract 5 from the value of <varname>$a</varname>), <code>$b *= 7</code>
(multiply the value of <varname>$b</varname> by 7), etc.
@@ -187,7 +187,7 @@ $h = $g += 10; /* first, $g is incremented by 10 and ends with the
<simpara>
Some expressions can be considered as statements. In
this case, a statement has the form of '<code>expr ;</code>' that is, an
expression followed by a semicolon. In <code>$b = $a = 5;</code>,
expression followed by a semicolon. In <code>$b = $a = 5;</code>,
<code>$a = 5</code> is a valid expression, but it's not a statement
by itself. <code>$b = $a = 5;</code>, however, is a valid statement.
</simpara>

View File

@@ -207,7 +207,7 @@ function takes_array($input)
</para>
<para>
As of PHP 8.0.0, the list of function parameters may include a trailing comma, which
will be ignored. That is particularly useful in cases where the list of parameters is
will be ignored. That is particularly useful in cases where the list of parameters is
long or contains long variable names, making it convenient to list parameters vertically.
</para>
<example>

View File

@@ -10,7 +10,7 @@
<?phpdoc print-version-for="namespaces"?>
<simpara>
What are namespaces? In the broadest definition namespaces are a way of encapsulating
items. This can be seen as an abstract concept in many places. For example, in any
items. This can be seen as an abstract concept in many places. For example, in any
operating system directories serve to group related files, and act as a namespace for
the files within them. As a concrete example, the file <literal>foo.txt</literal> can
exist in both directory <literal>/home/greg</literal> and in <literal>/home/other</literal>,
@@ -44,7 +44,7 @@
</para>
<simpara>
PHP Namespaces provide a way in which to group related classes, interfaces,
functions and constants. Here is an example of namespace syntax in PHP:
functions and constants. Here is an example of namespace syntax in PHP:
</simpara>
<example>
<title>Namespace syntax example</title>
@@ -95,7 +95,7 @@ echo constant($d); // see "Namespaces and dynamic language features" section
</para>
<para>
Namespaces are declared using the <literal>namespace</literal>
keyword. A file containing a namespace must declare the namespace
keyword. A file containing a namespace must declare the namespace
at the top of the file before any other code - with one exception: the
<xref linkend="control-structures.declare" /> keyword.
<example>
@@ -120,7 +120,7 @@ function connect() { /* ... */ }
</simpara>
</note>
The only code construct allowed before a namespace declaration is the
<literal>declare</literal> statement, for defining encoding of a source file. In addition,
<literal>declare</literal> statement, for defining encoding of a source file. In addition,
no non-PHP code may precede a namespace declaration, including extra whitespace:
<example>
<title>Declaring a single namespace</title>
@@ -145,7 +145,7 @@ namespace MyProject; // fatal error - namespace must be the first statement in t
<?phpdoc print-version-for="namespaces"?>
<para>
Much like directories and files, PHP namespaces also contain the ability to specify
a hierarchy of namespace names. Thus, a namespace name can be defined with
a hierarchy of namespace names. Thus, a namespace name can be defined with
sub-levels:
<example>
<title>Declaring a single namespace with hierarchy</title>
@@ -172,7 +172,7 @@ function connect() { /* ... */ }
<titleabbrev>Defining multiple namespaces in the same file</titleabbrev>
<?phpdoc print-version-for="namespaces"?>
<para>
Multiple namespaces may also be declared in the same file. There are two allowed
Multiple namespaces may also be declared in the same file. There are two allowed
syntaxes.
</para>
<para>
@@ -227,12 +227,12 @@ function connect() { /* ... */ }
</para>
<para>
It is strongly discouraged as a coding practice to combine multiple namespaces into
the same file. The primary use case is to combine multiple PHP scripts into the same
the same file. The primary use case is to combine multiple PHP scripts into the same
file.
</para>
<para>
To combine global non-namespaced code with namespaced code, only bracketed syntax is
supported. Global code should be
supported. Global code should be
encased in a namespace statement with no namespace name as in:
<example>
<title>Declaring multiple namespaces and unnamespaced code</title>
@@ -289,47 +289,47 @@ echo MyProject\Connection::start();
<?phpdoc print-version-for="namespaces"?>
<para>
Before discussing the use of namespaces, it is important to understand how PHP
knows which namespaced element your code is requesting. A simple analogy can be made
between PHP namespaces and a filesystem. There are three ways to access a file in a
knows which namespaced element your code is requesting. A simple analogy can be made
between PHP namespaces and a filesystem. There are three ways to access a file in a
file system:
<orderedlist>
<listitem>
<simpara>
Relative file name like <literal>foo.txt</literal>. This resolves to
Relative file name like <literal>foo.txt</literal>. This resolves to
<literal>currentdirectory/foo.txt</literal> where <literal>currentdirectory</literal> is the
directory currently occupied. So if the current directory is
directory currently occupied. So if the current directory is
<literal>/home/foo</literal>, the name resolves to <literal>/home/foo/foo.txt</literal>.
</simpara>
</listitem>
<listitem>
<simpara>
Relative path name like <literal>subdirectory/foo.txt</literal>. This resolves
Relative path name like <literal>subdirectory/foo.txt</literal>. This resolves
to <literal>currentdirectory/subdirectory/foo.txt</literal>.
</simpara>
</listitem>
<listitem>
<simpara>
Absolute path name like <literal>/main/foo.txt</literal>. This resolves
Absolute path name like <literal>/main/foo.txt</literal>. This resolves
to <literal>/main/foo.txt</literal>.
</simpara>
</listitem>
</orderedlist>
The same principle can be applied to namespaced elements in PHP. For
The same principle can be applied to namespaced elements in PHP. For
example, a class name can be referred to in three ways:
<orderedlist>
<listitem>
<simpara>
Unqualified name, or an unprefixed class name like
<literal>$a = new foo();</literal> or
<literal>foo::staticmethod();</literal>. If the current namespace is
<literal>foo::staticmethod();</literal>. If the current namespace is
<literal>currentnamespace</literal>, this resolves to
<literal>currentnamespace\foo</literal>. If
<literal>currentnamespace\foo</literal>. If
the code is global, non-namespaced code, this resolves to <literal>foo</literal>.
</simpara>
<simpara>
One caveat: unqualified names for functions and constants will
resolve to global functions and constants if the namespaced function or constant
is not defined. See <link linkend="language.namespaces.fallback">Using namespaces:
is not defined. See <link linkend="language.namespaces.fallback">Using namespaces:
fallback to global function/constant</link> for details.
</simpara>
</listitem>
@@ -337,9 +337,9 @@ echo MyProject\Connection::start();
<simpara>
Qualified name, or a prefixed class name like
<literal>$a = new subnamespace\foo();</literal> or
<literal>subnamespace\foo::staticmethod();</literal>. If the current namespace is
<literal>subnamespace\foo::staticmethod();</literal>. If the current namespace is
<literal>currentnamespace</literal>, this resolves to
<literal>currentnamespace\subnamespace\foo</literal>. If
<literal>currentnamespace\subnamespace\foo</literal>. If
the code is global, non-namespaced code, this resolves to <literal>subnamespace\foo</literal>.
</simpara>
</listitem>
@@ -347,7 +347,7 @@ echo MyProject\Connection::start();
<simpara>
Fully qualified name, or a prefixed name with global prefix operator like
<literal>$a = new \currentnamespace\foo();</literal> or
<literal>\currentnamespace\foo::staticmethod();</literal>. This always resolves
<literal>\currentnamespace\foo::staticmethod();</literal>. This always resolves
to the literal name specified in the code, <literal>currentnamespace\foo</literal>.
</simpara>
</listitem>
@@ -436,7 +436,7 @@ $c = new \Exception('error'); // instantiates global class Exception
<?phpdoc print-version-for="namespaces"?>
<para>
PHP's implementation of namespaces is influenced by its dynamic nature as a programming
language. Thus, to convert code like the following example into namespaced code:
language. Thus, to convert code like the following example into namespaced code:
<example>
<title>Dynamically accessing elements</title>
<simpara>example1.php:</simpara>
@@ -520,7 +520,7 @@ echo constant('namespacename\constname'), "\n"; // also prints namespaced
</para>
<para>
The value of <constant>__NAMESPACE__</constant> is a string that contains the current
namespace name. In global, un-namespaced code, it contains an empty string.
namespace name. In global, un-namespaced code, it contains an empty string.
<example>
<title>__NAMESPACE__ example, namespaced code</title>
<programlisting role="php">
@@ -565,7 +565,7 @@ function get($classname)
</para>
<para>
The <literal>namespace</literal> keyword can be used to explicitly request
an element from the current namespace or a sub-namespace. It is the namespace
an element from the current namespace or a sub-namespace. It is the namespace
equivalent of the <literal>self</literal> operator for classes.
<example>
<title>the namespace operator, inside a namespace</title>
@@ -612,7 +612,7 @@ $b = namespace\CONSTANT; // assigns value of constant CONSTANT to $b
<?phpdoc print-version-for="namespaces"?>
<para>
The ability to refer to an external fully qualified name with an alias, or importing,
is an important feature of namespaces. This is similar to the
is an important feature of namespaces. This is similar to the
ability of unix-based filesystems to create symbolic links to a file or to a directory.
</para>
<para>
@@ -697,7 +697,7 @@ $obj = new $a; // instantiates object of class Another
</example>
</para>
<para>
In addition, importing only affects unqualified and qualified names. Fully qualified
In addition, importing only affects unqualified and qualified names. Fully qualified
names are absolute, and unaffected by imports.
<example>
<title>Importing and fully qualified names</title>
@@ -819,8 +819,8 @@ function fopen() {
<?phpdoc print-version-for="namespaces"?>
<para>
Inside a namespace, when PHP encounters an unqualified Name in a class name, function or
constant context, it resolves these with different priorities. Class names always
resolve to the current namespace name. Thus to access internal or non-namespaced
constant context, it resolves these with different priorities. Class names always
resolve to the current namespace name. Thus to access internal or non-namespaced
user classes, one must refer to them with their fully qualified Name as in:
<example>
<title>Accessing global classes inside a namespace</title>
@@ -1155,8 +1155,8 @@ A\B::foo(); // calls method "foo" of class "B" from namespace "A\A"
<sect2 xml:id="language.namespaces.faq.shouldicare">
<title>If I don't use namespaces, should I care about any of this?</title>
<para>
No. Namespaces do not affect any existing code in any way, or any
as-yet-to-be-written code that does not contain namespaces. You can
No. Namespaces do not affect any existing code in any way, or any
as-yet-to-be-written code that does not contain namespaces. You can
write this code if you wish:
</para>
<para>
@@ -1424,7 +1424,7 @@ $a = new MyClass; // instantiates class "thing" from namespace another
<para>
There is no name conflict, even though the class <literal>MyClass</literal> exists
within the <literal>my\stuff</literal> namespace, because the MyClass definition is
in a separate file. However, the next example causes a fatal error on name conflict
in a separate file. However, the next example causes a fatal error on name conflict
because MyClass is defined in the same file as the use statement.
<informalexample>
<programlisting role="php">
@@ -1476,7 +1476,7 @@ namespace my\stuff\nested {
<title>Dynamic namespace names (quoted identifiers) should escape backslash</title>
<para>
It is very important to realize that because the backslash is used as an escape character
within strings, it should always be doubled when used inside a string. Otherwise
within strings, it should always be doubled when used inside a string. Otherwise
there is a risk of unintended consequences:
<example>
<title>Dangers of using namespaced names inside a double-quoted string</title>
@@ -1501,7 +1501,7 @@ $obj = new $a;
<para>
Any undefined constant that is unqualified like <literal>FOO</literal> will
produce a notice explaining that PHP assumed <literal>FOO</literal> was the value
of the constant. Any constant, qualified or fully qualified, that contains a
of the constant. Any constant, qualified or fully qualified, that contains a
backslash will produce a fatal error if not found.
<example>
<title>Undefined constants</title>

View File

@@ -14,7 +14,7 @@
information. Instead, they are
<link linkend="features.gc.refcounting-basics">symbol table</link>
aliases. Note that in PHP, variable name and variable content are different, so the same
content can have different names. The closest analogy is with
content can have different names. The closest analogy is with
Unix filenames and files - variable names are directory entries,
while variable content is the file itself. References can be
likened to hardlinking in Unix filesystem.

View File

@@ -303,7 +303,7 @@ function test()
that global variables in C are automatically available to
functions unless specifically overridden by a local definition.
This can cause some problems in that people may inadvertently
change a global variable. In PHP global variables must be
change a global variable. In PHP global variables must be
declared global inside a function if they are going to be used in
that function.
</simpara>
@@ -352,13 +352,13 @@ echo $b;
By declaring
<varname>$a</varname> and <varname>$b</varname> global within the
function, all references to either variable will refer to the
global version. There is no limit to the number of global
global version. There is no limit to the number of global
variables that can be manipulated by a function.
</simpara>
<simpara>
A second way to access variables from the global scope is to use
the special PHP-defined <varname>$GLOBALS</varname> array. The
the special PHP-defined <varname>$GLOBALS</varname> array. The
previous example can be rewritten as:
</simpara>
<para>
@@ -419,9 +419,9 @@ function test_superglobal()
<title>Using <literal>static</literal> variables</title>
<simpara>
Another important feature of variable scoping is the
<emphasis>static</emphasis> variable. A static variable exists
<emphasis>static</emphasis> variable. A static variable exists
only in a local function scope, but it does not lose its value
when program execution leaves this scope. Consider the following
when program execution leaves this scope. Consider the following
example:
</simpara>
<para>
@@ -444,9 +444,9 @@ function test()
<simpara>
This function is quite useless since every time it is called it
sets <varname>$a</varname> to <literal>0</literal> and prints
<literal>0</literal>. The <varname>$a</varname>++ which increments the
<literal>0</literal>. The <varname>$a</varname>++ which increments the
variable serves no purpose since as soon as the function exits the
<varname>$a</varname> variable disappears. To make a useful
<varname>$a</varname> variable disappears. To make a useful
counting function which will not lose track of the current count,
the <varname>$a</varname> variable is declared static:
</simpara>
@@ -475,7 +475,7 @@ function test()
<simpara>
Static variables also provide one way to deal with recursive
functions. The following
functions. The following
simple function recursively counts to 10, using the static
variable <varname>$count</varname> to know when to stop:
</simpara>
@@ -719,8 +719,8 @@ Static object: object(stdClass)#3 (1) {
<simpara>
Sometimes it is convenient to be able to have variable variable
names. That is, a variable name which can be set and used
dynamically. A normal variable is set with a statement such as:
names. That is, a variable name which can be set and used
dynamically. A normal variable is set with a statement such as:
</simpara>
<informalexample>
@@ -735,7 +735,7 @@ $a = 'hello';
<simpara>
A variable variable takes the value of a variable and treats that
as the name of a variable. In the above example,
as the name of a variable. In the above example,
<emphasis>hello</emphasis>, can be used as the name of a variable
by using two dollar signs. i.e.
</simpara>
@@ -753,7 +753,7 @@ $$a = 'world';
<simpara>
At this point two variables have been defined and stored in the
PHP symbol tree: <varname>$a</varname> with contents "hello" and
<varname>$hello</varname> with contents "world". Therefore, this
<varname>$hello</varname> with contents "world". Therefore, this
statement:
</simpara>
@@ -787,11 +787,11 @@ echo "$a $hello";
<simpara>
In order to use variable variables with arrays,
an ambiguity problem has to be resolved. That is, if the parser sees
an ambiguity problem has to be resolved. That is, if the parser sees
<varname>$$a[1]</varname> then it needs to know if
<varname>$a[1]</varname> was meant to be used as a variable, or if
<varname>$$a</varname> was wanted as the variable and then the <literal>[1]</literal>
index from that variable. The syntax for resolving this ambiguity
index from that variable. The syntax for resolving this ambiguity
is: <varname>${$a[1]}</varname> for the first case and
<varname>${$a}[1]</varname> for the second.
</simpara>
@@ -915,7 +915,7 @@ echo $_REQUEST['username'];
<para>
Using a GET form is similar except the appropriate
GET predefined variable can be used instead. GET also applies to the
<literal>QUERY_STRING</literal> (the information after the '?' in a URL). So,
<literal>QUERY_STRING</literal> (the information after the '?' in a URL). So,
for example, <literal>http://www.example.com/test.php?id=3</literal>
contains GET data which is accessible with <varname>$_GET['id']</varname>.
See also <varname>$_REQUEST</varname>.
@@ -933,7 +933,7 @@ echo $_REQUEST['username'];
PHP also understands arrays in the context of form variables
(see the <link linkend="faq.html">related faq</link>).
For example, related variables may be grouped together, or this
feature may be used to retrieve values from a multiple select input. For
feature may be used to retrieve values from a multiple select input. For
example, let's post a form to itself and upon submission display
the data:
</simpara>
@@ -995,7 +995,7 @@ if ($_POST) {
form will be transmitted to the server with two additional
variables, <varname>sub_x</varname> and <varname>sub_y</varname>.
These contain the coordinates of the
user click within the image. The experienced may note that the
user click within the image. The experienced may note that the
actual variable names sent by the browser contains a period
rather than an underscore, but PHP converts the period to an
underscore automatically.
@@ -1009,14 +1009,14 @@ if ($_POST) {
<simpara>
PHP transparently supports HTTP cookies as defined by <link
xlink:href="&url.rfc;6265">RFC 6265</link>. Cookies are a
xlink:href="&url.rfc;6265">RFC 6265</link>. Cookies are a
mechanism for storing data in the remote browser and thus
tracking or identifying return users. It is possible to set cookies using
the <function>setcookie</function> function. Cookies are part of
tracking or identifying return users. It is possible to set cookies using
the <function>setcookie</function> function. Cookies are part of
the HTTP header, so the SetCookie function must be called before
any output is sent to the browser. This is the same restriction
as for the <function>header</function> function. Cookie data
is then available in the appropriate cookie data arrays, such
any output is sent to the browser. This is the same restriction
as for the <function>header</function> function. Cookie data
is then available in the appropriate cookie data arrays, such
as <varname>$_COOKIE</varname> as well as in <varname>$_REQUEST</varname>.
See the <function>setcookie</function> manual page for more details and
examples.
@@ -1031,7 +1031,7 @@ if ($_POST) {
<simpara>
If multiple values should be assigned to a single cookie variable,
they can be assigned as an array. For example:
they can be assigned as an array. For example:
</simpara>
<informalexample>
@@ -1047,16 +1047,16 @@ if ($_POST) {
<simpara>
That will create two separate cookies although <varname>MyCookie</varname> will now
be a single array in the script. If just one cookie should be set
be a single array in the script. If just one cookie should be set
with multiple values, consider using <function>serialize</function> or
<function>explode</function> on the value first.
</simpara>
<simpara>
Note that a cookie will replace a previous cookie by the same
name in the browser unless the path or domain is different. So,
name in the browser unless the path or domain is different. So,
for a shopping cart application a counter may be kept,
and passed along. I.e.
and passed along. I.e.
</simpara>
<example>
@@ -1114,12 +1114,12 @@ $varname.ext; /* invalid variable name */
<para>
Because PHP determines the types of variables and converts them
(generally) as needed, it is not always obvious what type a given
variable is at any one time. PHP includes several functions
variable is at any one time. PHP includes several functions
which find out what type a variable is, such as:
<function>gettype</function>, <function>is_array</function>,
<function>is_float</function>, <function>is_int</function>,
<function>is_object</function>, and
<function>is_string</function>. See also the chapter on
<function>is_string</function>. See also the chapter on
<link linkend="language.types">Types</link>.
</para>
<para>