Global namespace//do not see the following Chinese-English combination look
When using the namespaces, you could find that internal functions (internal (built-in) function) is hidden by functions you wrote. To fix this, refer to the global function by using a backslash before the function name.
<?phpnamespace phptherightway;function fopen () { $file = \fopen (); Our function name is the same as an internal function. Execute the function from the global space by adding '.} function Array () { $iterator = new \arrayiterator (); Arrayiterator is an internal class. Using its name without a backslash //would attempt to resolve it within your namespace.}
Global space:
If no namespace is defined, all classes and functions are defined in the global space, as before the introduction of the namespace concept in PHP. Precede the name with a prefix \ means that the name is the name in the global space, even if the name is in a different namespace.
<?phpnamespace a\b\c;/* This function is A\b\c\fopen */function fopen () { /* ... */ $f = \fopen (...);//Call global fopen function return $f;} ?>
Stringsconcatenation
- If your line extends beyond the recommended line length (+ characters), consider concatenating your line
- For readability it's best to use concatenation operators over concatenating assignment operators
- While within the original scope of the variable, indent when concatenation uses a new line
<?php$a = ' multi-line example '; concatenating assignment operator (. =) $a. = "\ n"; $a. = ' of what ' does '; vs. $a = ' Multi-line example ' //Concaten ation operator (.) . "\ n" //indenting new lines . ' of what and do ';
String types
String types is a constant feature within the PHP community, but hopefully this section would explain the differences betw Een the string types and their benefits/uses.
Single quotes
Single quotes is the simplest-define a string and is often the quickest. Their speed stems from PHP not parsing the string (doesn ' t parse for variables). They ' re best suited for:
- Strings that does not need to be parsed
- Writing of a variable into plain text
<?phpecho ' This was my string, look at how pretty it was. '; No need to parse a simple string/** * Output: * * This is my string and look at how pretty it is. */
Double Quotes
Double quotes is the Swiss Army knife of strings, but is slower due to the string being parsed. They ' re best suited for:
- Escaped strings
- Strings with multiple variables and plain text
- Condensing multi-line concatenation, and improving readability
<?phpecho ' Phptherightway is '. $adjective. '. ' A single quotes example this uses multiple concatenating for . "\ n" //variables and escaped string . ' I love learning '. $code. ‘!‘; Vs.echo "Phptherightway is $adjective. \ n I Love learning $code!" Instead of multiple concatenating, double quotes //enables us to use a parsable string
While using a double quotes that contain variables, it's often the case that the variable would be touching another character . This would result in PHP not parsing the variable due to the variable being camouflaged. To fix this problem, wrap the variable within a pair of curly brackets.
<?php$juice = ' plum '; echo "I drank some juice made of $juices"; $juice cannot be parsedvs. $juice = ' plum '; echo "I drank some juice made of {$juice}s"; $juice'll be parsed/** * Complex variables would also be parsed within curly brackets * * $juice = array (' Apple ', ' orang E ', ' plum '); echo "I drank some juice made of {$juice [1]}s]; $juice [1] would be parsed
Nowdoc syntax Heredoc syntax
See PHP Manual.
Ternary operators (ternary operator)
Ternary operators is a great the condense code, but is often used in excess. While ternary operators can be stacked/nested, it's advised to use one per line for readability.
<?php$a = 5;echo ($a = = 5)? ' Yay ': ' Nay '; vs.//nested ternary$b = 10;echo ($a)? ($a = = 5)? ' Yay ': ' Nay ': ($b = = 10)? ' Excessive ': ':('; Excess nesting, sacrificing readability
To ' return ' a value with ternary operators use the correct syntax.
<?php$a = 5;echo ($a = = 5)? return True:return false; This example would output an errorvs. $a = 5;return ($a = = 5)? ' Yay ': ' Nope '; This example would return ' Yay '
Variable declarations
<?php$about = ' A very long string of text '; Uses 2MB memoryecho $about; Vs.echo ' A very long string of text '; Uses 1MB memory
PHP Basics (ii)