The question is pretty much self-explanatory, I am having trouble how to end if statement in php.
For example,
<?php
if (argument) {
// end if statement
}
else if (different argument) {
// end if statement
}
else if (another different argument) {
// end if statement
}
else {
// do something
}
?>
Consider carefully how an if condition works:
If (boolean condition) Then
(consequent)
Else
(alternative)
End If
When an interpreter finds an If, it expects a boolean condition ...
and evaluates that condition. If the condition is true, the statements
following the then are executed. Otherwise, the execution continues
... After either branch has been executed, control returns to the
point after the end If.
The if statement will end if none of its conditions evaluate to true or if one of its conditions evaluate to true. The statement(s) associated with the first true condition will evaluate, and the if statement will end.
See Wikipedia's If–then(–else) for more.
There should not be a closing brace on line 4, but there should be one at the end of all conditional statements.
The syntax is:
if (condition)
{
code to be executed if this condition is true;
} elseif (condition)
{
code to be executed if this condition is true;
} else
{
code to be executed if all conditions are false;
}
Further reference at:
http://www.w3schools.com/php/php_if_else.asp
Change :
<?php
if (argument) {
// end if statement
}
else if (different argument) {
// end if statement
}
else if (another different argument) {
// end if statement
else {
// do something
}
?>
To :
<?php
if (argument) {
// end if statement
}
else if (different argument) {
// end if statement
}
else if (another different argument) {
// end if statement
}
else {
// do something
}
?>
To use if statement, use the following syntax :
if (condition) {
// Put your codes here
}
Another example, if you use else if :
else if (condition) {
// Put your codes here
}
Use the curly bracket { } to contain your codes
PS: you just missed a } at the third if in your codes :)
Related
i want to continue my for-loop if called function execute exit(); statement.
Note:
Remaining code lines should not be processed when $i=2; so, the main thing i need help to exit my function but not for-loop.
Moreover, i have lot of conditions so i can not use if-else.
i have already tried return; statement but with the return; statement function executes remaining lines of code that's the main issue!.
Any help will be highly appreciated!
For example
for($i=0;$i<=5;$i++){
$this->calculate($i);
}
function calculate($counter){
if($counter==2){
exit(0);
}
echo "My remaining code lines";
}
UPDATED ANSWER
After your comment. We have to let the for loop know that we don't want to continue. Since the logic is separated we can do something like:
for ($i = 0; $i <= 5; $i++) {
if ($this->calculate($i) == false) {
break;
}
}
function calculate($counter) {
if ($counter == 2) {
return false;
}
echo "My remaining code lines";
return true;
}
ORIGINAL ANSWER
Do not exit the function. It halts any further processing.
Simply return:
function calculate($counter){
if($counter==2){
return;
}
Instead of exit use return because exit terminates the execution, whilst return only exists the current function.
And instead of if-else you can use swtich-case, like this:
for($i=0;$i<=5;$i++){
if(!$this->calculate($i))
{
continue;
}
}
function calculate($counter){
switch($counter){
case 2:
return false;
default:
echo "My remaining code lines";
break;
}
}
Edit: I have updated my answer to move the loop to next iteration if the method calculate() returns false.
return statement used.
for(condition){
return();
}
Just use a return instead. It will return to the caller and hence you can continue execution.
for($i=0;$i<=5;$i++){
$this->calculate($i);
}
function calculate($counter){
if($counter==2){
return;
}
echo "My remaining code lines";
}
// use return() function .. exit() stop execution ...
function calculate($counter){
if($counter==2){
return;
}
echo "My remaining code lines";
}
?>
It's not because I want to know how to do this instead of which one is faster..
How would I make a statement true or false ?
I want this:
if $var = true {
do this
} else{
do this
}
I'm aleady using a foreach but I thought something like this:
if($myvar == 'True'){
echo copy from array
} else {
echo none
}
Normally, if your variable contains a boolean, all you have to do is:
if($myVar)
{
//do stuff
}
else
{
//do something else
}
You can also do this if it's clearer to you:
if($myVar == true)
{
//do stuff
}
else
{
//do something else
}
What's important is not to mistake the comparison operator (==) with the assignment (=) otherwise you'll end up with strange results. This happened in your initial statement in which you also forgot the parenthesis.
As for the "else" statement, it is automatically executed when the condition is false, which in this case would mean $myVar is actually false.
I have a chain of if...elseif...else statements likes so:
if (!empty($video_meta)) {
echo 'foo';
} elseif ( WPCOM_Liveblog::is_liveblog_post() ) {
echo 'bar';
} elseif (has_tag('featured')) {
echo 'foobar';
}
The second elseif statement is reliant on a particular WordPress plugin (liveblog) being activated. In the event that the plugin is not active, the function will throw a fatal error because the class WPCOM_Liveblog doesn't exist.
I tried to use a nested IF statement like this
if (has_post_format('video')) {
echo 'foo';
} elseif (class_exists('WPCOM_Liveblog')) {
if ( WPCOM_Liveblog::is_liveblog_post() ) {
echo'bar';
}
} elseif (has_tag('featured')) {
echo 'foobar';
}
If the second condition is false, it never gets to the final elseif for obvious reasons. The class_exists condition always evaluates to true and the chain of if statements terminate.
I cannot use a simple && conditional, since I'm looking for a deactivated plugin, and the second condition will simply throw a fatal error for an unknown class.
I also cannot change the order of the if...elseif statements. There is a purpose behind the order.
This feels like a simple PHP question, but I'm stumped. Is there anyway I can nest another condition in the elseif? Basically, if the first part of the condition is true, execute the second condition. If both true, return true to the elseif. If the class doesn't exist, then the second conditional check never fires.
In PHP your conditions are executed in order from left to right so you can do the following:
elseif (class_exists('WPCOM_Liveblog') && WPCOM_Liveblog::is_liveblog_post()) {
echo'bar';
}
Proof (via ideone): http://ideone.com/fAN2YK
Or, you could move the test that fails out of the loop:
$plugin_loaded = false;
if ( class_exists('WPCOM_Liveblog') ) {
$plugin_loaded = ( WPCOM_Liveblog::is_liveblog_post() ) ? true : false;
}
if (has_post_format('video')) {
echo 'foo';
} elseif ($plugin_loaded ) {
echo 'bar';
} elseif (has_tag('featured')) {
echo 'foobar';
}
I have checked a few other questions but they don't really give me the answer I expect..
My code is a like this..
private function handle()
{
if()
{
if(!condition)
{
if(!condition)
{
if(!condition)
{
if(!condition))
{
if(!condition)
{
if(!condition)
{
if(!condition)
{
if(!condition)
{
if(!condition)
{
code
}
return;
}
return;
}
return;
}
return;
}
return;
}
return;
}
return;
}
return;
}
return;
}
}
In my opinion it is readable but messy, sadly I haven't found really a way of making it look 'pretty'. Any ideas?
EDIT: Each return is different.
EDIT2: Gave an answer of my own, thanks everybody!
Conditions can be merged by a && operator..It works form left to right, which means, as soon as the any one starting from left fails, it stops evaluating the condition..
if($a) {
if($b) {
}
}
can be replaced by
if($a && $b) {
}
Use a variable check, or combine the conditions into fewer IF statements.
Variable check like so:
$execute = TRUE;
// Opposite of what you want, e.g. if you want $a only to be TRUE, do $a !== TRUE
if (condition) {
$execute = FALSE;
}
...
// If all the conditions were met, then everything is OK
if($execute === TRUE) {
// code
}else {
// return
}
Edit:
Variable check can be preferably to combining IF statements if you want more control on what returns, e.g. something specific happens if a certain condition fails, which combining conditions can not always allow for.
Like already posted use
if(condition1&&condition2){}
or if this will not work, you can also use function which stops as soon as a condition is true
function some(){
if(!conditon 1){return 0;}
if(condition 2) {return 1;}
}
this provides more power as second if works only if first doesn't satisfy.
You must choose based on your requirements. Sometimes though nested loops are unavoidable.
I thought it out and have found a nice way of doing it, basically I'll make a method for each basic condition, and I'll call them in an if statement with the bitwise AND operator (&), which don't short-circuit.
/**
* nonsql_condition - It means it doesn't check with the database
*
* sql_condition - It means it does check with the database.
*/
if(!$this->nonsql_condition() & !$this->nonsql_condition() & !$this->nonsql_condition() & !$this->nonsql_condition())
{
if(!$this->sql_condition())
{
return error;
}
if(!$this->sql_condition())
{
return error;
}
code;
}
This allows me to use fewer lines of code in my method, plus also not doing unnecessary queries to my database.
Inside the begining of a function I have this:
if(false);
{
return 'TRUE';
}
it is returning "TRUE"! Obviously my real use was checking a more useful expression and returning something else. I just changed it to this to elaborate my point.
Why is this happening? Can you not put a return inside an if statement? I do this all the time in other languages.
For example
instead of this:
function () {
if(something)
{
//process stuff
}
}
which requires wraping everthing inside the function inside an if.
I prefer to do this:
function() {
if(!something)
return;
//process stuff
}
Is this not OK in PHP... is there a work around?
You're just crazy. :)
if(false); // <----- remove semi colon
{
return 'TRUE';
}
should have one less semi-colon.
if(false)
{
return 'TRUE';
}
You have an extra semicolon after the if condition.