Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP question

Status
Not open for further replies.

jammer1221

Programmer
Joined
Jul 30, 2003
Messages
352
Location
US
Hi I have a PHP question about using a sorta if statement...i was wondering if it was possible to write soemthing were you get a varible from another page an if that varible is for example y then write You choose y or something like that....
Code:
<?PHP
choice = $_REQUEST['choice'];
if choice != y
echo(&quot;You didnt choose y&quot;);
else
echo(&quot;You choose y&quot;)
?>
I'm not sure if this will work or if theres something like this you can do in PHP well thanks in advance
Jammer1221
 
Typically, I use either array_key_exists() or isset() to determine whether a value has been passed to a script.

Also, use $_GET or $_POST as appropriate rather than $_REQUEST. Otherwise you can fall prey to the same variable poisoning described here:
Want the best answers? Ask the best questions: TANSTAAFL!!
 
OK...I'm sorry I have no idea what that means or how you would use either isset() or array_key_exist()...could you please give me an example or a place to find that...that link you sent before probably had the answer but all that stuff means nothing to me as I barely get any of it thank you
Jammer1221
 
The ultimate source of information on PHP's functions is the PHP online manual, which is available at and at a large number of mirrors around the world. The manual is full of example script samples. I recommend you try there.

array_key_exists() or isset() are two methods of determining whether an array, such as $_POST or $_GET contains a value. It's a good idea to test for the presence of a value before you try to use it. If $_POST['choice'] does not exist, the line &quot;$choice = $_POST['choice'];&quot; will generate a warning.


Here's working code, functionally equivalent to yours but with the addition of error-checking.

Code:
<?php
if (array_key_exists ('choice', $_POST) && $_POST['choice'] == 'y')
{
   print 'You chose y';
}
else
{
   print 'You didn\'t choose y';
}
?>



Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top