/    Sign up×
Community /Pin to ProfileBookmark

PHP Web Page With Section of Code Conditional on Passed Variable

I have a PHP web page on which there is a section of code that I would like to make conditional on the passing of a certain variable to the page.

So in other words, if the page is pulled up like this:

[url]http://mysite.com/mypage.php?var=show[/url]

Then I would like the page to execute the block of code in question.

However, if the URL is typed like this:

[url]http://mysite.com/mypage.php[/url]

I would like for this block of code to NOT be executed. So it could be commented out or whatever works.

What would be the best way to accomplish this? Thanks in advance for any input anyone has!

J

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 10.2009 — [code=php]
if(!empty($_GET['var']) and $_GET['var'] == 'show')
{
// do something
}
else
{
// do something else
}
[/code]
Copy linkTweet thisAlerts:
@jprazenauthorDec 10.2009 — Thanks, that's exactly what I needed!
Copy linkTweet thisAlerts:
@jprazenauthorDec 10.2009 — Hmmm... Wait a minute. I didn't specify that the block of code I need to execute is HTML inside the body tags. This code won't work there will it?
Copy linkTweet thisAlerts:
@NogDogDec 10.2009 — [code=php]
<?php
if(!empty($_GET['var']) and $_GET['var'] == 'show')
{
?>
<p>Foo.</p>
<?php
}
else
{
?>
<p>Bar.</p>
<?php
}
?>
[/code]
Copy linkTweet thisAlerts:
@jprazenauthorDec 10.2009 — Thanks for the help!
×

Success!

Help @jprazen 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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