
DOMElement::hasAttribute 메서드는 속성이 존재하는지 여부를 확인하는 메서드입니다. 속성이 존재하는 경우 true, 존재하지 않는 경우 false를 반환합니다.
속성값을 반환하는 메서드는 DOMElement::getAttribute() 메서드입니다. 이 메서드는 속성이 존재하는 경우 속성값을 반환하고, 존재하지 않는 경우 null을 반환합니다.
속성이 존재하지만 속성값이 없는 경우, DOMElement::hasAttribute 메서드는 true를 반환합니다. DOMElement::getAttribute() 메서드는 null을 반환합니다.
예를 들어, HTML 문서에서 다음과 같은 요소가 존재한다고 가정해 보겠습니다.
#hostingforum.kr
html
이 경우, DOMElement::hasAttribute 메서드는 다음과 같이 동작합니다.
#hostingforum.kr
php
$element = new DOMElement('div', 'test');
$element->setAttribute('my-attribute', '');
echo $element->hasAttribute('my-attribute'); // true
echo $element->getAttribute('my-attribute'); // ''
속성이 존재하지만 속성값이 없는 경우, DOMElement::hasAttribute 메서드는 true를 반환하고, DOMElement::getAttribute() 메서드는 null을 반환하지는 않습니다. 속성값은 빈 문자열 ''이 반환됩니다.
2025-03-13 16:22