Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自动计数包含语法错误的金额_Php - Fatal编程技术网

Php 自动计数包含语法错误的金额

Php 自动计数包含语法错误的金额,php,Php,好的,我有一个脚本,如下所示: $i = 0; foreach($_SESSION['cart'] as $id => $quantity) { $photo + ++$i =$pwinty->addPhoto($order, $_SESSION['size'][$id], $row['source'], $quantity, "ShrinkToFit"); } 基本上,我有一个变量I,它从0开始,如果可以的话,每次在“列表”中添加一个新的$photo,它都会增加。因

好的,我有一个脚本,如下所示:

$i = 0;

foreach($_SESSION['cart'] as $id => $quantity) {

     $photo + ++$i =$pwinty->addPhoto($order, $_SESSION['size'][$id], $row['source'], $quantity, "ShrinkToFit");
}
基本上,我有一个变量
I
,它从
0
开始,如果可以的话,每次在“列表”中添加一个新的
$photo
,它都会增加。因此,基本上上述需要转化为:

$photo1 = ...
$photo2 = ...
$photo3 = ...
现在,
$photo
行已设置。脚本运行时,我收到以下错误:

Parse error: syntax error, unexpected '=' ...

我猜我需要把这行连接在一起,但我不确定。提前感谢您的帮助。

应该是这样的:

$i = 0;

foreach($_SESSION['cart'] as $id => $quantity) {

     $photo = $pwinty->addPhoto($order, $_SESSION['size'][$id], $row['source'], $quantity, "ShrinkToFit");
$i++;
}
试试这个

$i=1;
$ph='photo';
foreach($_SESSION['cart'] as $id => $quantity) {
   $photo=$ph.$i;// will give 'photo1' at first
   // $photo1=...
   $$photo =$pwinty->addPhoto($order, $_SESSION['size'][$id], $row['source'], $quantity, "ShrinkToFit");
   echo $$photo; // echo $photo1
   $i++;
}
您可以
创建每个变量的数组
,这些变量很容易访问,例如

$photo=array();
$i=0;
foreach($_SESSION['cart'] as $id => $quantity) {
     $photo['photo'][$i++]=$pwinty->addPhoto($order, $_SESSION['size'][$id], $row['source'], $quantity, "ShrinkToFit");
}
print_r($photo);
这:

在作业的左侧是非法的,因为它没有意义

删除+$i并移到下一行

更新:

尝试执行以下操作:

<?php $photo + ++$i = 3; ?>

您将得到相同的语法错误

分析错误:语法错误,第2行的C:\PHP\test.PHP中出现意外的“=”


你真正想做的事情是如何完成的,但要注意这是非常糟糕的做法:

$i = 0;
foreach($_SESSION['cart'] as $id => $quantity) {
    ${'photo'.++$i} = $pwinty->addPhoto($order, $_SESSION['size'][$id], $row['source'], $quantity, "ShrinkToFit");
}
// how to output a photo:
if(isset($photo0)){
    echo $photo0;
}
这是一种不好的做法,因为它会产生很多变量,而且效率要低得多。您应该使用数组

应该是这样的:

$photo = array();
$i = 0;
foreach($_SESSION['cart'] as $id => $quantity) {
    $photo[++$i] = $pwinty->addPhoto($order, $_SESSION['size'][$id], $row['source'], $quantity, "ShrinkToFit");
}
// for checking the content of the $photo array:
echo '<pre>';
print_r($photo);
echo '</pre>';
// example of how to access data from the array, where 0 is the first thing that got added, if anything was added:
if(isset($photo[0])){
    echo $photo[0];
}
// traverse the entire array:
foreach($photo AS $id => $p){
    echo 'id: '.$id.', photo: '.$p;
}
$photo=array();
$i=0;
foreach($\会话['cart']作为$id=>$quantity){
$photo[++$i]=$pwinty->addPhoto($order,$\u SESSION['size'][$id],$row['source'],$quantity,“ShrinkToFit”);
}
//要检查$photo数组的内容,请执行以下操作:
回声';
印刷(照片);
回声';
//如何从阵列访问数据的示例,其中,如果添加了任何内容,则0是第一个添加的内容:
如果(isset($photo[0])){
echo$photo[0];
}
//遍历整个阵列:
foreach($id=>p){
回显'id:'.$id',照片:'.$p;
}

只是想知道为什么要创建单独的变量?你可以改为使用数组,这将是更好的方法,因为
$i
在这里没有意义,如果
$\u SESSION['cart']
空的,那么
$photo
将给出
未定义的通知
,如果进一步使用的话。嘿,伙计们,鸭子是什么?OP问他为什么有语法错误。这会导致语法错误-请自己尝试,不要否决或解释我的答案的错误。
$photo = array();
$i = 0;
foreach($_SESSION['cart'] as $id => $quantity) {
    $photo[++$i] = $pwinty->addPhoto($order, $_SESSION['size'][$id], $row['source'], $quantity, "ShrinkToFit");
}
// for checking the content of the $photo array:
echo '<pre>';
print_r($photo);
echo '</pre>';
// example of how to access data from the array, where 0 is the first thing that got added, if anything was added:
if(isset($photo[0])){
    echo $photo[0];
}
// traverse the entire array:
foreach($photo AS $id => $p){
    echo 'id: '.$id.', photo: '.$p;
}