What is the use of $$ symbol in PHP? how to use? (code example)

青灯夜游
Release: 2023-04-05 12:12:01
Original
11368 people have browsed it

We often use the $ symbol in PHP, so do you know the $$ symbol? The following article will take you to understand the $$ symbol, introduce what the $$ symbol is used in PHP, and how to use it. I hope it will be helpful to everyone.

What is the use of $$ symbol in PHP? how to use? (code example)

The $$ symbol in PHP

In PHP, $var (single dollar) represents An ordinary variable named var, which stores any value such as string, integer, floating point, etc. And $$var (double dollar) is a reference variable used to store the value of $var. [Video tutorial recommendation: PHP tutorial]

Take an example to understand:

Use $var to store a String type value" PHP", and then use the reference variable $$var to store a String type value "PHP Chinese Network".

At this time, the value "Chinese Network" can be accessed in two ways:

1. Directly use reference variables. Example: echo $$var;

2. Access the value "PHP Chinese Network" by using the value "PHP" stored in the variable $var as the variable name. For example: echo $PHP; it will be output as "PHP Chinese Network" (without quotation marks).

What is the use of $$ symbol in PHP? how to use? (code example)

Code example:

The following is an example of how to use $ and $ in PHP $:

Example 1:

<?php 
header("content-type:text/html;charset=utf-8");   
// 声明变量并初始化它
$var = "PHP";      
  
// 引用变量
$$var = "PHP中文网"; 
  
// 输出$var的值
echo $var . "<br>";  
  
// 输出$$var的值
echo $$var . "<br>";  
  
// 输出$PHP的值
echo "$PHP"; 
?>
Copy after login

Output:

What is the use of $$ symbol in PHP? how to use? (code example)

Example 2:

<?php 
header("content-type:text/html;charset=utf-8");   
$name="Cat";  
${$name}="Dog";  
${${$name}}="Monkey";  
echo $name. "<br>";  
echo ${$name}. "<br>";  
echo $Cat. "<br>";  
echo ${${$name}}. "<br>";  
echo $Dog. "<br>";  
?>
Copy after login

Output:

What is the use of $$ symbol in PHP? how to use? (code example)

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What is the use of $$ symbol in PHP? how to use? (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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