Showing posts with label If statements. Show all posts
Showing posts with label If statements. Show all posts

HTML code directly in IF Conditions

This is the most amazing thing in php conditions, and exactly the if statement.
In a php condition, you can put directly a html code without the echo function :)

This is what I'm talking about, with echo function, then without.:



if ($condition == 'something') {
echo '
My Website...
'; } else { echo '
My Website...
'; } ?>
With the new way I'm talking about, the script will be like this:




 if ($condition = 'something') {?>
<div class="style1">My Website...<div>
 } else { ?>
<div class="style2">My Website2...<div>
 }?>




Now  why this can be useful ?
In fact, when you are using a html editor like dreamweaver, you will be able to design easily the html code in the if statement, you can add tables, images, div, change colors ... directly using the design view, or when you use the code view you can see the html code highlighted (like in the exemple above) wich believe me will help you a lot and make your programming so much easier !


Thank you.

PHP if -- else statement in conditions

If statement enable programmer to execute a script only when a condition is true.
And Else enable to quickly execute a script when the condition fails.

PHP Code:
<?php
$myname = "Smith" ;
if ( $myname == "Smith" ) {
echo "hello ! Your are Smith"; // wil be displayed only if condition is true
} else
{
echo "hello ! Your are not Smith";
// wil be displayed only if condition is false
}
?>
This may also be writed like this:
<?php
$myname = "Smith" ;
if ( $myname == "Smith" )
echo "hello ! Your are Smith"; // wil be displayed only if condition is true
else

echo "hello ! Your are not Smith";
// wil be displayed only if condition is false
?>
"{" and "}" can be deleted because there is only one line in if and else statements. If you want to create many lines of script, so then you should use"{" and "}":

PHP Code:
<?php
$myname = "Smith" ;
if ( $myname == "Smith" )
{
echo "hello ! Your are Smith"; // wil be displayed only if condition is true
echo "You are wellcome";
} else

echo "hello ! Your are not Smith";
// wil be displayed only if condition is false
?>
Here the "{" and "}" are necessary in the if statement, but not necessary in the else one as else statement has only one line of code.

PHP simple if statement

if statement in php is similar to almost all languages, and it necessary to make a usefull script.

If statement enable you to execute a script only if a condition is true.

Simple if statement:

php code:
<?php
$age = 28;

if ($age == 28)
echo "hello ! Your age is 28";
?>
Display:
hello ! Your age is 28

But if you want to do many lines of code under the if statement, you should do:

php code:
<?php
$age = 28;

if ($age == 28)
{
echo "hello ! Your age is 28";
echo "
" ;

echo "You are wellcome !";

?>
Display:
hello ! Your age is 28
You are wellcome !