Tutorials

[PHP] IF-ELSE Statement – A new way around

Writing PHP applications is pretty easy. Most people grasp the syntax rather quickly and will within short time be able to produce a script that works using tutorials, references, books, and blogs like the one we have here at Yeahhub.com.

The problem is that most people forget one of the most important aspects that one must consider when writing PHP applications which is the simplified and secured code.

PHP, which stands for PHP Hypertext Preprocessor, is a server-side embedded scripting language. In non-technical terms: a PHP processor is run on the server (Windows, or a flavor of UNIX). When a page is requested that contains PHP, the processor translates and executes all the commands in the page, and then outputs the result to the browser as regular HTML. Because this translation occurs on the server, a page written with PHP is viewable with any browser, on any operation system.

Also Read: Complete PHP Tutorial Series – Chapter 1 to Chapter 8

In PHP, conditional statements always plays an very important role in every aspect. Conditional Statements allow you to branch the path of execution in a script based on whether a single, or multiple conditions, evaluate to true or false. Put simply, they let you test things and perform various actions based on the results.

Basic If-Else PHP Statement –

The below syntax is a very basic If-Else statement used by most of the beginners. The below example illustrates the simplest kind of If Else Statement. If Statements always begin with “if”, followed by a condition surrounded in parentheses.

if( $a=1 ) {
echo “Hello World!”;
} else {
echo “Good Bye!”;
}

And for Nested If-Else, the syntax is:

if( $a=1) {
echo “Hello Sam!”;
} else if( $a=2 ) {
echo “Hello Bob!”;
} else {
echo “Good Bye!”;
}

Keep in mind that the positioning of the elements does not affect the execution of the script.

Intermediate Styling –

In this method, you just need to remove the curly braces from If and Else statement and replaced with by colon(:) symbol and also you need to add endif term at the end of the last statement.

if ($a == 1) :
echo “Hello John!”;
else :
echo “Good Bye!”;
endif;

This same syntax has been used by most of the CMS (Content Management Systems) just like WordPress.

And for Nested If-Else, the syntax is:

if ($a == 1) :
echo “Hello John!”;
elseif ($a == 2) :
echo “Hello Mark!”;
else :
echo “Good Bye!”;
endif;

Else If Statements are evaluated sequentially if the condition within the If Statement is false. When a condition within an Else If Statement evaluates to true, the nested statements are parsed, the script stops executing the entire If/Else if/Else Structure. The rest of the script proceeds to be parsed.

Advanced If-Else Styling –

You can easily complete the If-Else statement in just one line and this kind of statement can’t be used for Nested loop. In this type of styling, you need to set a condition and two output, it means the one value should be true.

echo ($a == 1)? “Welcome Chris!”: “Good Bye!”;

The above line of code check if value of a is equal to 1 or not. If it equal to a that means true, then it echo first text “Welcome Chris!”. If it false then it echo “Good Bye!”.

Have something to say about this article? Comment below or share it with us on Facebook or Twitter.

Yeahhub.com
Step by step hacking tutorials about wireless cracking, kali linux, metasploit, ethical hacking, seo tips and tricks, malware analysis and scanning.
https://www.yeahhub.com/

Leave a Reply

Your email address will not be published. Required fields are marked *

fifteen − eight =