Skip to main content

Amazon Ads

Control Statement in PHP with Example

PHP Control Statement

What is Control Statement in PHP?

A block of code which decide the execution way of programme according to the value of the given condition.

PHP Control Statement with Example

There are some control statements that PHP supports.

1. PHP if Statement

If a given condition is true, only then if statement will execute.
Syntax:
if(condition) {
block1 of code;
}

if Statement Example: 
<?php
if ($15 > $10)
echo “15 is greater than 10”;
?>

Output:
15 is greater than 10

2. PHP if…else Statement

In PHP, if a statement is executed only when a specified condition is true. It evaluated to its Boolean values. If the condition is FALSE, it’ll ignore it.
Syntax:
if(condition) {
block1 of code
}else {
block2 of code
}

if - else Statement Example: Find a largest number between two numbers using if else statement;
<?php
$first_value = 5;
$second_value = 10;
if($first_value > $second_value) {
echo “$first_value is greater than $second_value”;
} else {
echo  “$second_value is greater than $first_value”;
}
?>
Output:
10 is greater than 5

3. PHP if….elseif….else Statement 

With this statement, you can select one of several blocks of code to be executed.
Syntax:
if (condition)
first blockto be executed if condition is true;
elseif (condition)
Second block to be executed if condition is true;
else
final block to be executed if condition is false;
if -else if else statement Example:
<?php
$d = date("D");
if($d == "Thu"){
echo "Have a nice weekend!";
} elseif($d == "Sun"){
echo "Have a nice Sunday!";
} else{
echo "Have a nice day!";
}
?>

Output:
Have a nice day!

4. PHP Switch Statement

It is like IF statements. If you want to select one of several blocks of code to be executed, use the Switch statement.
Syntax:
switch ( )
{
case1
break;
case2
break;
default:
default code
break;
}
?>

PHP Switch case Statement Example:
<?php
         $d = date("D");

         switch ($d){
            case "Mon":
               echo "Today is Monday";
               break;

            case "Tue":
               echo "Today is Tuesday";
               break;

            case "Wed":
               echo "Today is Wednesday";
               break;

            case "Thu":
               echo "Today is Thursday";
               break;

            case "Fri":
               echo "Today is Friday";
               break;

            case "Sat":
               echo "Today is Saturday";
               break;

            case "Sun":
               echo "Today is Sunday";
               break;

            default:
               echo “which day is this ?";
         }
      ?>

Output:
Today is Wednesday

Comments

Post a Comment

Thanks for Reach us !

Popular posts from this blog

A Beginners Guide to Java JDBC

Introduction to JDBC in Java Java Database Connectivity is a standard Java API used to connect Java application with Database. Java JDBC is used to communicate with the different type of Databases like Oracle, MS Access, My SQL and SQL Server. JDBC can also define as the platform-independent interface between a relational database and Java programming. It allows a java program to execute the SQL statement and retrieve the result from the database. JDBC uses drivers to connect with database.  Different Types of JDBC Driver in Java  There are four types of JDBC Drivers:
1. JDBC-ODBC Bridge Driver 2. Natïve Driver 3. Network Protocol Driver 4. Thin Driver Why should we use JDBC? There is some critical point which explains why we should use JDBC.
1. JDBC API is Standard API. We can communicate with any Database without revising our Application, i.e. it is Database Independent API. 2.JDBC Drivers developed in Java, and hence JDBC Concept is applicable for any Platform. i.e., JDBC is Platfor…

10 Difference between C++ and C# Programming Language

What is the Difference Between C++ and C#?

The 10 main Difference between C++ and C# are as follow:

No. C++ C#
1. C++ is a low-level language. C# is a High-level Language.
2. It supports the multiple inheritances. It does not support multiple inheritances.
3. In C++, you require to manage memory manually. C# automatically manages Memory.
4. In C++, after compiling code changed into machine code. In C#, after compiling code is changed into an intermediate language code.
5.

How to do SEO For E-Commerce Website?

A Beginners Guides to Optimization of an E-Commerce website SEO: Search Engine Optimization
Search Engine Optimization, every websites backbone, is the tool that can make or break any website. Optimization of an e-commerce website is more critical and complicated than doing SEO for any other website. It is because your e-commerce site can have many more hundreds or even thousands of product listings that require you to follow best practices ensuring the performance of your website at an optimum level.
Strategy Forgetting to the top of Google search results there should be little margin or room for errors while conducting SEO for your e-commerce website. It is because ideally, you must be looking to be within top three positions to tremendously increase the traffic for your website. The number one ranked result receives the search of 31.24 clicks. The number two gets half of it around 14.04 clicks and so on coming down for rest three ranked searches. These statistics show the importan…

A Beginners Guide to Apache Ant Tutorial Step by Step

Introduction to Apache Ant Tutorial

Ant stands for "Another Neat Tool". It is a general purpose build tool. This build tool is based on java from Apache Software Foundation. Apache Ant's build files are written in XML and they take benefit of being an open standard, portable and easy to learn.

What is Apache Ant?Ant was the first build tool, the reason to build this tool is that created for building Java applications. It is used for building and deploying Java projects but can be used for every feasible repeated task, e.g. generating documentation.
Ant is a cross-platformed and open source project which provides source code, blueprints, and documentation freely available to all the developers. And it provides the facility to users to extend the project by creating their own “antlibs”. Immediately, it became popular as a build tool between Java developers. Ant was created because of a number of problems with Unix's and it was a good replacement for the "Make" b…