我想在不使用额外变量的情况下做这样的事情:
class className {
public static function func(){
return array('true','val2');
}
}
if(className::func()[0]) {
echo 'do?ru';
} else {
echo 'Yanl??';
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
用于解除引用功能结果的PHP语法
function returnArray(){
$array = array(
0 => "kittens",
1 => "puppies"
);
return $array;
}
echo returnArray()[0];
Run Code Online (Sandbox Code Playgroud)
如果不将整个数组赋值给变量,我该怎么做?
可能重复:
用于解除引用功能结果的PHP语法
这就是我的意思.目前我必须:
$str = "foo-bar";
$foo = explode("-",$str);
$foo = $foo[0];
Run Code Online (Sandbox Code Playgroud)
我想做的是:
$str = "foo-bar";
$foo = explode("-",$str)[0];
Run Code Online (Sandbox Code Playgroud)
但这是一个语法错误!还有另一种方法可以在一条线上完成吗?
编辑:只是想说清楚我也希望能够做到
$str = "foo-bar-baz";
$bar = explode("-",$str)[1];
Run Code Online (Sandbox Code Playgroud)
以及.
为什么这有效:
$cacheMatchesNotPlayed = $cache->load('externalData');
$cacheMatchesNotPlayed = $cacheMatchesNotPlayed['matchesNotPlayed'];
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
$cacheMatchesNotPlayed = $cache->load('externalData')['matchesNotPlayed'];
Run Code Online (Sandbox Code Playgroud)
有什么理由吗?第二位更容易编写.
下面是我的PHP代码,显示两个日期之间的所有日期..但不能正常工作...
fromdate = 20-04-2015
todate = 25-05-2015
Run Code Online (Sandbox Code Playgroud)
所以我的代码只显示 20,21,22,23,24,25
但我需要这两个月之间的所有日子.
预期产出
20-04 21-04 22-04 23-04 24-04 25-04 26-04 27-04 28-04 ......upto 25-05
Run Code Online (Sandbox Code Playgroud)
下面是我的代码..
<?php
$startdate = $_POST['fromdate'];
$enddate = $_POST['todate'];
$start = date('d', strtotime($startdate));
$end=date('d', strtotime($enddate));
?>
<?php for ($x = $start; $x <= $end; $x++) { ?>
<th width="58%"><?php echo $x; ?></th>
<?php } ?>
Run Code Online (Sandbox Code Playgroud) 当您尝试使用较新版本编写的PHP脚本来处理旧版本时,它们是否是任何良好的资源; 特别是5.4到5.3?
我甚至检查了有关这些变化的文章,我似乎无法弄清楚我做错了什么.
这是我得到的错误,此时此刻:
Parse error: syntax error, unexpected '[' in Schedule.php on line 113
Run Code Online (Sandbox Code Playgroud)
它所指的代码:
private static $GAMES_QUERY = array('season' => null, 'gameType' => null);
.....
public function getSeason(){
$test = array_keys(self::$GAMES_QUERY)[0]; //<<<<<<<<<< line:113
return($this->query[$test]);
}
Run Code Online (Sandbox Code Playgroud)
我所见过的所有内容似乎都说5.3具有self ::,array_keys,并且能够访问这样的数组.
为什么这段代码失败了
function f(){
return array('k'=>'abc');
}
print_r(f()['k']);
Run Code Online (Sandbox Code Playgroud)
这段代码有用吗?
function f(){
return array('k'=>'abc');
}
$a = f();
print_r($a['k']);
Run Code Online (Sandbox Code Playgroud)
唯一的区别是在尝试访问数组元素之前将f结果 赋值$a.如何在f没有额外作业的情况下直接参考结果?
我想要做的explode(" ",$q[2])[1]哪里$q[2]是一个字符串阅读"问题1",但我不断收到错误,说一个逗号或者分号预期,而不是以后爆炸"面向括号权[1]".当字符串不是数组位置时,我可以使用这种语法,所以有一种简单的方法来做这个,而不是制作一些临时变量并爆炸吗?
可能重复:
用于解除引用功能结果的PHP语法
我在php 5.4中编写了一些代码,我的服务器只能运行5.3,因此我需要解决一些语法错误.
当我到达网站时,我收到此错误
Parse error: syntax error, unexpected '[' in /home/content/51/6663351/html/application/controllers/admin.php on line 247
Run Code Online (Sandbox Code Playgroud)
第247行是
if(count($results->result()) > 0)
{
this here>>> $data['data'] = $results->result()[0];
$data['cats'] = $this->db->get('category')->result();
$data['curCat'] = $this->db->get('products_categories', array('product_id' => $id))->result()[0];
Run Code Online (Sandbox Code Playgroud)
所以我尝试将代码更改为:
$data = array();
if(count($results->result()) > 0)
{
$data['data'] = $results->result()[0];
$data['cats'] = $this->db->get('category')->result();
$data['curCat'] = $this->db->get('products_categories', array('product_id' => $id))->result()[0];
Run Code Online (Sandbox Code Playgroud)
如何添加$ data = array(); 没有解决任何问题.有谁知道出了什么问题?