Home Backend Development PHP Tutorial Variable definition PHP variable definition and variable substitution methods

Variable definition PHP variable definition and variable substitution methods

Jul 29, 2016 am 08:40 AM
Variable definitions

There are two ways to replace variables into strings - the simple way and the complex way.
The easy way is to put the variable name in a double quoted string or a heredoc:
$who = 'Kilroy';
$where = 'here';
echo "$who was $where";
Kilroy was here
Complex The method is to enclose the variable to be replaced in curly brackets. This approach can be used to disambiguate or replace array lookups. The classic function of braces is to separate the variable name from the surrounding text:
$n = 12;
echo “You are the {$n}th person”;
You are the 12th person
If there are no braces, PHP will try to print out the value of the variable $nth.
Unlike some shell environments, variables in PHP strings will not be parsed repeatedly, but will only be parsed in double-quoted strings, and the result will be used as the value of the string:
$bar = 'this is not printed ';
$foo = '$bar'; // Single quotes
print("$foo"); // Double quotes
$bar
4.1.2 Strings enclosed in single quotes
Single-Quoted Strings
Use Strings enclosed in single quotes do not replace variables. Because string literals are enclosed in single quotes, variable names are not parsed in the following string:
$name = 'Fred';
$str = 'Hello, $name'; // single-quoted Enclosed in single quotes
echo $str;
Hello, $name
The only escape sequence available in a string enclosed in single quotes is ' (putting single quotes inside a string enclosed in single quotes) , \ (putting a backslash inside a string enclosed in single quotes). Any other backslash will only be interpreted as a backslash:
$name = 'Tim O'Reilly'; //Escaped single quote
echo $name;
$path = 'C:\WINDOWS'; //Escaped backslash
echo $path;
$nope = 'n'; // Not an escape sequence
echo $nope;
Tim O'Reilly
C:WINDOWS
n
4.1.3 Use double quotes Surrounded Strings
Double-Quoted Strings
Strings enclosed in double quotes will be variable parsed and many escape sequences are allowed. Table 4-1 lists the escape sequences that PHP recognizes in strings enclosed in double quotes.
Table 4-1: Escape sequences in strings enclosed in double quotes
Escape sequence character meaning

Double quotes
n
Line feed
r
Carriage return
t
Tab character
\
Backslash Bar
$
Dollar sign
{
Left brace
}
Right brace
[
Left bracket
]
Right bracket
If you use a heredoc in a more complex expression, you need to write the expression on separate lines:
printf(<<< Template
%s is %d years old.
Template
, “Fred”, 35 );
Single and double quotes in the heredoc are skipped (treated as normal symbols):
$dialogue = <<< No_More
"It's not going to happen!" she fumed.
He raised an eyebrow. "Want to bet?"
No_More;
echo $dialogue;
"It's not going to happen!" she fumed.
He raised an eyebrow. "Want to bet?"
Whitespace in the heredoc is also preserved :
$ws = <<< Enough
boo
hoo
Enough;
// $ws = ” boon hoon”;
Because the newline character before the end terminator will be removed, the following two The assignment is the same:
$s = 'Foo';
// same as
$s = <<< End_of_pointless_heredoc
Foo
End_of_pointless_heredoc;
If you want to end the heredoc reference with a newline character String, you need to add it yourself:
$s = <<< End
Foo
End;
//Note that Foo is followed by a blank line and cannot be deleted

The above introduces the methods of variable definition, PHP variable definition and variable substitution, including the content of variable definition. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to define variables in Python? How to define variables in Python? Jun 04, 2023 am 10:02 AM

In Python, variables can be understood as containers for storing data. When we need to use or manipulate data, we can define variables to store the data so that we can easily call and process the data. The following will introduce how to define variables in Python. 1. Naming rules In Python, the naming rules for variables are very flexible and usually need to follow the following rules: variable names consist of letters, underscores and numbers, and the first part cannot be a number. Variable names can use uppercase and lowercase letters, but Python is case-sensitive. variable name

Assignment methods and differences when defining variables in Golang functions Assignment methods and differences when defining variables in Golang functions May 17, 2023 pm 07:01 PM

Golang is a fast, efficient, and modern programming language that automatically checks types at compile time and has features such as concurrency and memory safety, so it is favored by more and more developers. In Golang, we often need to use functions to encapsulate business logic, and the assignment method when defining variables in functions is a common problem. This article will explain this problem in detail and analyze the differences. Variable definition In Golang, variables can be defined in two ways: var and :=. Among them, var square

How to define variables and constants in PHP How to define variables and constants in PHP May 11, 2023 pm 04:03 PM

PHP is a widely used programming language with excellent scalability and practicality. In PHP, variables and constants are two very important concepts. They can be used to store and represent values ​​and store important information. In this article, we will introduce in detail how to define variables and constants in PHP to help beginners get started quickly. 1. Define variables A variable is a name or identifier used to store a value. In PHP, the definition of variables can be divided into three steps: variable declaration, variable assignment and variable use. Below we detail

How to solve Python's repeated function variable definition error? How to solve Python's repeated function variable definition error? Jun 25, 2023 am 11:59 AM

Duplicate definition errors of function variables in Python are a common problem. When a variable with the same name is repeatedly defined in a function, Python will throw a "localvariable'xxxx'redefined" error. This error is usually caused by duplication of variable names inside and outside the function. In Python, variable scope is divided into local scope and global scope. When a variable is defined in a function, the variable defaults to a local variable and can only be used in that function.

C++ compilation error: A defined variable must be at the top. How should I modify it? C++ compilation error: A defined variable must be at the top. How should I modify it? Aug 22, 2023 am 11:43 AM

In C++ programming, you sometimes encounter a common error, that is, the "a defined variable must be at the top" error. This is usually caused by a variable being defined in an incorrect location. In this article, we will discuss how to fix this error. In C++, variables usually need to be defined at the beginning of the function body or scope. If you define a variable at the bottom before calling it, a compilation error "A defined variable must be at the top" will appear. The solution to this error is to move the variable definition to a function or action

How to solve C++ compilation error: 'operating on 'variable' that is being defined'? How to solve C++ compilation error: 'operating on 'variable' that is being defined'? Aug 26, 2023 pm 01:01 PM

How to solve C++ compilation error: 'operatingon'variable'thatisbeingdefined'? In C++ programming, sometimes we encounter an error message: 'operatingon'variable'thatisbeingdefined'. This error message indicates that we are operating on a variable while defining it, which is not allowed. In this article we will discuss this

Golang variable definition specifications and techniques Golang variable definition specifications and techniques Jan 13, 2024 pm 03:43 PM

Overview of specifications and techniques for variable definition in Golang: In Golang, variables are the most basic data storage unit in the program. Proper use of variable definition conventions and techniques can improve code readability, maintainability, and performance. This article will introduce some specifications and techniques for variable definition in Golang, and provide specific code examples. Naming conventions for variables: In Golang, there are certain conventions for naming variables. Variable names should use camelCase, with the first letter lowercase. If it is a private variable, it should be named in camel case.

Common problems and solutions to variable definition in Golang language Common problems and solutions to variable definition in Golang language Jan 10, 2024 am 09:21 AM

Common problems and solutions for variable definition in Golang language When programming in Golang language, variable definition is a basic and common operation. However, since Golang has some special rules and regulations, we may encounter some problems during variable definition. This article will introduce common problems and give corresponding solutions and code examples. Problem 1: Variables are declared but not used. In Golang, if we declare a variable but do not use it in subsequent programs, the compiler

See all articles