search
HomeBackend DevelopmentPHP TutorialVariable definition PHP variable definition and variable substitution methods

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(%s is %d years old.
Template
, “Fred”, 35 );
Single and double quotes in the heredoc are skipped (treated as normal symbols):
$dialogue = "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 = 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 = 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 = 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
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
如何在Python中定义变量?如何在Python中定义变量?Jun 04, 2023 am 10:02 AM

在Python中,变量可以理解为存储数据的容器。当我们需要使用或操作数据时,可以通过定义变量来存储数据,从而方便地调用和处理这些数据。下面将介绍Python中定义变量的方法。一、命名规则在Python中,变量的命名规则非常灵活,通常需要遵循以下规则:变量名由字母、下划线和数字组成,首位不能为数字。变量名可以使用大小写字母,但Python是区分大小写的。变量名

Golang函数的变量定义时的赋值方法与区别Golang函数的变量定义时的赋值方法与区别May 17, 2023 pm 07:01 PM

Golang是一种快速、高效、现代化的编程语言,它在编译时会自动检查类型,并且具有并发性和内存安全性等特点,因此被越来越多的开发者所青睐。在Golang中,我们经常需要使用函数来封装业务逻辑,而函数中的变量定义时的赋值方法是一个常见的问题,本文将详细讲解这个问题并分析其中的区别。变量定义在Golang中,可以使用var和:=两种方式来定义变量。其中,var方

如何在PHP中定义变量和常量如何在PHP中定义变量和常量May 11, 2023 pm 04:03 PM

PHP是一种被广泛使用的编程语言,具有卓越的可扩展性和实用性。在PHP中,变量和常量是两种十分重要的概念,它们可以用来存储和表示值以及存储重要的信息。在这篇文章中,我们将会详细介绍如何在PHP中定义变量和常量,以帮助初学者快速上手。一、定义变量变量是用于存储值的名字或标识符。在PHP中,变量的定义可以分为三个步骤:变量的声明、变量的赋值和使用变量。下面我们详

如何解决Python的函数变量重复定义错误?如何解决Python的函数变量重复定义错误?Jun 25, 2023 am 11:59 AM

Python的函数变量重复定义错误是一个常见问题,当一个函数中重复定义了相同名称的变量时,Python会抛出“localvariable'xxxx'redefined”错误。这个错误通常是由于函数内部的变量名和外部的变量名重复导致的。在Python中,变量作用域分为局部作用域和全局作用域,当在一个函数中定义变量时,该变量默认为局部变量,并且只能在该函数

C++编译错误:一个定义的变量必须在最上面,应该怎么修改?C++编译错误:一个定义的变量必须在最上面,应该怎么修改?Aug 22, 2023 am 11:43 AM

在C++编程中,有时会遇到一个常见的错误,即“一个定义的变量必须在最上面”的错误。这通常是由于变量定义的位置不正确导致的。在本文中,我们将讨论如何修复这个错误。在C++中,变量的定义通常需要在函数体或作用域的开始处进行。如果你定义的变量放在下面,而在调用之前,则会出现“一个定义的变量必须在最上面”的编译错误。出现这个错误的解决方案就是将变量定义移到函数或作用

Golang变量定义的规范和技巧Golang变量定义的规范和技巧Jan 13, 2024 pm 03:43 PM

Golang中变量定义的规范与技巧概述:在Golang中,变量是程序中最基本的数据存储单元。正确使用变量定义的规范和技巧可以提高代码的可读性、可维护性和性能。本文将介绍一些Golang中变量定义的规范和技巧,并提供具体的代码示例。变量的命名规范:在Golang中,变量的命名是有一定规范的。变量名应该使用驼峰命名法,首字母小写。如果是私有变量,应该使用驼峰命名

解决C++编译错误:'operating on 'variable' that is being defined',如何解决?解决C++编译错误:'operating on 'variable' that is being defined',如何解决?Aug 26, 2023 pm 01:01 PM

解决C++编译错误:'operatingon'variable'thatisbeingdefined',如何解决?在C++编程中,有时候我们会遭遇到一个错误信息:'operatingon'variable'thatisbeingdefined'。这个错误信息指明我们在定义变量的同时对其进行了操作,这是不被允许的。在本文中,我们将讨论这

Golang语言中变量定义的常见问题及解决方法Golang语言中变量定义的常见问题及解决方法Jan 10, 2024 am 09:21 AM

Golang语言中变量定义的常见问题及解决方法在使用Golang语言进行编程时,变量的定义是一个基础且常见的操作。然而,由于Golang有一些特殊的规则和规定,我们可能在变量定义过程中遇到一些问题。本文将针对常见问题进行介绍,并给出相应的解决方法和代码示例。问题一:变量声明但未使用在Golang中,如果我们声明了一个变量,但在后续的程序中并未使用该变量,编译

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools