A->b->c
might exist but c
might not exist. How do I check it?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
After some experimentation, I've discovered that the only reliable method of checking if a node exists is using
count($xml->someNode)
.Here's a test case: https://gist.github.com/Thinkscape/6262156
I use a helper function to check if a node is a valid node provided as a parameter in function.
Usage example:
Name Spaces
Be aware that if you are using name spaces in your XML file you will need to include those in your function calls when checking for children otherwise it will return ZERO every time:
SimpleXML always return Object. If there is no child, empty object is returned.
Thought I'd share my experience. Running on 5.4 I tried testing with 'isset' and 'empty' but neither worked for me. I ended up using is_null.
It might be better to wrap this in an
isset()
That way if
$A
or$A->b
don't exist... it doesn't blow up.