If else in a php function

Asked
Active3 hr before
Viewed126 times

9 Answers

function
90%

Your message has been sent to W3Schools.,switch statement - selects one of many blocks of code to be executed,We just launchedW3Schools videos,In PHP we have the following conditional statements:

$a = 50;
$b = 10; >
{
   echo "Hello World";
}
88%

return true works, thanks. But the problem of the function repeating itself inside the if statement is present here too. It says "Hello WorldHello World" in the browser. – Bruce Feb 14 '10 at 2:05 , 1 Thanks, this works. Just one thing though, the function is repeated again inside the if statement, so there is "Hello WorldHello World" displayed in the browser. Can this be done without the function repeating itself inside the if statement. – Bruce Feb 14 '10 at 2:01 , this is an incorrect solution to the question asking to check the result of a function in order to process further parsing – Luceos May 22 '14 at 11:16 , 1 Welcome to Stack Overflow! While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Ferrybig Nov 18 '16 at 10:34

if (sayHello() === FALSE)
   echo "Function Failed";
else
   echo "Function Worked";
load more v
72%

There may be several elseifs within the same if statement. The first elseif expression (if any) that evaluates to true would be executed. In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior. ,Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error. , The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to false, and the current elseif expression evaluated to true. , elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

elseif

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

if

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

else

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

else

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

if

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

if

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

else

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

elseif
load more v
65%

PHP if-else statement is executed whether condition is true or false.,PHP if statement allows conditional execution of code. It is executed if condition is true.,If-else statement is slightly different from if statement. It executes one block of code if the specified condition is true and another block of code if the condition is false.,If statement is used to executes the block of code exist inside the if statement only if the specified condition is true.

12 is less than 100
load more v
75%

Using the ternary operator the same code could be written in a more compact way:,Tip: Code written using the ternary operator can be hard to read. However, it provides a great way to write compact if-else statements.,The ternary operator provides a shorthand way of writing the if...else statements. The ternary operator is represented by the question mark (?) symbol and it takes three operands: a condition to check, a result for true, and a result for false.,Using the null coalescing operator the same code could be written as:

< ? php
$d = date("D");
if ($d == "Fri") {
   echo "Have a nice weekend!";
} ?
>
load more v
40%

else: Else function works when the if, Extra if conditions fail in the program/code in PHP/any other Programming Language mostly.,Given below are the examples of if else Statement in PHP:,if else Statement in PHP,if: If function works when the conditions inside of the function are true else compiler will go to else condition if extra if the condition is not there at first.

if(condition statement){
Programming code statements to run only if the IF condition is TRUE
}
if(condition statement){
Another code to be executed with an extra IF condition. (Multiple iF statements can be ignored to if we don’t want)
}
else{
Code Here will run only if the IF condition becomes FALSE
}

if (condition statement) {
   Programming code statements to run only
   if the IF condition is TRUE
}
if (condition statement) {
   Another code to be executed with an extra IF condition.(Multiple iF statements can be ignored to
      if we don’ t want)
} else {
   Code Here will run only
   if the IF condition becomes FALSE
}
load more v
22%

if-elseif-else statement allows you to execute different code groups based on different conditions.,Conditionals are used to perform different actions on different conditions.,Code group is a group of statements which needed to be executed if the condition is true.,if statement executes the code group inside {}, if the condition inside () returns true.

if Syntax

if (condition) {
   code group
}

PHP If Statement Example

<
? php
$day = date('j'); // day of the month
if ($day < 15) {
   echo 'You are spending the first half of the month';
}

PHP If-Else Statement

if (condition) {
   code to be executed
   if the condition is true
} else {
   code to be executed
   if the condition is false
}

PHP If-Else Statement Example

<
? php
$day = date('j'); // day of the month
if ($day < 15) {
   echo 'You are spending the first half of the month';
} else {
   echo 'You are spending the last half of the month';
}

if-elseif-else statement allows you to execute different code groups based on different conditions.

if (condition 1) {
   code to be executed
   if the condition 1 is true
}
elseif(condition 2) {
   code to be executed
   if the condition 1 is false, but condition 2 is true
} else {
   code to be executed
   if both condition 1 and 2 are false
}

PHP If-Elseif-Else Statement Example

<
? php
$day = date('j'); // day of the month

if ($day >= 21) {
   $quarter = 'last';
} else if ($day >= 14) {
   $quarter = 'third';
} else if ($day >= 7) {
   $quarter = 'second';
} else {
   $quarter = 'first';
}

echo 'You are spending the '.$quarter.
' quarter of the month';

PHP Using Multiple Elseif

