https://dev.webpages.dk/  
 
PHP How to Use IF
PHP How to Use IF:

IF is a commonly used statement. It's used to see if a variable is of a certain value, and then react on it.
You may be looking for information about the 'switch' statement. Depending on your need. - I am sorry but there is not a t this point apage here, about that... Yet.

Also you should see the page about IF and ELSE.

 IF

How do you use IF in PHP?

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.

 HTML

<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>

 PHP

<?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;
    }
 ?>
Icons made by Freepik from www.flaticon.com Here is an example of how to use IF in PHP.
04:51:48