# PHP/XML Question



## ShiBDiB (Sep 22, 2015)

```
$url3 = 'https://api.eveonline.com/account/Characters.xml.aspx';
                    $url3 .= '?keyid=' . urlencode($_POST['keyid']) . '&vcode=' . urlencode($_POST['vcode']);

                    $xml = makeApiRequest($url3);
                    $charidcheck = $xml->xpath('//row[@characterID="' . $id .'"]');
                   
                    if ($charidcheck = NULL) { $error_msg .= '<p class="error">Character Not On API Key!</p>';
                       $stmt->close(); }
```

Basically I'm trying to check if the variable $id exists in an xml file. And if it doesn't it should return the error.

Somewhere this isn't working because regardless of if I use an api that contains the $id or not the error doesn't happen.


----------



## W1zzard (Sep 22, 2015)

Show us the fetched XML

if ($charidcheck *=* NULL) missing second = for ==


----------



## FordGT90Concept (Sep 22, 2015)

DOMDocument::getElementsByTagName


----------



## ShiBDiB (Sep 22, 2015)

W1zzard said:


> Show us the fetched XML
> 
> if ($charidcheck *=* NULL) missing second = for ==



going from = to == fixed it. Damn little things -_-


----------



## FordGT90Concept (Sep 22, 2015)

I wish PHP had an editor like Visual Studio.  Visual Studio would mark the = saying an assignment is invalid in this context or something that effect.


----------



## W1zzard (Sep 22, 2015)

FordGT90Concept said:


> Visual Studio would mark the = saying an assignment is invalid in this context or something that effect.


Nope, it's perfectly valid code, happens to me like once a month


----------



## Aquinus (Sep 22, 2015)

There are legitimate reasons for doing something like that though, if you take something like Moodle (a learning management system,) you might have something like:

```
if ($rec = $DB->get_record('user', array('id' => 12345))) {
    /// Do something if the record was retrieved.
    print_r($rec);
}
```

That way you can do something if you get back a truthy value and at the same time, set it to a variable. You just have to remember that = is assignment whereas == and ===  different forms of equality which are predicates (returns a boolean).

Edit: Remember, when we're talking about PHP, it's best to use the PHP bbcode. Syntax highlighting makes it a lot easier to read code (in my opinion.)


----------



## FordGT90Concept (Sep 22, 2015)

W1zzard said:


> Nope, it's perfectly valid code, happens to me like once a month


It is valid in C# too but Visual Studio still catches it because assignments don't evaluate to a Boolean expression.  In this case, "$charidcheck = NULL" is always true because it will never fail unless $charidcheck is a type that is not nullable (not sure if that is even possible in PHP).

In Aquinus's example, it is using the result of get_record to evaluate the expression.


----------



## Aquinus (Sep 22, 2015)

FordGT90Concept said:


> It is valid in C# too but Visual Studio still catches it because assignments don't evaluate to a Boolean expression.  In this case, "$charidcheck = NULL" is always true because it will never fail unless $charidcheck is a type that is not nullable (not sure if that is even possible in PHP).
> 
> In Aquinus's example, it is using the result of get_record to evaluate the expression.


You can also do:

```
if(!$rec = $DB->get_record('user', array('id' => $userid))) {
    throw new Exception("OMG! No user with {$userid}!");
}
/// Process everything normally.
print_r($rec);
```
That's also completely valid. You're checking the value of the assigned value in PHP so you can do some thing that can add up to be rather confusing. PHP's order of operations is something is isn't exactly intuitive. For example:
If you think these two statements are the same, you're wrong.

```
$rval = true and false;
```


```
$rval = true && false;
/// or
$rval = (true and false);
```
It's really quite insane because according to PHP's order of operations, = has higher precedence than "and" but "&&" has higher precedence than =. Then of course, you can use parens to force an order.

Don't you just _love*_ php?
*: Sarcasm.


----------



## FordGT90Concept (Sep 23, 2015)

Nope, not really.  I prefer my languages have explicit typing.

I didn't know that PHP does assignments first.  That's just wrong.


----------



## kn00tcn (Sep 23, 2015)

heh, here i am doing hacky php/wordpress/jquery projects *dodges flying objects*

well i do keep the code clean & indented, just not the syntax 'at all times'

maybe one day i'll wrap my head around MVC frameworks & stricter languages (in fact i never learned AS3, was on easy-going AS2 when flash died out)


----------