<
? php
$randomScore = rand(0, 4); // random score between 0-4
if ($randomScore === 0) {
   echo '0 Points, please try again';
}
elseif($randomScore === 1) {
   echo '1 Point, Try more';
}
elseif($randomScore === 2) {
   echo '2 Points, Nice!';
}
elseif($randomScore === 3) {
   echo '3 Points, One more to reach the best';
}
elseif($randomScore === 4) {
   echo '4 Points, You won!';
}

In many places, you may need to compare the same expression (or variable) with different values. In this case, The switch statement is seriously essential.

switch (expression) {
   case value1:
      code to execute
      if expression = value1
      break;
   case value2:
      code to execute
      if expression = value2
      break;
      ...
   default:
      code to execute
      if expression is not equal to any value above
}

PHP Switch Example

<
? php
$randomScore = rand(0, 4); // random score between 0-4
switch ($randomScore) {
   case 0:
      echo '0 Points, please try again';
      break;
   case 1:
      echo '1 Point, Try more';
      break;
   case 2:
      echo '2 Points, Nice!';
      break;
   case 3:
      echo '3 Points, One more to reach the best';
      break;
   case 4:
      echo '4 Points, You won!';
      break;
}

PHP AND Example

<
? php
$a = true;
$b = true;
if ($a and $b) {
   echo 'Both $a and $b are true';
}

$a = false;
$b = true;
if ($a and $b) {
   echo 'This is not echoed because $a is false';
}

PHP && Example

<
? php
$a = true;
$b = true;
if ($a && $b) {
   echo 'Both $a and $b are true';
}

$a = false;
$b = true;
if ($a && $b) {
   echo 'This is not echoed because $a is false';
}

PHP OR Example

<
? php
$a = true;
$b = false;

if ($a or $b) {
   echo 'Either $a or $b (or both) is true';
}

PHP || Example

<
? php
$a = true;
$b = false;

if ($a || $b) {
   echo 'Either $a or $b (or both) is true';
}

PHP XOR Example

<
? php
$a = true;
$b = false;

if ($a xor $b) {
   echo 'Either $a or $b is true. But not both';
}

$a = true;
$b = true;

if ($a xor $b) {
   echo 'This is not echoed because both $a and $b are true';
}

PHP ! (NOT) Example

<
? php
$a = false;
if (!$a) {
   echo '$a is false. So, !$a is returns true';
}

$a = true;
if (!$a) {
   echo 'This is not echoed because $a is true';
}

PHP Nested If Example

<?php
$a = 10;
if ($a >= 0) {
	echo 'Positive Number <br>';
	if ($a % 10 === 0) {
		echo 'The number is a multiple of 10';
	}
} else {
	echo 'Negative Number. Please enter a positive one';
}

load more v
60%

Using if else, elseif and if statements lets you set certain conditions in which a specific block of code will run.,4. Overall Usage of Conditional Statements,PHP if else, elseif and if are statements used to declare conditions under which certain code must be executed.,Conditional statements work by checking whether the conditions we specified are true or false.

< ? php
$time = date("H");
if ($time < "20") {
   echo "Good day, bud!";
} ?
>
load more v
48%

The if statement execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false. For this purpose else is used.,elseif is a combination of if and else. It extends an if statement to execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false. The following example display 'x is greater than y', 'x is equal to y' or 'x is smaller than y' depends on the value of $x or $y. ,Previous: Incrementing Decrementing Operators Next: while statement,if else elseif statement

Syntax:

if (condition)
   execute statement(s) if condition is true;
else
   execute statement(s) if condition is false;

Example :

< ? php
$overtime = 60;
if ($overtime <= 50) {
   $pay_amt = 1200;
   $medical = 1000;
   echo "Pay Amount : $pay_amt : Medical : $medical";
} else {
   $pay_amt = 2000;
   $medical = 1500;
   echo "Pay Amount : $pay_amt : Medical : $medical";
} ?
>

Example :

< ? php
if ($x > $y) {
   echo "x is bigger than y";
}
elseif($x == $y) {
   echo "x is equal to y";
}
else {
   echo "x is smaller than y";
} ?
>

Syntax:

if (condition)

   execute statement(s) if condition is true;
else
   execute statement(s) if condition is false;

Example:

< ? php
$overtime = 60;
if ($overtime <= 50) {
   $pay_amt = 1200;
   $medical = 1000;
   echo "Pay Amount : $pay_amt : Medical : $medical";
} else {
   $pay_amt = 2000;
   $medical = 1500;
   echo "Pay Amount : $pay_amt : Medical : $medical";
} ?
>

Example :

< ? php
if ($x > $y) {
   echo "x is bigger than y";
}
elseif($x == $y) {
   echo "x is equal to y";
}
else {
   echo "x is smaller than y";
} ?
>
load more v

Other "function-undefined" queries related to "If else in a php function"