-2

When I run the following PHP code:

<?php

$start = "https://en.wikipedia.org";

function follow_links($url) {

    $doc = new DOMDocument();
    $doc->loadHTML(file_get_contents($url));

    $linklist = $doc->getElementsByTagName("a");

    foreach ($linklist = $link) {
        echo $link->getAttribute("href")."\n";
    }
}

follow_links($start);

Error:

Parse error: syntax error, unexpected ')' in C:\xampp\htdocs\test.php on line 12

What am I doing wrong, because I swear that I have closed all brackets correctly.

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42

2 Answers2

0

Change your foreach loop to this. foreach reference

foreach ($linklist as $link) {
        echo $link->getAttribute("href")."\n";
    }
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
0

You need to change this

foreach ($linklist = $link) {

to this..

foreach ($linklist as $link) {
Rick
  • 712
  • 5
  • 23