How to make true/false statements - php

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.

Related

How can I add a condition inside a php object?

I have n php object like this:
$test
->key1(1)
->key2(1)
->key3(1);
Is it possible to add a condition inside it?
like this:
$test
if(true) ->key1(1)
->key2(1)
if(true) ->key3(1);
what you want 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;
}
for example
if ($YOUR_KEY == "key1") {
//if key1 then do something
} elseif ($YOUR_KEY == "key2") {
//if key2 then do something
} else {
//if none condition work than do something else
}
here $YOUR_KEY is your variable and "key1" is your value
check www.w3schools.com and http://php.net/manual for reference

Setting variable value on PHP ternary case

I am trying to use ternary output to do 2 things on the latter case.
The issue I have is setting a text value to the variable in the latter case, after incrementing the error count.
I have tried a few things, here's two attempts, but these both fail on setting the $errors_log value.
Q. How can i set a variable value within an output of ternary.
$errors_v=0;
if (validate_username() == false ? null : $errors_v++ && $errors_log='username invalid');
if ($errors_v != 0) {
echo $errors_log;
}
function validate_username() {
return true;
}
$errors_v=0;
$errors_log[];
if (validate_username() == false ? null : $errors_v++ && $errors_log[]='username invalid');
if ($errors_v != 0) {
var_dump($errors_log);
}
function validate_username() {
return true;
}
I would do my ternary like below, and then check if $errors_log is not empty and if it's not, print out the errors.
$errors_log[] = validate_username() === false ? null : 'username invalid';
if (!empty($errors_log)) {
foreach($errors_log as $error) {
echo $error;
}
}
function validate_username() {
return true;
}
If it's needed to have a counter aswell, even though I really recommend you count on the $errors_log array instead, you could do something like this:
if (!validate_username()) {
$errors_log[] = 'username invalid';
$errors_v++;
}
You are mixing "longhand" and "shorthand" conditional syntax.
Your ambiguous function name coupled with its return value is confusing/unintuitive. I recommend renaming your function or reversing the boolean it returns.
Always endeavor to use DRY and DAMP coding practices.
The approach in the second half of your code looks better than the first.If you are going to generate an array of errors, don't bother with incrementing a counter, just count the array when you wish.
I don't see any need to fancy up your code with shorthand conditionals.
Code: (Demo)
function bad_username(){ // new meaningful function name
return true;
}
$errors_log=[]; // declare the variable as an array
if(bad_username()){
$errors_log[]='username invalid'; // push the value
}
if(sizeof($errors_log)){ // count elements in array, if 1 or more display them
var_export($errors_log);
}else{
echo "No Error";
}
Output:
array (
0 => 'username invalid',
)
You can't perform two operations in a ternary case. You could just check if $errors_log is empty and if it is increment $errors_v inside the if statement like this:
if (validate_username() == false ? null : $errors_log='username invalid');
if (!empty($errors_log)) {
$errors_v++;
echo $errors_log;
}
function validate_username() {
return true;
}

how to use "class_exists" conditional inside if...elseif

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';
}

Nested if statements, any possible way of cleaning?

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.

Why is (If false return "true") ... returning true?

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.

Categories