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 setlocale() Function
PHP setlocale() Function The setlocale() function in PHP sets locale information(language, monetary, time and, other information specific for a geographical area.) Syntax setlocale ( int $category , array $locale ) Parameter category(required)- This parameter specified a named constant specifying the category of the functions affected...
1 minute read.
PHP array_uintersect() Function
PHP array_uintersect() Function The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are...
2 minutes read.
PHP array_map() Function
PHP array_map() Function The array_map() function in PHP is used to return an array containing the results of applying the user-defined callback function over the elements of one or more arrays. Syntax array_map ( callable $callback , array $array1 [, array $... ] ) Parameter callable(required)- This parameter...
1 minute read.
PHP Operator
PHP Operator is used to perform operations on variable and values. Following are the types of Operator: PHP Arithmetic Operator PHP Arithmetic operator is used for arithmetical operations, such as addition, subtraction, multiplication...
2 minutes read.
PHP floatval() Function
PHP floatval() Function The floatval() function in PHP is used to give the float value of a variable. Syntax floatval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that is to be converted. Return This function...
1 minute read.
PHP Continue
What is continue in PHP? Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which...
3 minutes read.
PHP array_reverse() Function
PHP array_reverse() Function The array_reverse() function in PHP returns a new array with the elements in the reversed order. Syntax array_reverse (array $array [, bool $preserve_keys = FALSE ] ) Parameter array(required)- This parameter represents the input array preserve_keys (optional)- This parameter specifies if the function should preserve the keys...
1 minute read.
PHP parse_str() Function
PHP parse_str() Function The parse_str() function in PHP parses a query string into variables. Syntax parse_str ( string $encoded_string [, array &$result ] ) Parameter encoded_string(required)- This parameter represents the input string. result(optional)- This parameter specifies the name of an...
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 Indexed Array
In PHP, arrays are linear data structures that allow to store multiple elements of the same type under a single variable which helps in saving the time and effort of...
3 minutes 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 array_flip() function
PHP array_flip() function The array_flip() function in PHP exchanges all keys with their associated values in an array and returns an array in flip order. If the specified value has several occurrences, the latest key will be...
1 minute read.
PHP array_fill() function
PHP array_fill() function The array_fill() function in PHP fills an user-defined array with num entries of the value that of specified values parameter, where the keys start at the start_index parameter. Syntax array_fill ( int $start_index , int $num , mixed $value ) Parameter start_index(required)- This parameter represents the first index of the...
1 minute read.
PHP join() Function
PHP join() Function The join() function of PHP returns a string from the elements of an array. This function is an alias of the implode() function. Syntax join(separator,array) Parameter separator(Optional)- Specifies what to put between the array elements. The default...
1 minute read.
PHP vfprintf() Function
PHP vfprintf() Function The vfprintf() function in PHP writes a formatted string to a specified output stream. Syntax vfprintf ( resource $handle , string $format , array $args ) Parameter handle(required)- This parameter represents where to write the string. format(required)- This parameter specifies the string and how to format the...
3 minutes read.
PHP is_string() Function
PHP is_string() Function The is_ string() function in PHP finds whether a variable is a string or not. It returns a Boolean value TRUE if the parameter var is string, else it returns FALSE. Syntax is_string ( mixed $var ) Parameter var(required)- This parameter represents...
1 minute read.
PHP strnatcasecmp() Function
PHP strnatcasecmp() Function The strnatcasecmp() function in PHP compares two strings using a "natural" algorithm. It is a case-insensitive function. Syntax strnatcasecmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less...
1 minute read.
PHP soundex() Function
PHP soundex() Function The soundex() function in PHP Calculates the soundex key(four character long alphanumeric string that represent English pronunciation of a word) of a string. Syntax soundex ( string $str ) Parameter str(required) – This parameter represents the input string. Return This function returns the soundex...
1 minute read.
PHP Switch Statement with Example
PHP switch statement The PHP switch statement is used to execute one condition from many conditions.It similar to if statements on the same expression. Syntax: switch(n) { case1: statement if n=case1; break; case2: statement if n=case2; break; case3: ...
3 minutes read.
PHP str_repeat() Function
PHP str_repeat() Function The str_repeat() function in PHP repeats a string a specified number of times. Syntax str_repeat ( string $input , int $multiplier ) Parameter string(required)- This parameter signifies the string to be repeated. multiplier(required)- This parameter represents the number of times the string will be...
1 minute read.
