0% found this document useful (0 votes)
16 views

The If Statement: PHP Code

The PHP if statement allows code to execute conditionally based on whether an expression evaluates to true. It is used to make decisions and ensure the appropriate code runs. For example, an if statement can print "Happy New Year!" on January 1st each year by automating the process well in advance. The basic structure of an if statement tests if a value is true, and if so, the code within the curly braces will execute. An example prints "Your name is someguy!" if a variable called $my_name equals the string "someguy".

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

The If Statement: PHP Code

The PHP if statement allows code to execute conditionally based on whether an expression evaluates to true. It is used to make decisions and ensure the appropriate code runs. For example, an if statement can print "Happy New Year!" on January 1st each year by automating the process well in advance. The basic structure of an if statement tests if a value is true, and if so, the code within the curly braces will execute. An example prints "Your name is someguy!" if a variable called $my_name equals the string "someguy".

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

The If Statement

The PHP if statement is very similar to other programming languages use of the if statement, but for those
who are not familiar with it, picture the following:
Think about the decisions you make before you go to sleep. If you have something to do the next day, say
go to work, school, or an appointment, then you will set your alarm clock to wake you up. Otherwise, you will
sleep in as long as you like!
This simple kind of if/then statement is very common in every day life and also appears in programming
quite often. Whenever you want to make a decision given that something is true (you have something to do
tomorrow) and be sure that you take the appropriate action, you are using an if/then relationship.

The PHP If Statement

The if statement is necessary for most programming, thus it is important in PHP. Imagine that on January
1st you want to print out "Happy New Year!" at the top of your personal web page. With the use of PHP if
statements you could have this process automated, months in advance, occuring every year on January 1st.
This idea of planning for future events is something you would never have had the opportunity of doing if
you had just stuck with HTML.

If Statement Example

The "Happy New Year" example would be a little difficult for you to do right now, so let us instead start off
with the basics of the if statement. The PHP if statement tests to see if a value is true, and if it is a segment of
code will be executed. See the example below for the form of a PHP if statement.

PHP Code:
$my_name = "someguy";

if ( $my_name == "someguy" ) {
echo "Your name is someguy!<br />";
}
echo "Welcome to my homepage!";

You might also like