How to skip null value and to continue to other value in php [duplicate]

I have to generate some keyword based on a link …if this link is null i want to skip and to continue with other results .

I cannot find the way how to skip and to store return only data i want :

 public function getwordbasedonURL($link_platform_product_page, $target_locale_iso, $language, $limit)
{
    if ($link_platform_product_page != null) {

        // Make sure that keywords and/or page URL were specified. The request must have exactly one
        // of urlSeed, keywordSeed, or keywordAndUrlSeed set.
        if (empty($keywords) && is_null($link_platform_product_page)) {
            throw new InvalidArgumentException(
                'At least one of keywords or page URL is required, but neither was specified.'
            );
        }

        // Specify the optional arguments of the request as a keywordSeed, urlSeed,
        // or keywordAndUrlSeed.
        $requestOptionalArgs = [];          
        $customerId = $decodedJson["customerId"];

        $locationIds = ['10000'];            
        // $languageId = '1001';
        $keywords = [];


        if (empty($keywords)) {
            // Only page URL was specified, so use a UrlSeed.
            $requestOptionalArgs['urlSeed'] =
                new UrlSeed(['url' => new StringValue(['value' => $link_platform_product_page])]);
        } elseif (is_null($link_platform_product_page)) {
            // Only keywords were specified, so use a KeywordSeed.
            $requestOptionalArgs['keywordSeed'] = new KeywordSeed([
                'keywords' => array_map(function ($keyword) {
                    return new StringValue(['value' => $keyword]);
                }, $keywords)
            ]);
        } else {

            // Both page URL and keywords were specified, so use a KeywordAndUrlSeed.
            $requestOptionalArgs['keywordAndUrlSeed'] = new KeywordAndUrlSeed([
                'url' => new StringValue(['value' => $link_platform_product_page]),
                'keywords' => array_map(function ($keyword) {
                    return new StringValue(['value' => $keyword]);
                }, $keywords)
            ]);
        }

        // Create a list of geo target constants based on the resource name of specified location
        // IDs.
        $geoTargetConstants = array_map(function ($locationId) {
            return new StringValue(
                ['value' => ResourceNames::forGeoTargetConstant($locationId)]
            );
        }, $locationIds);

        // Generate keyword ideas based on the specified parameters.
        $response = $keywordPlanIdeaServiceClient->generateKeywordIdeas(
            $customerId,
            // Set the language resource using the provided language ID.
            new StringValue(['value' => ResourceNames::forLanguageConstant($language)]),
            // Add the resource name of each location ID to the request.
            $geoTargetConstants,
            // Set the network. To restrict to only Google Search, change the parameter below to
            // KeywordPlanNetwork::GOOGLE_SEARCH.
            KeywordPlanNetwork::GOOGLE_SEARCH_AND_PARTNERS,
            $requestOptionalArgs
        );

        $keywords = "";
        $i = 0;

        // Iterate over the results and print its detail.
        foreach ($response->getResults() as $result) {
            if ($result != null)
            if ($result->getKeywordIdeaMetrics()) {
                $compet = $result->getKeywordIdeaMetrics()->getCompetition();

                $log = $this->getLogger();
                $log->err($response->serializeToJsonString());

                if ($compet == 4) {
                    // echo str_word_count($words);
                    $keywords .= $result->getText()->getValue() . ",";

                        $keywordslist = implode(',', array_unique(explode(',', $keywords  )));

                        if ($i++ == $limit) break;
                    }

                }
                return $keywordslist;

        }
    }

    return null;

    }

After word are genereated , i got this error

[SymfonyComponentDebugExceptionContextErrorException]
  Notice: Undefined variable: keywordslist

How to prevent this error and to get only the result when link_platform_product_page is not null

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *