Home > Backend Development > PHP Tutorial > Four ways to define strings in PHP

Four ways to define strings in PHP

不言
Release: 2023-03-23 20:18:02
Original
3892 people have browsed it

This article mainly introduces the four ways to define strings in PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

1. Single quotes

In single quotes, any special characters will be output as they are [except \\ and \' will be escaped and output] only Only single quotes, backslashes are escaped

##2, double quotes

In double quotes, the variable will be parsed when $ is encountered;

#Backslashes are escaped in double quotes Escape characters for bar;

##The difference between single and double quotes

# Double quotes will replace the value of the variable, while single quotes will output it as a string.

Support for escaping

Performance and speed issues, since the string in double quotes needs to detect whether it contains a variable modified with the $ symbol, so in theory, single quotes are faster (Single quotes are more efficient)

3. heredoc

heredoc can be understood as not using double quotes Quotes are used to define a string, but the effect is the same as using double quotes. [Double quotes are equivalent to ordinary characters at this time] (long string)

Grammar requirements :

1. The start tag and the end tag use the same string, usually written in uppercase letters.

2. No spaces or extra characters can appear after the opening tag.

3. The closing tag must be written at the beginning, without indentation or spaces, and there must be a semicolon at the end of the closing tag.

4. Variables between the start tag and the end tag can be parsed normally, but functions cannot.

At this time, the double quotation mark is just an ordinary character; other effects are the same as those of the double quotation mark string

Example:

<?php
$var = &#39;this is suifeng&#39;;
echo <<<"ETO"
$var <br/>
"666666"
ETO
Copy after login


##Output:

This is suifeng

##​ 666666

##4、

nowdoc

Similarly, nowdoc does not use single Quotes are used to define a string, but the effect is equivalent to using single quotes to define a string. [Single quotes are equivalent to ordinary characters at this time]


##data Type

Three major data types [scalar, consistent, special]

Floating point type

Floating point type cannot be used in comparison operators

Boolean type

Seven cases of false

Integer 0, floating point 0.0, Boolean false, empty string, 0 string, empty array, null

Array type

Super global array

  • ## $GLOBALS

  • $_SERVER

  • $_REQUEST

  • $_POST

  • $_GET

  • $_FILES

  • $_ENV

  • $_COOKIE

  • $_SESSION

nullThree cases

Direct assignment to null, undefined variables, variables destroyed by unset()

Constant

Definition

const define

const is faster, It is a language structure, define is a function

define cannot be used to define class constants, const can. Once a constant is defined, it cannot be modified or deleted

Predefined constants (magic constants)

  • __LINE__ : Returns the current line number in the file. It can also be written as __line__.

  • __FILE__: Returns the absolute path of the current file (including the file name).

  • ##__DIR__: Returns the absolute path of the current file (excluding the file name), equivalent to dirname( __FILE__).

  • __FUNCTION__: Returns the name of the current function (or method).

  • __CLASS__: Returns the current class name (including the scope or namespace of the class).

  • __TRAIT__: Returns the current trait name (including the trait's scope or namespace).

  • __METHOD__: Returns the current method name (including class name).

  • __NAMESPACE__: Returns the name of the namespace of the current file.

Related recommendations:

How to define an empty object in PHP

How to define an array in PHP




#

The above is the detailed content of Four ways to define strings in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template