PHP - 在单个变量中存储多个值

时间:2017-09-18 08:53:32

标签: php variables

例如,我有像这样的PHP代码

if ($rating_count == $rating_notnull) {
        $returnValue =  1;
    }
    if($project_count == $project_notnull) {
        $returnValue = 2;
    }

现在,我想将两个变量值存储到一个变量中?

2 个答案:

答案 0 :(得分:3)

数组可用于保存多个值。他们可以有一把钥匙。

<?php

$x = array();

$x[] = 'Some Value';
$x[] = 'Another value';

var_dump($x);

$x = array();

$x['name'] = 'Anil';
$x['other name'] = 'Del Boy';

var_dump($x);

$x = array(
    'Name' => 'Batman',
    'Status' => 'Busy',
    'etc' => 'etc',
);

var_dump($x);

在这里玩游戏:https://3v4l.org/LCjtY

答案 1 :(得分:0)

正如其他评论中所解释的那样,最有效的方法是使用数组来存储这些值。

但是,如果您希望将其存储在单个变量中,则可以使用explode

这会在爆炸时将其输出到数组中但是允许您将其存储到一个变量

$str = "Hello, this, is, a, variable";
explode(",", $str);