How to use IF in PHP, simple
TAGS: PHP, if


In this posting we will look shortly on using IF in PHP scripts.
In the code below you can see three radiobuttons and a submitbutton. When you tick one of the radiobuttons and click the submit you will put a string into the variable $check that will be shown on submit.

We read the value using $_POST['question'], and with IF we assign a string to the variable, depending on wich radiobutton was the selected one.

In the radiobuttons on the HTML form field you can see that the values are set to 'yes', 'no' and 'unsure', this is what we look for. If you have other input types in your form, then what you look for are these VALUES.

Here is the code.

<?php

if (isset($_POST['readme']))  { 
checkme(); 
} 

function checkme() {
$question = $_POST['question'];
if ( $question == 'yes' ) { $check = "You like PHP!"; }
if ( $question == 'no' ) { $check = "You don't like PHP?"; }
if ( $question == 'unsure' ) { $check = "You are unsure.."; }
echo $check;
}

echo "<HTML>
<BODY>
<FORM METHOD='POST' ACTION=''>
<TABLE>

<TR> 
<TD>Do you like PHP: </TD> 
<TD><INPUT TYPE='radio' NAME='question' VALUE='yes'> Yes</TD> 
</TR>

<TR> 
<TD></TD> 
<TD><INPUT TYPE='radio' NAME='question' VALUE='no'> No</TD> 
</TR>

<TR> 
<TD></TD> 
<TD><INPUT TYPE='radio' NAME='question' VALUE='unsure'> Unsure</TD> 
</TR>

<TR> 
<TD></TD> 
<TD><INPUT TYPE='submit' NAME='readme' VALUE='Okay, try this'> 

</TR> 
</TABLE> 
</FORM> 
</BODY> 
</HTML>";
?>


In the next post I will show you how IF/ELSE works in PHP.