Introduction to Conditional Statements

If a Condition is True

Introduction

 A Boolean comparison is an operation used to find out whether one value is greater than, lower than, or equal to, another. The operation produces a Boolean value that holds the result. To let you perform the comparison, PHP provides various operators and keywords. The if keyword is used to created an expression that compares two values. The formula to use it is:
```if(expression)
statement```

The if keyword is required and it is followed by parentheses. In the parentheses, create a Boolean expression in the form:

`operand1 Boolean-operator operand2`

A Boolean Value

A value is Boolean if it can be, or can hold, a value as TRUE or FALSE.

A Boolean Variable

Introduction

To use a Boolean value, you can declare a variable for it. Here is an example:

`<?php \$isMarried ?>`

Initializing A Boolean Variable

When declaring a Boolean variable, you can initialize it with a true or a false value. Here is an example:

`<?php \$isMarried = true ?>`

If you don't initialize the variable, it may receive a value of false. You can also assign a Boolean variable to another. Here is an example:

```<?php
\$isMarried = true;
\$maritalStatus = \$isMarried;
?>```

Casting a Value to or From Boolean

To convert a value to Boolean, precede it with (bool) or (boolean). The conversions use the following logic:

• Natural Numbers:
• If 0 is converted to Boolean, the result is 0
• If a positive or negative number is converted to Boolean, the result is 1
• Floating-Point Numbers:
• If 0.0 is converted to Boolean, the result is 0
• If a positive or negative decimal number is converted to Boolean, the result is 1
• Strings: It is first important to understand that if a number (natural or real) is provided in double-quotes (example \$nbr = "24.50"), it would automatically be converted to a number if/when it is involved in an arithmetic operation. As a result, "24.50" is practically equivalent to 24.50. This means that if the conversion is performed on a number provided in a double-quoted string, the conversion would follow the previous two logical points (conversion to natural and real numbers). Other than that:
• If an empty string (a string that contains nothing at all, example \$something = "") is converted to Boolean, the result is 0
• If a string that contains something (anything that is not nothing) is converted to Boolean, the result is 1

Logic-Based Controls

Check Boxes

A check box is a small square box that allows the user to select an item or to deselect it. To get a check box, create an input element and specify the control type as checkbox, CheckBox, or CHECKBOX. Here is an example:



A check box doesn't display what it is used for. Therefore, you should (always) add a label close to it to help the user know the role of this control. Here is an example:

```<?php
echo "<form name='frmEmploymentDecision' action='employee.php' method='post'>
<h3>Human Resources: Employment Decision</h3>
<table border='4'>
<tr>
<td>Employee Name:</td>
<td><input name='txtEmployeeName' type='text' /></td>
</tr>
<tr>
<td>Full-Time?</td>
<td><input name='chkFullTime' type='checkbox'  /></td>
</tr>
</table>
</div>
</form>";
?>```

This would produce:

When the user clicks the check box, its value becomes checked. You can programmatically set this state by using the checked attribute of this tag. If you set this atribute (or you assign it true, checked, yes, or on, etc), the check box becomes checked. Otherwise, it would be unchecked. Here is an example of setting that attribute:

`<input type="checkbox" checked />`

If you plan to use a check box in a script, you should give it a name. This is done by assigning a string to the Name attribute.

A radio button is a small round box that allows the user to select an item from a group. To get a radio button, create an input element and specify the type attribute as radio. Here is an example:



To make your radio buttons effective, you should always provide at least two radio buttons in a group.

One of the most important attributes to set on the radio button is the name. This attribute should be used even if you don't plan to use the radio button in a script. Many radio buttons in the same group can/should (must) use the same name. This common name will be helpful in determining what particular control was selected. Therefore, the same value (string) for the Name attribute can be assigned to each radio button of the same group. Here is an example:

```<?php
echo "<html>
<style type='text/css'>
#main-title { font-size: 28px }
#main-title, #sub-title {
font-weight: bold;
text-align:  center;
}
#whole {
margin: auto;
width:  600px;
}
</style>
<body>
<div id='whole'>
<form name='frmCompoundInterest' method='POST'>
<p id='main-title'>Compound Interest</p>

<p id='sub-title'>Compound Frequency</p>

<table align='center'>
<tr>
<td>Monthly</td>
</tr>
<tr>
<td>Quarterly</td>
</tr>
<tr>
<td>Semiannually</td>
</tr>
<tr>
<td>Annually</td>
</tr>
</table>
</form>
</div>
</body>
</html>";
?>```

