How can I add a condition inside a php object? - php

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

Related

PHP if statement, does not validate, only first if runs

I have 3 stages to my if statement.
If is front page return true.
If is not front page but object is set and has certain value return true.
If not return false.
It worked as two seperate statement,
1) if is front page return true if not false
2) if object is set and has given value return true, objects is not set type return false.
When I try to put them as one statement I get true if front page but this rest doesn't return.
Code is below. Is their anything wrong with the statement? Thanks
<?php
$object = get_object();
if(is_front())
{
return 'true';
}
elseif (!(is_front() & !empty($object))) //is not front but object has been set check value
{
confirm_value();
}
else (!(is_front() & empty($object))) //if not front and object is not set
{
return 'false';
}
function confirm_value() {
$value = load($object); //load object
if($value->id($id)) //check value of id
{
return 'true';
}
else
{
return 'false';
}
}
?>
working code below
<?php
$object = get_object();
if(is_front())
{
return 'true';
}
elseif (!(is_front() && !empty($object))) //is not front but object has been set check value
{
return confirm_value();
}
else
{
return 'false';
}
function confirm_value() {
$value = load($object); //load object
if($value->id($id)) //check value of id
{
return 'true';
}
else
{
return 'false';
}
}
?>
Two problems I see:
1 - you are using a single & when you should be using &&.
2
confirm_value();
should be
return confirm_value();
I don't know the aim of code above but:
Use return confirm_value();
Else statement do not have condition. If you want check condition you should continue using elseif
Should using && instead of &. Because of & is bitwise operator. It will same result with && when all conditions are boolean. But when 1
& 2 == false

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 end if statement in php

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 :)

How to make true/false statements

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.

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

Categories