PHP if..Else..Elseif
PHP conditional statements are used to test the conditions. It supports different types of conditional statement. Following are the types of conditional statement
- if statement
- ..else statement
- ..elseif....else statement
- switch statement
PHP if Statement PHP if statement is used to execute the code block if the one condition is true.
if (condition) {
Php code to be executed if condition is true;
}
Example:
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<?php
$nos=5;
if($nos==5){
echo "Nos is Equall to: 5";
}
?>
</body>
</html>
Output
Nos is Equall to 5PHP if… else Statement PHP if- else statement is used to execute the condition whether it is true of false.
Syntax:
if (condition) {
php code will be executed if condition is true;
} else {
phpcode will be executed if condition is false;
}
Example
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<?php
$age=18;
if($age==17){
echo "Congratulation! You are eligible for vote";
}
else{
echo "Sorry! You are not eligible for vote";
}
?>
</body>
</html>
Output
Sorry! You are not eligible for votePHP if..elseif ..else Statement PHP if- elseif-else statement is used to execute one or more condition whether it is true of false.

if (condition) {
php code wil be executed if this condition is true;
} elseif (condition) {
php code wil be executed if this condition is true;
} else {
php code wil be executed if all conditions are false;
}
Example:
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<?php
$day=4;
if($day==1){
echo "Today is Sunday";
}
elseif($day==2){
echo "Today is Monday";
}
elseif($day==3){
echo "Today is Tuesday";
}
elseif($day==4){
echo "Today is Wednesday";
}
elseif($day==5){
echo "Today is Thursday";
}
elseif($day==6){
echo "Today is Friday";
}
elseif($day==7){
echo "Today is Saturday";
}
else{
echo"Invalid Day !";
}
?>
</body>
</html>
Output
Today is Wednesday
Related Topics
PHP trim() Function
PHP trim() Function The trim() Function in PHP strips the whitespace or other characters from the beginning and the end of the given string. The trim() function strips the following characters: " " - an ordinary space."\t"- a tab."\n"- a new...
1 minute read.
PHP implode() Function
PHP implode() Function The implode() function in PHP joins the specified array elements with a string. Syntax implode ( string $glue, array $pieces ) Parameter glue(optional)- It specifies what to put between the array elements. pieces(required)- This parameter represents...
1 minute read.
PHP strval() function
PHP strval() function The strval() function in PHP is used to get the string value for the specified variable. This function cannot be used on arrays or objects, if applied then would return only the...
1 minute read.
PHP array_udiff_assoc() Function
PHP array_udiff_assoc() Function The array_udiff_assoc () function in PHP computes the difference between the specified arrays with additional index check and compares the data by a callback function. It returns all the values from array1 that are not...
1 minute read.
PHP array_walk() Function
PHP array_walk() Function The array_walk() function in PHP is used to apply the user-defined callback function to each element of the array. This function returns a Boolean TRUE on success or FALSE on failure. Syntax array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] ) Parameter array(required)- This array represents...
1 minute read.
WordPress Theme
Writing our WordPress Theme is relatively simple. Requirements: Before we begin, we need to know about the following set of requirements. We need to have full knowledge of WordPress setup, which is either...
2 minutes read.
How to use Date and Time in PHP
HOW TO USE DATE AND TIME IN PHP: PHP time and date function informs us about current date and time. it can be manipulated also. How time() function works: The time() function...
2 minutes read.
PHP print() Function
PHP print() Function The print() function in PHP is used to output one or more string. Syntax print ( string $arg ) Parameter arg(required)- This parameter represents the input data. It can take one or more arguments. Return This function returns an integer value 1. Example...
1 minute read.
PHP array() function
PHP array() function The array() function in PHP is used to create an array. Syntax array ([ mixed $... ] ) Parameter …(optional)- This parameter represents the elements’ values along with keys of the array. The syntax of this is as follows: "index => values"...
1 minute read.
PHP crypt() Function
PHP crypt() Function The crypt () function in PHP returns a hashed string using DES, Blowfish, or MD5 algorithms. Some constants of crypt() function are as follows: [CRYPT_STD_DES][CRYPT_EXT_DES][CRYPT_MD5][CRYPT_BLOWFISH][CRYPT_SHA_256][CRYPT_SHA_512] etc. Syntax crypt ( string $str [, string $salt ] ) Parameter str(required)- This parameter represents the string to be hashed salt(optional)- This parameter...
2 minutes read.
PHP key_exists() Function
PHP key_exists() Function The key_exists() function in PHP checks if the specified key or index exists in the array or not. Syntax key_exists ( mixed $key , array $array ) Parameter key(required)- This parameter represents the value to check. array(required)- This parameter signifies an input array with...
1 minute read.
PHP ucwords() Function
PHP ucwords() Function The ucwords() function in PHP converts the first character of each word in the specified string to uppercase. It is a binary-safe function. The related function are as follows: ucfirst()lcfirst()strtoupper()strtolower() Syntax ucwords ( string $str [, string $delimiters ] ) Parameter str(required)- This parameter represents...
1 minute read.
PHP similar_text() Function
PHP similar_text() Function The similar_text() function in PHP calculates the similarity between two strings. Syntax similar_text ( string $first , string $second [, float &$percent ] ) Parameter first(required): This parameter represents the first string to be compared. second(required): This parameter represents the second string to be compared. percent(optional): This...
1 minute read.
PHP substr_compare() Function
PHP substr_compare() Function The substr_compare() function in PHP compares two strings from a specified start position. It is a binary-safe and optionally case-sensitive function. Syntax substr_compare ( string $main_str , string $str , int $offset [, int $length [, bool$case_insensitivity]] ) Parameter main_str(required)- This parameter specifies the first string to compare. str(required)- This parameter...
1 minute read.
PHP Array Functions
There are various array functions in PHP. They are used to access and manipulate the elements ofan array. Following are functions of array. Array Function Description array() It is used to create an array. array_change_key_case() It is...
3 minutes read.
PHP list() Function
PHP list() Function The list() function in PHP sorts an array by key while maintaining the keys. Syntax list ( mixed $var1 [, mixed $... ] ) Parameter var1(required)- This parameter accepts variables separated by spaces. …(optional)- This parameter accepts a list of variables separated...
1 minute read.
PHP array_walk_recursive() Function
PHP array_walk_recursive() Function The array_walk_recursive () function in PHP is used to apply a user-defined callback function recursively to each element of the array. This function returns a Boolean TRUE on success or FALSE on failure. Syntax array_walk_recursive ( array &$array , callable $callback [, mixed $userdata = NULL ] ) Parameter array(required)- This array represents the...
1 minute read.
PHP Comment
PHP commentis a line of code that is not executed. It is often used by developer to understand the code. PHP support single and multiple comments. In other language like C,...
1 minute read.
PHP Include Function
PHP Include( ) The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the "include" statement. With the help of include...
3 minutes read.
PHP strip_tags() Function
PHP strip_tags() Function The strip_tags() function in PHP is sued to strip a string from HTML, XML, and PHP tags. It is a binary-safe function. Syntax strip_tags ( string $str [, string $allowable_tags ] ) Parameter str(required)- This parameter specifies the input string. allowable_tags(optional)- This parameter represents...
1 minute read.
