PHP path-PHP Basics)

Source: Internet
Author: User
Tags php basics php switch
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.

 

  • If statement
Switch statement

The Switch statement is a good way to avoid endless input of if and elseif, but pay attention to the following points:

  • The Switch statement only compares values and does not care about the type (equivalent to '= ')
  • Branches one by one until a matching item is found. If no match is found, use the default Branch (if defined)
  • If the code body of the matching item does not have a 'Break' statement, it continues to execute each of the following branches until it encounters a break/return Statement.
  • In a function, using 'Return 'can reduce the use of 'Break', because 'Return' can end the function.

       

  • Switch statement

  • PHP switch
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
  • If the code line exceeds the recommended line length (120 characters), you should consider splicing the code line.
  • To facilitate reading, we recommend that you use the concatenation operator instead of the concatenation assignment operator.
  • In the original namespace of the variable, when a new line is used for splicing, it should be indented.

       

  • String Operators

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:

  • Strings that do not need to be parsed
  • Write the variable as a plain text value (Writing of a variable into plain text)

       

  • Single quotes

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:

  • Escape string
  • String containing multiple variables and plain text
  • Compress multiple lines to improve readability

       // Double quotation marks allow us to use resolvable strings

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.

 

  • Double quotation marks

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.

 

  • Nowdoc syntax

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.

 

  • Heredoc syntax
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.

 

  • Ternary Operators
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.

 

  • Performance skills

Original article address: the path to PHP-php basics (translated). Thank you for sharing it with the original author.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.