-3

我从以下位置收到解析错误:

$uphalfh = if(isset($price30in)) { echo $price30in->textContent;}
if(isset($price30out)) { echo ", " . $price30out->textContent; }

如何改进此代码以避免 Parse 错误?我知道我不能在 $var 中使用 IF 语句

当我回显变量时,它的作用就像一个魅力,我得到了我想要的结果。但是我怎样才能将Echo 变量的结果(第 10 行)分配给 $uphalfh

include_once './wp-config.php';
include_once './wp-load.php';
include_once './wp-includes/wp-db.php';

// A name attribute on a <td>
$price30in = $xpath->query('//td[@class=""]')->item( 1);
$price30out = $xpath->query('//td[@class=""]')->item( 2);

// Echo Variable's
if(isset($price30in)) { echo $price30in->textContent;} if(isset($price30out)) { echo ", " . $price30out->textContent; }

// The wrong CODE i tried:
// $uphalfh = if(isset($price30in)) { echo $price30in->textContent;}
// if(isset($price30out)) { echo ", " . $price30out->textContent; }


// Create post object
  $my_post = array();
  $my_post['post_title'] = 'MyTitle';
  $my_post['post_content'] = 'MyDesciprion';
  $my_post['post_status'] = 'draft';
  $my_post['post_author'] = 1;

// Insert the post into the database
$post_id =  wp_insert_post( $my_post );
update_post_meta($post_id,'30_min',$uphalfh);
4

6 回答 6

2
$uphalfh = isset($price30in);

if(isset($price30in)) { 
    echo $price30in->textContent;
}
if(isset($price30out)) {
   echo ", " . $price30out->textContent; 
}
于 2013-10-14T12:03:04.487 回答
1

删除分配。

if(isset($price30in)) { echo $price30in->textContent;}
if(isset($price30out)) { echo ", " . $price30out->textContent; }
于 2013-10-14T12:02:31.107 回答
1
// $uphalfh = you have to complete the variable initialising here and then continue with the conditional statement or remove the variable initialising and continue with the conditional statement because $var = if(conditon){ }else{ }; is wrong syntax in php
if(isset($price30in)) { 
   echo $price30in->textContent;
}
if(isset($price30out)) {
   echo ", " . $price30out->textContent; 
}
于 2013-10-14T12:02:40.943 回答
0
 echo $uphalf = (isset($price30in)) ? $price30in->textContent : "";
于 2013-10-14T12:02:59.537 回答
0

if子句不返回值,这意味着您不能在将值分配给变量的上下文中使用它,因为没有值。

$uphalfh = if(isset($price30in)) {
    echo $price30in->textContent;
} if(isset($price30out)) {
    echo ", " . $price30out->textContent;
}

上面的代码不会像你预期的那样工作,下面的代码应该会按预期工作—— 的值$uphalf将设置为$price30in->textContentor $price30out->textContent

if(isset($price30in)) {
    $uphalfh = $price30in->textContent;
} if(isset($price30out)) {
    $uphalfh = ", " . $price30out->textContent;
}

只有当您还希望将此结果输出到浏览器(访问者直接可见)时,您才可以使用echo.

if(isset($price30in)) {
    $uphalfh = $price30in->textContent;
}
if(isset($price30out)) {
    $uphalfh = ", " . $price30out->textContent;
}
echo $uphalfh;
于 2013-10-14T12:03:34.050 回答
0

一个简单的编辑就可以了!这解决了我的问题:-)

我更换了:

$uphalfh = if(isset($price30in)) { echo $price30in->textContent;}
if(isset($price30out)) { echo ", " . $price30out->textContent; }

有了这个:

$uphalfh = $price30in->textContent. ', ' . $price30out->textContent;
于 2013-10-14T14:56:31.207 回答