diff --git a/language/basic-syntax.xml b/language/basic-syntax.xml index d54affadf944..48f25616f197 100644 --- a/language/basic-syntax.xml +++ b/language/basic-syntax.xml @@ -54,7 +54,7 @@ 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"; Escaping from HTML 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. @@ -233,7 +233,7 @@ But newline now 'C' style comments end at the first */ 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. diff --git a/language/constants.xml b/language/constants.xml index 974cf3b40f63..094512ae24d9 100644 --- a/language/constants.xml +++ b/language/constants.xml @@ -253,7 +253,7 @@ echo ANIMALS[1]; // outputs "cat" Magic constants 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 __LINE__ 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" __DIR__ - 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 dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. diff --git a/language/enumerations.xml b/language/enumerations.xml index 3401de7dafab..8f3081b5044e 100644 --- a/language/enumerations.xml +++ b/language/enumerations.xml @@ -7,14 +7,14 @@ 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." 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. @@ -111,7 +111,7 @@ $a instanceof Suit; // true It also means that enum values are never < or > 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. @@ -121,7 +121,7 @@ $a instanceof Suit; // true - 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. @@ -177,18 +177,18 @@ enum Suit: string 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. A Backed Enum may be backed by types of int or string, and a given enumeration supports only a single type at a time (that is, no union of int|string). 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 Enumeration constants. + 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 Enumeration constants. @@ -216,7 +216,7 @@ print Suit::Clubs->value; In order to enforce the value 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: @@ -238,13 +238,13 @@ $ref = &$suit->value; from(int|string): self will take a scalar and return the corresponding - Enum Case. If one is not found, it will throw a ValueError. This is mainly + Enum Case. If one is not found, it will throw a ValueError. This is mainly useful in cases where the input scalar is trusted and a missing enum value should be considered an application-stopping error. tryFrom(int|string): ?self will take a scalar and return the - corresponding Enum Case. If one is not found, it will return null. + corresponding Enum Case. If one is not found, it will return null. 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. @@ -252,11 +252,11 @@ $ref = &$suit->value; The from() and tryFrom() 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 from() 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 from() on a string-backed enum (or vice versa) will result in a TypeError, - 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. @@ -337,7 +337,7 @@ print Suit::Diamonds->shape(); // prints "Rectangle" In this example, all four instances of Suit have two methods, - color() and shape(). As far as calling code + color() and shape(). As far as calling code and type checks are concerned, they behave exactly the same as any other object instance. @@ -447,8 +447,8 @@ final class Suit implements UnitEnum, Colorful Enumeration static methods - 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.: @@ -513,7 +513,7 @@ enum Size Enumerations may leverage traits, which will behave the same as on classes. The caveat is that traits used 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. @@ -649,8 +649,8 @@ $foo = new Foo(); Enums may implement any number of interfaces. Enums and cases may have attributes attached - to them. The TARGET_CLASS target - filter includes Enums themselves. The TARGET_CLASS_CONST target filter + to them. The TARGET_CLASS target + filter includes Enums themselves. The TARGET_CLASS_CONST target filter includes Enum Cases. @@ -662,14 +662,14 @@ $foo = new Foo(); The ::class magic constant on an Enum type evaluates to the type - name including any namespace, exactly the same as an object. The ::class + name including any namespace, exactly the same as an object. The ::class magic constant on a Case instance also evaluates to the Enum type, as it is an instance of that type. Additionally, enum cases may not be instantiated directly with new, nor with - ReflectionClass::newInstanceWithoutConstructor in reflection. Both will result in an error. + ReflectionClass::newInstanceWithoutConstructor in reflection. Both will result in an error. @@ -691,7 +691,7 @@ $horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor( Both Pure Enums and Backed Enums implement an internal interface named - UnitEnum. UnitEnum includes a static method + UnitEnum. UnitEnum includes a static method cases(). cases() returns a packed array of all defined Cases in the order of declaration. @@ -713,9 +713,9 @@ Suit::cases(); Serialization - Enumerations are serialized differently from objects. Specifically, they have a new serialization code, - "E", 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, + "E", 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: @@ -735,9 +735,9 @@ print serialize(Suit::Hearts); value a warning will be issued and &false; returned. - 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 JsonSerializable. @@ -900,7 +900,7 @@ function query($fields, $filter, SortOrder $order = SortOrder::Asc) The query() function can now proceed safe in the knowledge that $order is guaranteed to be either SortOrder::Asc - or SortOrder::Desc. Any other value would have resulted in a + or SortOrder::Desc. Any other value would have resulted in a TypeError, so no further error checking or testing is needed. @@ -939,7 +939,7 @@ enum UserStatus: string In this example, a user's status may be one of, and exclusively, UserStatus::Pending, UserStatus::Active, UserStatus::Suspended, or - UserStatus::CanceledByUser. A function can type a parameter against + UserStatus::CanceledByUser. A function can type a parameter against UserStatus and then only accept those four values, period. diff --git a/language/exceptions.xml b/language/exceptions.xml index a71c669d60b6..9b2a5d8f03a6 100644 --- a/language/exceptions.xml +++ b/language/exceptions.xml @@ -4,7 +4,7 @@ Exceptions 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 @@ 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 @@ <literal>catch</literal> - 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 @@ Global exception handler If an exception is allowed to bubble up to the global scope, it may be caught - by a global exception handler if set. The set_exception_handler + by a global exception handler if set. The set_exception_handler 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;. diff --git a/language/expressions.xml b/language/expressions.xml index ef78a1d56500..ed937167113e 100644 --- a/language/expressions.xml +++ b/language/expressions.xml @@ -3,27 +3,27 @@ Expressions - 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". The most basic forms of expressions are constants and variables. When you type $a = 5, you're assigning 5 into - $a. 5, obviously, + $a. 5, obviously, has the value 5, or in other words 5 is an expression with the value of 5 (in this case, 5 is an integer constant). After this assignment, you'd expect $a's value to be 5 as well, so if you wrote $b = $a, you'd expect it to behave just as - if you wrote $b = 5. In other words, $a is an expression with the - value of 5 as well. If everything works right, this is exactly + if you wrote $b = 5. In other words, $a is an expression with the + value of 5 as well. If everything works right, this is exactly what will happen. - Slightly more complex examples for expressions are functions. For + Slightly more complex examples for expressions are functions. For instance, consider the following function: @@ -43,14 +43,14 @@ function foo () not, take a look at the chapter about functions), you'd assume that typing $c = foo() is essentially just like - writing $c = 5, and you're right. Functions - are expressions with the value of their return value. Since foo() - returns 5, the value of the expression 'foo()' is 5. Usually + writing $c = 5, and you're right. Functions + are expressions with the value of their return value. Since foo() + returns 5, the value of the expression 'foo()' is 5. Usually functions don't just return a static value but compute something. Of course, values in PHP don't have to be integers, and very often - they aren't. PHP supports four scalar value types: int + they aren't. PHP supports four scalar value types: int values, floating point values (float), string values and bool 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 () 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, $a = 5. 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, $a = 5. It's easy to see that there are two values involved here, the value of the integer constant 5, and the value of $a 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 $a = 5, 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 $b = ($a = 5) is like writing $a = 5; $b = 5; (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 $b = $a = 5. 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 variable++ and - variable--. These are - increment and decrement operators. In PHP, like in C, there + variable--. These are + increment and decrement operators. 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 ++$variable, 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 $variable++ evaluates to the original value of $variable, before it was incremented (PHP increments the variable after reading its value, thus the name 'post-increment'). @@ -104,27 +104,27 @@ function foo () 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 $a by 1, you can simply write $a++ or ++$a. But what if you want to add more than one to it, for instance 3? You could write $a++ 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 $a = - $a + 3. $a + 3 evaluates + $a + 3. $a + 3 evaluates to the value of $a plus 3, and is assigned back into $a, which results in incrementing $a - 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 $a - can be written $a += 3. This means exactly + can be written $a += 3. This means exactly "take the value of $a, add 3 to it, and assign it back into $a". 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 $a += 3, like the value of a regular assignment, is the assigned value. Notice that it is NOT 3, but the combined value of $a plus 3 (this is the value that's - assigned into $a). Any two-place operator can be used + assigned into $a). Any two-place operator can be used in this operator-assignment mode, for example $a -= 5 (subtract 5 from the value of $a), $b *= 7 (multiply the value of $b by 7), etc. @@ -187,7 +187,7 @@ $h = $g += 10; /* first, $g is incremented by 10 and ends with the Some expressions can be considered as statements. In this case, a statement has the form of 'expr ;' that is, an - expression followed by a semicolon. In $b = $a = 5;, + expression followed by a semicolon. In $b = $a = 5;, $a = 5 is a valid expression, but it's not a statement by itself. $b = $a = 5;, however, is a valid statement. diff --git a/language/functions.xml b/language/functions.xml index 555326acfb92..1979e22c9f22 100644 --- a/language/functions.xml +++ b/language/functions.xml @@ -207,7 +207,7 @@ function takes_array($input) 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. diff --git a/language/namespaces.xml b/language/namespaces.xml index 1f3754597777..714eb87550d3 100644 --- a/language/namespaces.xml +++ b/language/namespaces.xml @@ -10,7 +10,7 @@ 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 foo.txt can exist in both directory /home/greg and in /home/other, @@ -44,7 +44,7 @@ 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: Namespace syntax example @@ -95,7 +95,7 @@ echo constant($d); // see "Namespaces and dynamic language features" section Namespaces are declared using the namespace - 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 keyword. @@ -120,7 +120,7 @@ function connect() { /* ... */ } The only code construct allowed before a namespace declaration is the - declare statement, for defining encoding of a source file. In addition, + declare statement, for defining encoding of a source file. In addition, no non-PHP code may precede a namespace declaration, including extra whitespace: Declaring a single namespace @@ -145,7 +145,7 @@ namespace MyProject; // fatal error - namespace must be the first statement in t 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: Declaring a single namespace with hierarchy @@ -172,7 +172,7 @@ function connect() { /* ... */ } Defining multiple namespaces in the same file - 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. @@ -227,12 +227,12 @@ function connect() { /* ... */ } 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. 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: Declaring multiple namespaces and unnamespaced code @@ -289,47 +289,47 @@ echo MyProject\Connection::start(); 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: - Relative file name like foo.txt. This resolves to + Relative file name like foo.txt. This resolves to currentdirectory/foo.txt where currentdirectory is the - directory currently occupied. So if the current directory is + directory currently occupied. So if the current directory is /home/foo, the name resolves to /home/foo/foo.txt. - Relative path name like subdirectory/foo.txt. This resolves + Relative path name like subdirectory/foo.txt. This resolves to currentdirectory/subdirectory/foo.txt. - Absolute path name like /main/foo.txt. This resolves + Absolute path name like /main/foo.txt. This resolves to /main/foo.txt. - 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: Unqualified name, or an unprefixed class name like $a = new foo(); or - foo::staticmethod();. If the current namespace is + foo::staticmethod();. If the current namespace is currentnamespace, this resolves to - currentnamespace\foo. If + currentnamespace\foo. If the code is global, non-namespaced code, this resolves to foo. 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 Using namespaces: + is not defined. See Using namespaces: fallback to global function/constant for details. @@ -337,9 +337,9 @@ echo MyProject\Connection::start(); Qualified name, or a prefixed class name like $a = new subnamespace\foo(); or - subnamespace\foo::staticmethod();. If the current namespace is + subnamespace\foo::staticmethod();. If the current namespace is currentnamespace, this resolves to - currentnamespace\subnamespace\foo. If + currentnamespace\subnamespace\foo. If the code is global, non-namespaced code, this resolves to subnamespace\foo. @@ -347,7 +347,7 @@ echo MyProject\Connection::start(); Fully qualified name, or a prefixed name with global prefix operator like $a = new \currentnamespace\foo(); or - \currentnamespace\foo::staticmethod();. This always resolves + \currentnamespace\foo::staticmethod();. This always resolves to the literal name specified in the code, currentnamespace\foo. @@ -436,7 +436,7 @@ $c = new \Exception('error'); // instantiates global class Exception 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: Dynamically accessing elements example1.php: @@ -520,7 +520,7 @@ echo constant('namespacename\constname'), "\n"; // also prints namespaced The value of __NAMESPACE__ 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. __NAMESPACE__ example, namespaced code @@ -565,7 +565,7 @@ function get($classname) The namespace 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 self operator for classes. the namespace operator, inside a namespace @@ -612,7 +612,7 @@ $b = namespace\CONSTANT; // assigns value of constant CONSTANT to $b 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. @@ -697,7 +697,7 @@ $obj = new $a; // instantiates object of class Another - 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. Importing and fully qualified names @@ -819,8 +819,8 @@ function fopen() { 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: Accessing global classes inside a namespace @@ -1155,8 +1155,8 @@ A\B::foo(); // calls method "foo" of class "B" from namespace "A\A" If I don't use namespaces, should I care about any of this? - 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: @@ -1424,7 +1424,7 @@ $a = new MyClass; // instantiates class "thing" from namespace another There is no name conflict, even though the class MyClass exists within the my\stuff 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. @@ -1476,7 +1476,7 @@ namespace my\stuff\nested { Dynamic namespace names (quoted identifiers) should escape backslash 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: Dangers of using namespaced names inside a double-quoted string @@ -1501,7 +1501,7 @@ $obj = new $a; Any undefined constant that is unqualified like FOO will produce a notice explaining that PHP assumed FOO 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. Undefined constants diff --git a/language/references.xml b/language/references.xml index 7c8f64b5067b..76a1210bfc87 100644 --- a/language/references.xml +++ b/language/references.xml @@ -14,7 +14,7 @@ information. Instead, they are symbol table 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. diff --git a/language/variables.xml b/language/variables.xml index f38b9210b7df..b2e514334d1d 100644 --- a/language/variables.xml +++ b/language/variables.xml @@ -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. @@ -352,13 +352,13 @@ echo $b; By declaring $a and $b 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. A second way to access variables from the global scope is to use - the special PHP-defined $GLOBALS array. The + the special PHP-defined $GLOBALS array. The previous example can be rewritten as: @@ -419,9 +419,9 @@ function test_superglobal() Using <literal>static</literal> variables Another important feature of variable scoping is the - static variable. A static variable exists + static 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: @@ -444,9 +444,9 @@ function test() This function is quite useless since every time it is called it sets $a to 0 and prints - 0. The $a++ which increments the + 0. The $a++ which increments the variable serves no purpose since as soon as the function exits the - $a variable disappears. To make a useful + $a variable disappears. To make a useful counting function which will not lose track of the current count, the $a variable is declared static: @@ -475,7 +475,7 @@ function test() 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 $count to know when to stop: @@ -719,8 +719,8 @@ Static object: object(stdClass)#3 (1) { 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: @@ -735,7 +735,7 @@ $a = 'hello'; 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, hello, can be used as the name of a variable by using two dollar signs. i.e. @@ -753,7 +753,7 @@ $$a = 'world'; At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello" and - $hello with contents "world". Therefore, this + $hello with contents "world". Therefore, this statement: @@ -787,11 +787,11 @@ echo "$a $hello"; 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 $$a[1] then it needs to know if $a[1] was meant to be used as a variable, or if $$a was wanted as the variable and then the [1] - index from that variable. The syntax for resolving this ambiguity + index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second. @@ -915,7 +915,7 @@ echo $_REQUEST['username']; Using a GET form is similar except the appropriate GET predefined variable can be used instead. GET also applies to the - QUERY_STRING (the information after the '?' in a URL). So, + QUERY_STRING (the information after the '?' in a URL). So, for example, http://www.example.com/test.php?id=3 contains GET data which is accessible with $_GET['id']. See also $_REQUEST. @@ -933,7 +933,7 @@ echo $_REQUEST['username']; PHP also understands arrays in the context of form variables (see the related faq). 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: @@ -995,7 +995,7 @@ if ($_POST) { form will be transmitted to the server with two additional variables, sub_x and sub_y. 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) { PHP transparently supports HTTP cookies as defined by RFC 6265. Cookies are a + xlink:href="&url.rfc;6265">RFC 6265. 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 setcookie function. Cookies are part of + tracking or identifying return users. It is possible to set cookies using + the setcookie 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 header 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 header function. Cookie data + is then available in the appropriate cookie data arrays, such as $_COOKIE as well as in $_REQUEST. See the setcookie manual page for more details and examples. @@ -1031,7 +1031,7 @@ if ($_POST) { 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: @@ -1047,16 +1047,16 @@ if ($_POST) { That will create two separate cookies although MyCookie 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 serialize or explode on the value first. 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. @@ -1114,12 +1114,12 @@ $varname.ext; /* invalid variable name */ 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: gettype, is_array, is_float, is_int, is_object, and - is_string. See also the chapter on + is_string. See also the chapter on Types.