You are advised to check corresponding YouTube video at the end of this article.
if-else
statements are control structures used in programming to execute different actions based on whether a condition is true or false.
The if
statement executes a block of code only if a specified condition is true, while the else
statement is used to execute a block of code when the if
condition is false.
<?php
$number = 5;
if ($number == 5) {
echo "Operation from Above is True";
}
else {
echo "Number is not 5";
}
?>
This PHP code initializes a variable $number
with the value 5
. Then, it uses an if-else statement to check if the value of $number
is equal to 5
.
If the condition is true, meaning $number
is equal to 5
, the code inside the curly braces {}
following the if
statement will be executed. In this case, it will output the string "Operation from Above is True" using the echo
statement.
If the condition is false, meaning $number
is not equal to 5
, the code inside the curly braces {}
following the else
statement will be executed. In this case, it will output the string "Number is not 5" using the echo
statement.
In the above example, the parentheses ()
are used to contain the conditional statement that is being evaluated. The curly braces {}
are used to contain the code that will be executed if the condition is true.
Using parentheses is important because it helps to ensure that the condition is properly evaluated. Without them, the code may not work as intended or may result in unexpected behavior.
Using curly braces is also important because it ensures that only the code within the braces is executed if the condition is true. Without them, the code may execute in unexpected ways or may not execute at all.
<?php
$str = "Something";
if ($str == "Something") {
echo "Strings are equal";
}
else {
echo "Strings are different";
}
?>
This PHP code declares a string variable $str
and checks if its value is equal to the string "Something" using an if
statement.
If the condition $str == "Something"
is true, the code inside the curly braces { }
following the if
statement will be executed, which in this case is the echo
statement "Strings are equal"
.
If the condition is false, the code inside the curly braces following the else
statement will be executed, which in this case is the echo
statement "Strings are different"
.
So in this case, since $str
is equal to "Something", the output of the code will be "Strings are equal".
form.html
<form action="grabthings.php" method="post">
<label>Name:</label>
<input type="text" name=name>
<br><br>
<input type="submit" value="Submit">
</form>
This code creates an HTML form that asks the user to input their name. The form uses the POST method to submit the data to a PHP file called "grabthings.php" for processing.
The form has one input field with the name attribute set to "name", which means that the data entered by the user will be stored in the $_POST superglobal array with the key "name". When the user submits the form, the data will be sent to the "grabthings.php" file for processing.
grabthings.php
<?php
$get_name = $_POST["name"];
if ($get_name == "John") {
echo "John comes from Form";
}
else {
echo "You entered something different";
}
?>
This PHP code captures the value of the input field named "name" from an HTML form submitted via the HTTP POST method.
The code then checks if the value of the variable $get_name is equal to the string "John". If it is, it prints the message "John comes from Form". If not, it prints the message "You entered something different".
In summary, the code compares the user input with the string "John" and prints a message based on the comparison result.
No comments:
Post a Comment