PHP section 2 data type: string type _ PHP Tutorial

Source: Internet
Author: User
The string type of the data type in section 2 of PHP. The following four methods can be used to define a string in PHP: single quotation marks, double quotation marks, heredoc syntax structure, nowdoc syntax structure (since PHP5.3.0 and later), and single quotation marks can be used to define a string.

One in PHPThe string can beThe following four methods are used for definition:

  • Single quotes
  • Double quotation marks
  • Heredoc syntax structure
  • Nowdoc syntax structure(Since PHP 5.3.0)
Single quotes

DefineStringThe simplest way is to enclose it with single quotes (punctuation marks').

To output a single quotation mark, add a backslash (\). Before single quotes or beforeStringAt the end (\\). Note: If a backslash is added before any other character, the backslash is directly output.

Note:UnlikeDouble quotation marksAndHeredocSyntax structure, in single quotesStringInVariableAnd special characters willNoReplaced.

IfStringIs enclosed in double quotes ("), PHP will parse some escape characters,And single quotesStringSimilarly, if characters other than escape characters are output, the backslash will be printed. Before PHP5.1.1,\ {$ Var}The backslash in is not displayed yet.

Double quotation marksStringThe most important feature is that variables are executed.

Heredoc structure

Third definitionStringThe method is to use heredoc syntax structure:<. After this prompt, you need to define an identifier and then a new line. NextStringThe identifier defined above is used as the end mark.

Identifier referenced at end timeRequiredAt the beginning of a row, the identifier name must follow the PHP rules like other labels: it can only contain letters, numbers, and underscores, and cannot start with numbers or underscores.

Note that the end identifier in additionPossibleThere is a semicolon (;. This means the identifierNot indent, There cannot be any blank or tabs before and after the semicolon. More importantly, the end identifier must be preceded by a New Line label recognized by the local operating system. for example, in UNIX and Mac OS X systems\ NThe end identifier (may have a semicolon) must be followed by a New Line label.

If the end tag is not "clean" due to non-compliance with this rule, PHP considers it not the end identifier and continues searching. If a correct end identifier is not found before the end of the file, PHP will generate a syntax error in the last line.

The Heredoc structure is like a double quotation mark without double quotation marks.StringIn the heredoc structure, the quotation marks do not need to be replaced, but the characters listed above (\ NAnd so on. The variable will be replaced, but in the heredoc structureStringBe careful when expressing complex variables.

Nowdoc structure

Just like the heredoc structure is similar to the double quotation mark string, and the Nowdoc structure is similar to the single quotation mark string. The Nowdoc structure is similar to the heredoc structure, but nowdocNo parsing operation. This structure is suitable for PHP code and other large text without escaping. And SGML The structure is similar to the non-resolved text used to declare a large segment, and the nowdoc structure also has the same features.

A nowdoc structure is also marked with the same structure as heredocs<But the sign following it must be enclosed in single quotes, just like<'Eot'In this way. All the rules of the heredocs structure also apply to the nowdoc structure, especially the rules of the End flag.

Variable parsing

WhenStringWhen it is defined using double quotation marks or heredoc structureChangesWill be parsed.

There are two types of syntax rules:SimpleRule,ComplexRules. Simple syntax rules are the most common and convenient. they can be used inStringAdd variables,ArrayValue, orObjectAttribute.

Complex syntax rules are added after PHP4, and the expressions surrounded by curly brackets are obviously marked.

Simple syntax rules

When the PHP parser encounters a dollar sign ($), It will form a valid variable name as much as possible like many other resolvers. You can define the boundary of a variable name using the asterisk.

To express more complex structures, use complex syntax rules.

Complex syntax rules

Complex syntax rules are not named because of complex structures, but because they can use complex expressions.

Anything you want to useStringThis method can be used for scalar variables, array variables, or object attributes. Just like inStringWrite the expression in other places, and then use curly brackets.{And}Enclose it. Because{Cannot be escaped, only$Stay close{Can be recognized.{\ $To express{$.

Access and modify characters in a string

StringThe character in can start with 0, with a similarArraySquare brackets in the structure contain numbers to search for and modify, such$ Str [42], You can setStringImagineArray. FunctionSubstr ()AndSubstr_replace ()It can be used to implement more than one character.

The number in the Warning square brackets is out of range and will be blank. The non-integer type is converted to an integer, and the non-integer type is converted to an integer. if the non-integer type is invalid,E_NOTICELevel error. a negative number will generateE_NOTICEBut the read is a null string. The specified string is only available for the first character, and the null string is specified as a null byte.

Useful functions and operators

StringYou can use the '.' (vertex) operator to connect. Note the '+' (plus sign) operator.NoThis function.

ForStringOperations have a lot of useful functions.

ReferString functionsFor more information about most functions, see advanced search and replacement functions.Regular Expression functionsOrPerl-type regular expression functions.

In additionURL string functions, There are also functions for encrypting/decrypting strings. (McryptAndMhash).

Finally, you can referCharacter type functions.

Convert to string

A value can be prefixed by adding(String)Or useStrval ()Function. InStringIn the expression,StringWill automatically change, for example, when using a functionEchoOrPrintOr in a variable andStringThis change occurs during comparison.TypeAndType conversionYou can better explain the following, or refer to the number of letters.Settype ().

OneBoolean TRUEThe value is convertedString"1".Boolean FALSEConverted""(Empty string ). This transformation can be performed inBooleanAndString.

OneIntegerOrFloating point numberConverted to numeric literalString(IncludingFloating point numberIn the index section), the floating point using the index counting method (4.1E + 6.

ArrayConvertString "Array", Therefore,EchoAndPrintC cannot display thisArray. If an array value is displayed, you can useEcho $ arr ['foo']For more information about this structure, see the following.

In PHP 4ObjectConvertedString "Object"To debug, you need to print the object value. for the method, see the body. You can useGet_class ()Function. In PHP5, you can use_ ToString.

ResourcesAlways transformed"Resource id #1"This structureString, Where1Is a unique number allocated to the resource by PHP. You don't need to pay too much attention to this structure. it is about to change. To getResourceType. functions can be used.Get_resource_type ().

NULLIt is always converted into an empty string.

As mentioned aboveArray,ObjectOrResourcesConvertStringNo more information exceeded. Functions availablePrint_r ()AndVar_dump ()List these types of content.

Most PHP values can be convertedStringS is used for long-term storage, which is called serialization and can be used as a function.Serialize (). If PHP engine settings are supportedWDDXPHP values can also be stored in XML format.

Convert string to numeric

WhenStringThe result and type are as follows:

IfStringThe value does not contain '.', 'e', or 'e', and the numeric value must be of the integer type (PHP_INT_MAXDefined ).StringCan be considered asInteger, Otherwise identified asFloat.

StringThe starting part of is given its value, ifStringStart with a valid number, which can be used directly. Otherwise, the value is 0 (0 ). A valid value is a symbol followed by one or more digits (which may have a decimal point), followed by an optional index symbol, such as 'E' or 'e ', followed by one or more numbers.

Do not convert an integer into a corresponding character as in c. use a functionOrd ()AndChr ()Converts ASCII codes and characters.

The sequence string can be defined in the following four ways: single quotation marks double quotation marks heredoc syntax structure nowdoc syntax structure (since PHP 5.3.0) single quotation marks define a string...

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.