1

Is it possible to write the if syntax somewhere close to this:

if (function($value)) else {echo "Error Message";}

If function($value), nothing needs to happen. The function returns true, but if not, I need the Error Message and I cant put that Inside the function.

Any idea how to do this?

0

7 Answers 7

8

You do not need to include a case at all if nothing needs to happen. Thus, what you are looking for is:

if (!function($value)) {
    echo "Error Message";
}
Sign up to request clarification or add additional context in comments.

4 Comments

I think I got too caught up in the single line syntax. +1 for a nice simple answer.
You can still write this on a single line, as if (!function($value)) echo "Error Message";, or similarly if (!function($value)) { echo "Error Message"; }.
Yes lol, I just didn't think of the simplest answer of using ! in the check :)
@Fluffeh you can still do what you oroginally wanted, see my answer ;)
3

This is a better approach:

if (!function($value)){echo "Error Message";}

Comments

2

You can actually do this by inserting an empty statement:

if (function($value)) ; else {echo "Error Message";}

Comments

2
if (!function($value)) {
    echo "Error Message";
}

Comments

1

You could use a ternary to do this:

echo (function($value)? "" : "Error Message";

1 Comment

+1 This is a definite candidate for Ternary over normal IF ELSE
0
return (function($value)? true : "Error Message");

returns true on valid else will return a string, which can be echoed outside.

Comments

0

The Best way:

if (function($value)==0)
{
echo "Error Message";
}else{

//Execution code
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.