3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html1 = <<<EOT <option selected="selected" value="1">value text</option> EOT; $html2 = <<<EOT <option selected value="1">value text</option> EOT; $html3 = <<<EOT <option value="the devil with **selected** ">value text</option> EOT; function get_selected($html) { $doc = new DOMDocument; $doc->loadHTML($html); $xpath = new DOMXpath($regex); foreach ($xpath->query("//option") as $option) { $selected_value = $xpath->evaluate('string(@value)', $option); return $selected_value; } } var_dump( get_selected($html1) ); var_dump( get_selected($html2) ); var_dump( get_selected($html3) );
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Warning: Undefined variable $regex in /in/betQW on line 17 Fatal error: Uncaught TypeError: DOMXPath::__construct(): Argument #1 ($document) must be of type DOMDocument, null given in /in/betQW:17 Stack trace: #0 /in/betQW(17): DOMXPath->__construct(NULL) #1 /in/betQW(24): get_selected('<option selecte...') #2 {main} thrown in /in/betQW on line 17
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: regex in /in/betQW on line 17 Fatal error: Uncaught TypeError: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, null given in /in/betQW:17 Stack trace: #0 /in/betQW(17): DOMXPath->__construct(NULL) #1 /in/betQW(24): get_selected('<option selecte...') #2 {main} thrown in /in/betQW on line 17
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught TypeError: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, null given in /in/betQW:17 Stack trace: #0 /in/betQW(17): DOMXPath->__construct(NULL) #1 /in/betQW(24): get_selected('<option selecte...') #2 {main} thrown in /in/betQW on line 17
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Notice: Undefined variable: regex in /in/betQW on line 17 Catchable fatal error: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, null given, called in /in/betQW on line 24 and defined in /in/betQW on line 17
Process exited with code 255.

preferences:
225.46 ms | 402 KiB | 326 Q