简体   繁体   中英

Cant find my error

I am trying to find my error, but this code does not work:

$marginprice = $retProd['price']['FormattedPrice'];
$marginten = '10';
if ($marginprice < $marginten) {$marginprice + '7';}
else {$marginprice + '12';}                 
update_post_meta($lastId, '_price', $marginprice);

As per the comments, + is not a valid string operator.

I assume you want to add numbers, in which case you don't need to quote the number:

$marginprice = 10;
$marginprice += 7;
echo $marginprice; // will output 17

If you want to concatenate strings (add one after the other):

$marginprice = 10; // it starts as a number
$marginprice .= '7'; // marginprice is now a string
echo $marginprice; // will output 107

So your code becomes:

$marginprice = $retProd['price']['FormattedPrice'];
$marginten = 10;
if ($marginprice < $marginten) {$marginprice += 7;}
else {$marginprice += 12;}                 
update_post_meta($lastId, '_price', $marginprice);

您需要为变量分配一个值。

  $marginprice = $marginprice + 7;

Not sure what you mean by "doesn't work", but you should be using integers for..well..integers, and you are not incrementing marginprice properly:

$marginprice = $retProd['price']['FormattedPrice'];
$marginten = 10;
if ($marginprice < $marginten) {$marginprice += 7;}
else {$marginprice += 12;}                 
update_post_meta($lastId, '_price', $marginprice);

If you want to use numbers, you don't need to quote them or they will be interpreted as strings.

$marginprice = $retProd['price']['FormattedPrice'];
$marginten = 10;

if ($marginprice < $marginten)
    $marginprice + 7;
else
    $marginprice + 12;

update_post_meta($lastId, '_price', $marginprice);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM