Table of Contents
php获取一个变量的名字的方法,php获取变量方法
PHP中怎获得变量名
php怎从一个文件中获取另一个文件的变量
Home php教程 php手册 php获取一个变量的名字的方法,php获取变量方法

php获取一个变量的名字的方法,php获取变量方法

Jun 13, 2016 am 09:25 AM
variable name

php获取一个变量的名字的方法,php获取变量方法

PHP中,所有的变量都存储在"符号表"的HastTable结构中,符号的作用域是与活动符号表相关联的。因此,同一时间,只有一个活动符号表。

我们要获取到当前活动符号表可以通过 get_defined_vars 方法来获取。

get_defined_vars // 返回所有已定义的变量所组成的数组

根据变量的值查找变量名字,但要注意,有可能有相同值的变量存在。

因此先将当前变量的值保存到一个临时变量中,然后再对原变量赋唯一值,以便查找出变量的名字,找到名字后,将临时变量的值重新赋值到原变量。

例子1:

<&#63;php 

$a = '100'; 

echo '$a name is:'.get_variable_name($a).' value:'.$a; // $a name is: a value: 100 

/** 
* @param String $var 要查找的变量 
* @param Array $scope 要搜寻的范围 
* @param String 变量名称 
*/ 
function get_variable_name(&$var, $scope=null){ 

$scope = $scope==null&#63; $GLOBALS : $scope; // 如果没有范围则在globals中找寻 

// 因有可能有相同值的变量,因此先将当前变量的值保存到一个临时变量中,然后再对原变量赋唯一值,以便查找出变量的名称,找到名字后,将临时变量的值重新赋值到原变量 
$tmp = $var; 

$var = 'tmp_value_'.mt_rand(); 
$name = array_search($var, $scope, true); // 根据值查找变量名称 

$var = $tmp; 
return $name; 
}
&#63;>
Copy after login

例子2:获取function里面定义的变量名字

<&#63;php 
function test(){ 
$a = '100'; 
echo '$a name is:'.get_variable_name($a); 
} 

test(); // $a name is: undefined 
//因为在function中定义的变量globals会找不到 

function test2(){ 
$a = '100'; 
echo '$a name is:'.get_variable_name($a, get_defined_vars()); 
} 

test2(); // $a name is: a 
// 将scope设定为 get_defined_vars() 可以找到 

&#63;>
Copy after login

PHP中怎获得变量名

function vname(&$var, $scope=false, $prefix='unique', $suffix='value')
{
if($scope) $vals = $scope;
else $vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key => $val) {
if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
}

echo "\$变量名=".vname($变量名);
?>

输出结果: $变量名=变量名
 

php怎从一个文件中获取另一个文件的变量

1.楼上的方法 include("A.php") 或 require("A.php")
2.通过url后附带该变量,在B.php中用 $_GET['变量名'] 获得
3.玩狠点: 把这个变量设为session或cookie
 

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

A guide to using Windows 11 and 10 environment variables for profiling A guide to using Windows 11 and 10 environment variables for profiling Nov 01, 2023 pm 08:13 PM

Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode for variables in PHP7: how to reduce potential bugs? Strict mode for variables in PHP7: how to reduce potential bugs? Oct 19, 2023 am 10:01 AM

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

Internal error: Unable to create temporary directory [Resolved] Internal error: Unable to create temporary directory [Resolved] Apr 17, 2023 pm 03:04 PM

Windows system allows users to install various types of applications on your system using executable/setup files. Recently, many Windows users have started complaining that they are receiving an error named INTERNALERROR:cannotCreateTemporaryDirectory on their systems while trying to install any application using an executable file. The problem is not limited to this but also prevents the users from launching any existing applications, which are also installed on the Windows system. Some possible reasons are listed below. Run the executable to install without granting administrator privileges. An invalid or different path was provided for the TMP variable. damaged system

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

How to change the name on TikTok How to change the nickname on TikTok How to change the name on TikTok How to change the nickname on TikTok Feb 22, 2024 pm 03:13 PM

Enter my page on the home page, select Edit Information, then select a name, delete it and then enter the name you want to set to save it. Tutorial Applicable Model: iPhone 13 System: IOS 15.3 Version: Douyin 20.6.0 Analysis 1 First open Douyin and click My Options in the lower right corner of the homepage. 2Then click the Edit Information button on the right side of the avatar on my page. 3 Then click the name option on the pop-up page. 4 Click the delete icon at the back, enter the name you want to set, and click Save. Supplement: What is the role of Douyin nicknames? 1 The role of Douyin account nickname settings is that on the one hand it is easy to distinguish, on the other hand it is helpful for users to enter searches, and finally it is conducive to the expansion of fan users' creations. Summary/Notes on Douyin nicknames for one day

PHP function introduction—is_string(): Check whether the variable is a string PHP function introduction—is_string(): Check whether the variable is a string Jul 24, 2023 pm 09:33 PM

PHP function introduction—strpos(): Check whether a variable is a string In PHP, is_string() is a very useful function, which is used to check whether a variable is a string. When we need to determine whether a variable is a string, the is_string() function can help us achieve this goal easily. Below we will learn about how to use the is_string() function and provide some related code examples. The syntax of the is_string() function is very simple. it only needs to

See all articles