在 PHP 语言中,将变量名再赋予变量的语法为可变变量。

常规使用实例

<?php 
$like = "apple"; //定义 like 的值为 apple
$revariable = "relike_".$like; //定义重新变量
$relike_apple = "banana"; //定义重新的值
echo $like."<br>"; //apple
echo $revariable."<br>"; //relike_apple
echo $relike_apple."<br>"; //banana
echo $$revariable."<br>"; //banana
<?php
$like = "apple";
$apple = "banana";
echo $$like; //banana
<?php
$like = "apple"; $apple = "banana"; $banana = "orange";
echo $like; //apple 
echo $$like; //banana 
echo $$$like; //orange 
... 无限延续

表单对象实例

<form role="form" action="" method="post">
<select name="like" required>
	<option value=""> 我的选择</option>
	<option value="<?php $fruit = "Apple"; $Apple = "Banana"; $Banana = "Orange"; echo "$fruit" ?>"><?php echo $fruit; ?></option>
	<option value="<?php $car = "Benz"; $Benz = "BMW"; $BMW = "Audi"; echo "$car" ?>"><?php echo $car; ?></option>	
	<option value="<?php $city = "HongKong"; $HongKong = "Paris"; $Paris = "Tokyo"; echo "$city" ?>"><?php echo $city; ?></option>
</select>
  <input type="submit" value='Get My Like'>
</form>

<?php $name = $_POST['like']; echo $name; ?>;
<?php $name = $_POST['like']; echo $$name; ?>;
<?php $name = $_POST['like']; echo $$$name; ?>;

如果提交到新页面,需要在新页面补充相同的变量数据。

<?php $fruit = "Apple"; $Apple = "Banana"; $Banana = "Orange"; ?>
<?php $car = "Benz"; $Benz = "BMW"; $BMW = "Audi"; ?>
<?php $city = "HongKong"; $HongKong = "Paris"; $Paris = "Tokyo"; ?>

同时还支持以下句法规范:

<?php $fruit = "Apple"; $$fruit = "Banana"; $$$fruit = "Orange"; ?>
<?php $car = "Benz"; $$car = "BMW"; $$$car = "Audi"; ?>
<?php $city = "HongKong"; $$city = "Paris"; $$$city = "Tokyo"; ?>

友情提示:本站所有文章,如无特殊说明或标注,均为何星星原创发布。与此同时,趋于近年来本站的文章内容频繁被他站盗用与机器采集,现已全局禁用网站文字内容操作,了解详情或转载文章请 点此 继续!
分类: 前端开发

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用*标注