PHP - 解析错误:语法错误,意外T_IF

时间:2013-10-14 12:00:46

标签: php

我得到Parse错误:

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

如何改进此代码以避免Parse错误? 我知道我不能在$ var

中使用IF语句

当我 Echo Variable的时,它就像魅力一样,我得到了我想要的结果。但是如何将回声变量的结果(在线: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);

6 个答案:

答案 0 :(得分:2)

$uphalfh = isset($price30in);

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

答案 1 :(得分:1)

删除作业。

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

答案 2 :(得分: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; 
}

答案 3 :(得分:0)

 echo $uphalf = (isset($price30in)) ? $price30in->textContent : "";

答案 4 :(得分:0)

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

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

上述代码无法按预期运行,波纹管代码应按预期工作 - $uphalf的值将设置为$price30in->textContent$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;

答案 5 :(得分:0)

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

我换了:

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

有了这个:

$uphalfh = $price30in->textContent. ', ' . $price30out->textContent;