This would produce:

The checked attribute, as applied to the check box, can be used if you want one of the buttons in the group to be selected by default. To do this, apply the checked attribute (you can optionally set its value to on or true. Here is an example:

 Select your T-Shirt size Small Medium Large X-Large
<table border="0" width="176">
<tr>
<td width="168" colspan="2">Select your T-Shirt size</td>
</tr>
<tr>
<td width="119">Small&nbsp;</td>
</tr>
<tr>
<td width="119">Medium</td>
</tr>
<tr>
<td width="119">Large</td>
</tr>
<tr>
<td width="119">X-Large</td>
</tr>
</table>

To find out what radio button is/was clicked at one time, first assign a different value to each radio button. The value can be an integer. Here are examples:

```<?php
echo "<html>
<style type='text/css'>
#main-title { font-size: 28px }
#main-title, #sub-title {
font-weight: bold;
text-align:  center;
}
#whole {
margin: auto;
width:  400px;
}
</style>
<body>
<div id='whole'>
<form name='frmCompoundInterest' method='POST'>
<p id='main-title'>Compound Interest</p>

<p id='sub-title'>Compound Frequency</p>

<table align='center'>
<tr>
<td>Monthly</td>
</tr>
<tr>
<td>Quarterly</td>
</tr>
<tr>
<td>Semiannually</td>
</tr>
<tr>
<td>Annually</td>
</tr>
</table>
</form>
</div>
</body>
</html>";
?>```

The value can also be a string. Here are examples:

```<?php
echo "<html>
<title>Payroll Preparation</title>
<style type='text/css'>
#main-title { font-size: 28px }
#main-title, #sub-title {
font-weight: bold;
text-align:  center;
}
#whole {
margin: auto;
width:  400px;
}
</style>
<body>
<div id='whole'>
<form name='frmPayroll' method='POST'>
<p id='main-title'>Payroll Preparation</p>

<p id='sub-title'>Payroll Period</p>

<table align='center'>
<tr>
<td>Weekly</td>
value='Weekly'></input></td>
</tr>
<tr>
<td>Biweekly</td>
value='Biweekly'></input></td>
</tr>
<tr>
<td>Semimonthly</td>
value='Semimonthly'></input></td>
</tr>
<tr>
<td>Monthly</td>
value='Monthly'></input></td>
</tr>
<tr>
<td>Quarterly</td>
value='Quarterly'></input></td>
</tr>
<tr>
<td>Semiannually</td>
value='Semiannually'></input></td>
</tr>
<tr>
<td>Annually</td>
value='Annually'></input></td>
</tr>
<tr>
<td>Miscellaneous</td>
value='Miscellaneous'></input></td>
</tr>
</table>
</form>
</div>
</body>
</html>";
?>```

This would produce:

Based on this, to find out whether a certain radio is selected, simply compare the name to the desired value. This can be done as follows:

```if(htmlspecialchars(\$_POST['rdoPayrollPeriod']) == 'Weekly')
. . . blah blah blah . . .```

A Combo Box

A combo box is a web control that displays a list of items. To get a combo box, create an HTML element named select. Here is an example:

```<form name="frmPayroll">
<p>Employee Name: <input name="txtEmployeeName" type="text"></p>
<p>Marital Status: <select></select></p>
</form>```

This would produce:

Each item of a combo box is created as its own element but inside the select element. An item is created in an element named option. Here are examples:

```<form name="frmPayroll">
<p>Employee Name: <input name="txtEmployeeName" type="text"></p>
<p>Marital Status:
<select>
<option>Single</option>
<option>Married</option>
</select></p>
</form>```

This would produce:

If you want to select an item when creating the combo box, set that item's selected attribute. Here is an example:

```<form name="frmPayroll">
<p>Employee Name: <input name="txtEmployeeName" type="text"></p>
<p>Marital Status:
<select>
<option>Single</option>
<option selected>Married</option>
</select></p>
</form>```

This would produce:

After creating a combo box, you can get the string that is selected by comparing it to the string you want. As an alternatively, when creating a combo box, you can create a value for each item. This is done by adding an attribute named value and assigning a value of your choice. Here is an example:

```<form name="frmPayroll">
<p>Employee Name: <input name="txtEmployeeName" type="text"></p>
<p>Marital Status:
<select>
<option value="1">Single</option>
<option value="2">Married</option>
</select></p>
</form>```