How to use If statement in PHP?
Hi friends,
I am new to PHP. Before this I have studied the C, C++ and Visual Basic. We were having that subjects in junior college. I guess that the concept of the if statement in PHP should be very much similar to the same statement in C or C++ languages. But I don't know the coding that can be used in the PHP. So please help me..!! Tell me how to use If statement in PHP? Any coding related to same would be appreciable.
Re: How to use If statement in PHP?
The logic of the if statement is same in every language. The if statement is a conditional statement. Conditional statements are used to perform different actions based on different conditions. When you want to perform different actions for different decisions, you have to use the conditional statements like the if statement. The if statement is used to execute some code only if a specified condition is true. Hope that you get the point of using the if statement.
Re: How to use If statement in PHP?
The following is an example of the if statement. I have also provided you the syntax so that it can be easy for you to use the if statement. Here is a syntax :
Code:
if (condition) code to be executed if condition is true;
And here is an example of the same :
HTML Code:
<html>
<body>
<?php
$d=date("D");
if ($d=="Sat") echo "Enjoy the Party!";
?>
</body>
</html>
Re: How to use If statement in PHP?
Many times you want to display more than one line in a coder. If you want more than one line should be executed if a condition is true/false, then you should include the curly braces in which the lines must be enclosed. I have done the same thing in the given example :
HTML Code:
<html>
<body>
<?php
$d=date("D");
if ($d=="Mon")
{
echo "Welcome!!<br />";
echo "Lets Start the Project!!";
echo "Collect your Salary!!";
}
?>
</body>
</html>
Re: How to use If statement in PHP?
You can also use the if statement, for checking the false statements. You can check the below example in which the if statement is used for the false statement :
PHP Code:
$my_name = "differentcar";
if ( $my_name == "differentcar" ) {
echo "Your name is differentcar!<br />";
}
echo "Welcome to my homepage!";
The output will be
Code:
Welcome to my homepage!