Im getting Parse error from:

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

How can I improve this code to avoid Parse error? I understand that I can't use IF statement in $var

When I Echo Variable's its works like a charm and I get the results that I want. But how can I assign the result of Echo Variable's (on Line:10) to $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);
share
10  
$var=if() is just wrong syntax. – geomagas Oct 14 '13 at 12:02
    
If you have $price29in, $price28in, etc... you should read up on variable variables or arrays. – Mike B Oct 14 '13 at 12:06
    
you got learn php very neatly. Find a documentation about php here – nurakantech Oct 14 '13 at 12:35

remove the assignment.

if(isset($price30in)) { echo $price30in->textContent;}
if(isset($price30out)) { echo ", " . $price30out->textContent; }
share
    
This code works well: if(isset($price30in)) { echo "<br />" . $price30in->textContent;} if(isset($price30out)) { echo ", " . $price30out->textContent; } How can I add the result of this code to $var ? – Cyborg Oct 14 '13 at 12:12
    
@Cyborg you're going about it the wrong way. Check out my answer. – Mihai Stancu Oct 14 '13 at 12:24
    
@Cyborg $varPrice30in = (isset($price30in))?$price30in->textContent:''; $varPrice30out = (isset($price30out))?", " . $price30out->textContent:''; – vbrmnd Oct 14 '13 at 12:27
// $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; 
}
share
    
Can you explain reason for downvotting @downvotter. – 웃웃웃웃웃 Oct 14 '13 at 12:05
$uphalfh = isset($price30in);

if(isset($price30in)) { 
    echo $price30in->textContent;
}
if(isset($price30out)) {
   echo ", " . $price30out->textContent; 
}
share
 echo $uphalf = (isset($price30in)) ? $price30in->textContent : "";
share

The if clause does not return a value, which means you can't use it in the context of assigning a value to variable because there is no value to be had.

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

The above code will not work as you expected it, the bellow code should work as expected -- the value of $uphalf will be set to either $price30in->textContent or $price30out->textContent.

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

Only if you also want this result to be outputted to the browser (directly visible to a visitor) you could use echo.

if(isset($price30in)) {
    $uphalfh = $price30in->textContent;
}
if(isset($price30out)) {
    $uphalfh = ", " . $price30out->textContent;
}
echo $uphalfh;
share
    
Please take a look at my updated question. Any suggestions? Thanks – Cyborg Oct 14 '13 at 13:30
    
My example should work correctly, $uphalfh should be assigned a value based on which of the if conditions is satisfied. – Mihai Stancu Oct 14 '13 at 14:07
    
@Cyborg did you by any chance intend to have the value of both nodes? Not just one of them? – Mihai Stancu Oct 14 '13 at 14:09
up vote 0 down vote accepted

A simple edit did the trick! That solved my problem :-)

I replaced:

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

With this:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.