PHP-解析错误:语法错误,意外的T\u IF
我正在从以下位置获取分析错误:PHP-解析错误:语法错误,意外的T\u IF,php,Php,我正在从以下位置获取分析错误: $uphalfh = if(isset($price30in)) { echo $price30in->textContent;} if(isset($price30out)) { echo ", " . $price30out->textContent; } 如何改进此代码以避免解析错误我明白我不能在$var中使用IF语句 当我Echo变量的像一个符咒一样工作时,我得到了我想要的结果。但是如何将Echo变量的(第10行)的结果分配给$uphalfh
$uphalfh = if(isset($price30in)) { echo $price30in->textContent;}
if(isset($price30out)) { echo ", " . $price30out->textContent; }
如何改进此代码以避免解析错误我明白我不能在$var中使用IF语句
当我Echo变量的像一个符咒一样工作时,我得到了我想要的结果。但是如何将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);
include_once./wp config.php';
包括_once'/wp load.php';
include_once'/wp includes/wp db.php';
//表上的名称属性
$price30in=$xpath->query('//td[@class=”“])->项(1);
$price30out=$xpath->query('//td[@class=”“])->项(2);
//回声变量
if(isset($price30in)){echo$price30in->textContent;}if(isset($price30out)){echo',“$price30out->textContent;}
//我试过的错误代码:
//$uphalfh=if(isset($price30in)){echo$price30in->textContent;}
//if(isset($price30out)){echo',“$price30out->textContent;}
//创建post对象
$my_post=array();
$my_post['post_title']='MyTitle';
$my_post['post_content']='MyDesciprion';
$my_post['post_status]='draft';
$my_post['post_author']=1;
//将帖子插入数据库
$post\u id=wp\u insert\u post($my\u post);
更新发布元($post\u id,'30\u min',$uphalfh);
删除分配
if(isset($price30in)) { echo $price30in->textContent;}
if(isset($price30out)) { echo ", " . $price30out->textContent; }
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;
简单的编辑就成功了!这解决了我的问题:-) 我替换了:
$uphalfh = if(isset($price30in)) { echo $price30in->textContent;}
if(isset($price30out)) { echo ", " . $price30out->textContent; }
为此:
$uphalfh = $price30in->textContent. ', ' . $price30out->textContent;
$var=if()
只是语法错误。如果您有$price29in
,$price28in
,等等。。。你应该仔细阅读或数组。你已经非常熟练地学习了php。查找有关php的文档可以解释downvotting@downvotter的原因吗?此代码运行良好:if(isset($price30in)){.echo“
“$price30in->textContent;}if(isset($price30out)){.echo“,”$price30out->textContent;}
如何将此代码的结果添加到$var?@Cyborg您的操作方式不对。查看我的答案。@Cyborg$varPrice30in=(isset($price30in))?$price30in->textContent:“”$varPrice30out=(isset($price30out))?“,”$price30out->textContent:“”代码>请看我更新的问题。有什么建议吗?谢谢我的示例应该正常工作,$uphalfh
应该根据满足条件的中的哪一个来分配一个值。@Cyborg您是否有可能想要两个节点的值?不只是其中一个?
if(isset($price30in)) {
$uphalfh = $price30in->textContent;
}
if(isset($price30out)) {
$uphalfh = ", " . $price30out->textContent;
}
echo $uphalfh;
$uphalfh = if(isset($price30in)) { echo $price30in->textContent;}
if(isset($price30out)) { echo ", " . $price30out->textContent; }
$uphalfh = $price30in->textContent. ', ' . $price30out->textContent;