/    Sign up×
Community /Pin to ProfileBookmark

Why Undefined Variable Error When It Has To Do With Function ?

Folks,

Why I get this error:
**http://localhost/Templates/validate_current_url.php?find=cars
Notice: Undefined variable: current_url in C:xampphtdocsTemplatesvalidate_current_url.php on line 31**

NOTE:
Current Url is:
https://localhost/Templates/validate_current_url.php?find=cars

[code]
<?php

function fetch_current_url($data=NULL)
{
//FETCH URL
if(isset($SERVER[‘HTTPS’]) && $SERVER[‘HTTPS’] === ‘on’)
{
$url = “https://”;
}
else
{
$url = “http://”;
}
// Append the host(domain name, ip) to the URL.
$url.= $_SERVER[‘HTTP_HOST’];
// Append the requested resource location to the URL
$url.= $_SERVER[‘REQUEST_URI’];
return $url; echo ‘<br>’;
echo ‘<b>’ .__LINE__ .'</b>’; echo ‘<br>’;
}

echo $current_url = fetch_current_url();

function validate_url_with_query_string($data=NULL)
{
if(filter_var($current_url, FILTER_VALIDATE_URL) && filter_var($current_url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED))
{
echo ‘Valid Url with Query String!’;
}
}
[/code]

How to fix it ? Write what on which line ?
Just need the code to check the url first. Then check if the url contains a query or not. If so then show echo. Else not.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 25.2021 — I'm guessing line 31 is the "if" line in the final function definition? Assuming that's the case, the problem is that $current_url does not exist within that function's scope. You'll need to pass it in as a function argument if you want to use it there -- or set it by calling that fetch_current_url() function within that function.
Copy linkTweet thisAlerts:
@developer_webauthorJul 25.2021 — @barand

Can you kindly show me how to do what NogDog suggested ? I mean a code sample editing my code.

Thank you very much!

Don;t you prowl this forum anymore ? I don't come across you anymore nowadays ?

How you fairing the Corona Virus ?
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...