Original article: PHP: TheRightWay
Original article: PHP: The Right Way-The Basics Translator: youngsterxyf comparison operator is often a neglected aspect of PHP, which leads to many unexpected results. One of these problems is due to strict comparison (comparison of Boolean values as integers ). ? Php $ a = 5; // 5 is an integer var_dump ($ a = 5); // compare
Original article: PHP: The Right Way-The Basics
Translator: youngsterxyf
Comparison operator
Comparison operators are often a neglected aspect of PHP, which leads to many unexpected results. One of these problems is due to strict comparison (comparison of Boolean values as integers ).
- Comparison operator
- Comparison List
Conditional statement If statement
When using 'if/else' in a function or class, there is a common misunderstanding that 'else' must be used together to declare potential results. However, if the result is a defined return value, 'else' is not required because 'Return 'ends the function, making 'else' meaningless.
Switch statement
The Switch statement is a good way to avoid endless input of if and elseif, but pay attention to the following points:
Global namespace
When using namespaces, you may find that built-in functions are overwritten by the functions you write. We recommend that you add a backslash before the global function name to fix this problem.
- Global namespace
- Global rules
String concatenation
String type
The string type is a constant feature in the PHP community, but we hope this section can clearly explain the differences between string types and their respective benefits/usage.
Single quotes
Single quotes are the easiest way to create a string, and the execution speed is usually the fastest, because PHP does not parse this string (does not parse whether there is a variable), so single quotes are best suited:
Double quotation marks
Double quotation marks are Swiss Army knives processed by strings, but the execution speed is slow because strings must be parsed. Double quotation marks are most suitable:
When a string created with double quotation marks contains a variable, the variable name is often in contact with another character, which causes PHP to not parse the variable because it is "disguised. To solve this problem, you can use a pair of braces to enclose the variables.
Nowdoc syntax
PHP 5.3 introduces the Nowdoc syntax. Its behavior is the same as that of single quotes, except that it is applicable to the use of multi-line strings without splicing.
Heredoc syntax
The syntax of Heredoc is the same as that of double quotation marks, except that it is applicable to multiline strings without splicing.
Ternary Operators
Ternary operators are a good way to compress code, but they are often abused. When you need multi-layer or nested ternary operators, we recommend that you use only one ternary operator for a line of code to improve code readability.
Use the correct syntax to return a value using the ternary operator.
Variable Declaration
Sometimes programmers try to make the code "clean" by declaring a predefined variable as a different name ". In fact, this will double the memory consumption of the script. In the following example, assume that an example text string contains 1 MB of data. By copying this variable, the script will be increased to 2 MB during execution.
Original article address: the path to PHP-php basics (translated). Thank you for sharing it with the original author.