Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8804261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:33:27+00:00 2026-06-14T01:33:27+00:00

Im working on a simple php script that will return the number of search

  • 0

Im working on a simple php script that will return the number of search results from google search on a specific string (using cURL). When i keep my code global everything works fine but as soon as i create a function I get an error

Notice: Undefined variable: resultTagId in C:\wamp\www\tag.php on line 24
Notice: Trying to get property of non-object in C:\wamp\tag.php on line 24

here is my code

<?php

$resultTagId = "resultStats"; 
$encodedNames = $_GET['names'];
$names=json_decode($encodedNames);


    getNumber($names[0]);

function getNumber($name) // before i used to set $name = $names[0] and everything worked fine
{
    $name = str_replace(" ", "+", trim($name));

    $url='http://www.google.com/search?q='.$name.'&ie=utf-8&oe=utf-8&aq=t';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec ($ch);
    curl_close ($ch);

    $dom = new DOMDocument();
    @$dom->loadHTML( $data );
    $resultsTag =  $dom->getElementById($resultTagId)->nodeValue;

    $results =  preg_replace("/[^0-9]/","" ,$resultsTag);
    echo $results;
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:33:28+00:00Added an answer on June 14, 2026 at 1:33 am

    There are a variety of ways that you can do this (including using a global, which tends to make things messy), but the best way is to pass your variable as an argument.

    Change the arguments for the function:

    function getNumber($name, $tagId) {
    

    And when you’re calling the function, pass your variable:

    getNumber($names[0], $resultTagId)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple working PHP script to write an HTML table from a
I'm working on (what should be) a simple script that will add text to
I have simple php validation form that is halfway working. If you leave the
PHP beginner working from a tutorial. I'm trying to do a simple upload from
I'm trying to write a simple php script to take in data from stdin
I'm working on an intermediary form that will gather some data from the user
I am attempting to create a simple tracker using php and google maps. Right
I am working on a website using a PHP script to display information formatted
I'm working on an iPhone project that needs to receive data from a PHP
I have written some JavaScript code that will read from the Google Maps API

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.