if else statement, not outputting else if? - php

I have a small if else statement, where the if gets output, but not the else currently? I tried double pipe, but no luck.
I have a variable set at the top of my page -
$portfolio_style = ( isset( $uno_theme['portfolio-style-select'] ) ? $uno_theme['portfolio-style-select'] : null );
And then my if else which will not work -
<?php if ( $portfolio_style == 'portfolio_style_one' || 'portfolio_style_two' ) { ?>
<div class="grid">
<?php } elseif ( $portfolio_style == 'portfolio_style_three' ) { ?>
<div class="wfull">
<?php } ?>
I want it where if portfolio style one or two has been selected, show class 'grid', else if style three has been selected show class 'wfull'
Could I also refine this piece of code at all? -
$portfolio_style = ( isset( $uno_theme['portfolio-style-select'] ) ? $uno_theme['portfolio-style-select'] : null );
Many thanks

You cannot omit $portfolio_style variable after || because otherwise the sole string 'portfolio_style_two' will always evaluate as true as stated in the manual describing conversion to boolean:
Your code should be this:
<?php if ( $portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two' ) { ?>
<div class="grid">
<?php } elseif ( $portfolio_style == 'portfolio_style_three' ) { ?>
<div class="wfull">
<?php } ?>

Try to change the if condition:
<?php if ( $portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two' ) { ?>

you need to change this line as
if ( $portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two' )

Try this:
echo ($portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two') ? '<div class="grid">' : (($portfolio_style == 'portfolio_style_three' ) ? '<div class="wfull">' : '');

Related

Checking Multiple $_GET variables if equals a string

if(isset($_GET['a']) || isset($_GET['b']) || isset($_GET['c'])){
if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x"){
echo "YES";
} else {
echo "NO";
}
}
in this php code, i'm trying to check if one of those requests isset and if one of them value == 'x' or not, But the 2nd part if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x") doesn't work as intended at all, I wrapped it inside () hoping it would work, In this condition, do i have to separate it as i did inthe isset() part? or is there a better method to do that?
This is likely what you are looking for
UPDATE - I just changed || to && for the last condition in case you were quick to try it out.
if( (isset($_GET['a']) && $_GET['a'] == "x") || (isset($_GET['b']) && $_GET['b'] == "x") || (isset($_GET['c']) && $_GET['c'] == "x")){
echo "YES";
} else {
echo "NO";
}
If you have to write a lot of conditionals you could use one of the following:
Using a foreach and a conditional:
$either_abc_is_x = function() {
$keys = ['a','b','c'];
foreach($keys as $key)
if(isset($_GET[$key]) && $_GET[$key] == "x")
return true;
return false;
};
echo $either_abc_is_x() ? 'YES' : 'NO';
Using a an array filter with a conditional:
$get_abc_keys_equal_to_x = array_filter(['a','b','c'], function($v) {
return isset($_GET[$v]) && $_GET[$v] == 'x';
});
echo $get_abc_keys_equal_to_x ? 'YES' : 'NO';
Array gymnastics:
$either_abc_is_x = isset($_GET) && in_array('x', array_intersect_key($_GET, array_flip(['a','b','c'])));
echo $either_abc_is_x ? 'YES' : 'NO';

PHP: Echoing HTML When Multiple Checkboxes Options Are Checked

I've edited this question, because I felt I needed to be clearer in my request.
The code below relates to options checked in a checkbox. It will echo 'both values' when they are both checked, and it will echo 'only valueA', I cannot get it to echo 'only valueB' which instead prints blankly with no echo. Thoughts?
<?php
if (( $a == "General" ) && ( $b == "Specialist" )) {
echo '<h2>both values are printed in HTML</h2>';
}
?>
<?php
if (( $a == "General" ) && ( $b != "Specialist" )){
echo '<h2>only valueA is printed in HTML</h2>';
}
?>
<?php
if (( $a != "General" ) && ( $b == "Specialist" )) {
echo '<h2>only valueB is printed in HTML</h2>';
}
?>
If you want to check whether a value is in array you should use in_array function. Your code should look like this:
<?php
$CheckBox = $form_data['field'][1]
$a = ( "valueA");
$b = ( "valueB");
$isAChecked = in_array($a, $CheckBox[1]);
$isBChecked = in_array($b, $CheckBox[1]);
?>
<?php
if ( $isAChecked && $isBChecked ) {
echo '<h2>both values are printed in HTML</h2>';
} else if ( $isAChecked ) {
echo '<h2>only valueA is printed in HTML</h2>';
} else if ( $isBChecked ) {
echo '<h2>only valueB is printed in HTML</h2>';
} else {
echo '<h2>nothing printed</h2>'
}
?>

Shortest way to code this php multiple if condition

I am trying to do a query like:
If $_GET['page'] == 'items' AND $_GET['action'] == 'new' OR 'edit'
Here's what I have:
if (isset($_GET['page']) && $_GET['page'] == 'items') {
if (isset($_GET['action']) && $_GET['action'] == 'new' || isset($_GET['action']) && $_GET['action'] == 'edit') {
// This is what Im looking for
}
}
Is this correct, and is this the easiest way to make this query?
You could have done it like this as well:
if (isset($_GET['page']) && $_GET['page'] == 'items') {
if (isset($_GET['action']) && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {
}
}
Your way is perfectly fine, although I would almost be tempted to do it the following way. The only reason I suggest this is that your code requires that both action and page are set. If action is not set then there isn't much point checking if the page is == 'items'.
if(isset($_GET['page']) && isset($_GET['action'])) {
if($_GET['page'] == 'items' && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {
//do code here
}
}
You may also try in_array like:
if (isset($_GET['page']) && $_GET['page'] == 'items')
{
if ( !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'new', 'edit' ) )
{
// This is what Im looking for
}
}
That is one of possible solutions
if ( #$_GET['page'] == 'items' && in_array(#$_GET['action'], array('new','edit')))
Everything is ok, but also you can use function:
function paramIs($param, $values) {
$result = false;
foreach ((array)$values as $value) {
$result = $result || isset($_GET[$param]) && $_GET[$param] == $value;
}
return $result;
}
Usage:
if (paramIs('page', 'items') && paramIs('action', array('new', 'edit')))
{
// your code here
}
It will reduce the number of repetitions in your code and encapsulate logic in one place

If conditional with or

Here is my script:
$user_data = user_data($name);
if($user_data['type'] == 'clerk') {
} elseif ($user_data['type'] == 'admin' ){
}else
{ header("Location: login_admin1.php");}
}
If I am using an if statement with blank {} it works but as soon as I use this script it's not working.
if($user_data['type'] != 'clerk' || $user_data['type'] != 'admin')
{ header("Location: login_admin1.php");}
I want to check if $user_data['type'] is clerk or admin, (only clerk or admin has access to page) can check page. If it's not one of them they can't access the page.
Why not use in_array()?
if ( ! in_array( $user_data['type'], array( "clerk", "admin" ) ) ) {
// Is neither Clerk, nor Admin.
}
Cuts down on having to write $user_data['type'] over and over.
Use && instead of || in if statement:
Try:
if( $user_data['type'] != 'clerk' && $user_data['type'] != 'admin' ) {
header("Location: login_admin1.php");
}

Newbie question: Why is my PHP OR operator not working?

I have a function where I want to display something if the value equals A, C or D.
The problem is if the value is set at B or E, the function is display data in the A.C.D. set in addition to the B or E set.
What am I doing wrong?
$linkp = opensky_featured_image_position();
if ( $linkp == 'featuredimg-1' || 'featuredimg-3' || 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
if ( $linkp == 'featuredimg-2' ) {
echo '<h1>L - '. opensky_featured_image_position() .'</h1>';
}
if ( $linkp == 'featuredimg-5' ) {
echo '<h1>N - '. opensky_featured_image_position() .'</h1>';
}
if ( $linkp == 'featuredimg-1' || 'featuredimg-3' || 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
Should be
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
If all the values of $linkkp are defined you could do something like this
$linkArr = array(
'featuredimg-1' => 'F',
'featuredimg-2' => 'L',
'featuredimg-3' => 'F',
'featuredimg-4' => 'F',
'featuredimg-5' => 'N',
);
$linkp = opensky_featured_image_position();
echo '<h1>'.$linkArr[$linkp].' - '. opensky_featured_image_position() .'</h1>';
It's all about operator precedence. Since == is with higher precedence, your code is rewritten as follows:
if ( ($linkp == 'featuredimg-1') || 'featuredimg-3' || 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
which turns to be
if ( false || true || true ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
Since when you cast a non-empty string to bool, it will be evaluated as true.
You have to rewrite it:
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
Which is equalent to:
if ( ($linkp == 'featuredimg-1') || ($linkp == 'featuredimg-3') || ($linkp == 'featuredimg-4') ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
But when you are dealing with such condition, maybe it's better to use in_array
For me this looks like that you're making too much use of if to describe something very simple: Certain values map to characters, that is essentially static data. No need to use so much verbose code to write that data into your code.
Consider using arrays instead to get the character you're actually looking for based on the number and then just echo it afterwards:
$linkp = opensky_featured_image_position();
sscanf($linkp, 'featuredimg-%d', $number);
$chars = 'FLN';
$map = array(1 => 0, 2 => 1, 3 => 0, 4 => 0, 5 => 2);
echo '<h1>', $chars[$map[$number]], ' - ', $linkp, '</h1>';
Demo
It should be:
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
..
'featuredimg-3' will always be evaluated as true, you have to try $linkp == 'whatever' in each or subcondition.
so :
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
You'll need to specify the == operator for each condition in the or clause, otherwise, the string values such as 'featuredimg-3' will be evaluated on their own, and any non-empty string evaluates to true.
Also, when the choices are mutually exclusive, it is better to use elseif so that PHP can skip the remaining if statements when it matches one.
I've also added a final else, so you can see how you can do something in case none of the values match.
$linkp = opensky_featured_image_position();
if ( $linkp == 'featuredimg-1' ||
$linkp == 'featuredimg-3' ||
$linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
} elseif ( $linkp == 'featuredimg-2' ) {
echo '<h1>L - '. opensky_featured_image_position() .'</h1>';
} elseif ( $linkp == 'featuredimg-5' ) {
echo '<h1>N - '. opensky_featured_image_position() .'</h1>';
} else {
echo '<h1>Invalid or missing image position</h1>';
}
You may also want to take a look at the switch/case structure.
Using arrays as others have suggested does allow for brevity and arguably readability. If you do use arrays, you may still need to check to ensure that you find a value in the array and handle the case if you don't.

Categories