Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:51:38+00:00 2026-06-12T23:51:38+00:00

I had no idea it was not possible to use if else statement inside

  • 0

I had no idea it was not possible to use if else statement inside an array in php. I’ve searched stackoverflow and found that shorthand version should actually work fine.

I tried it but still have errors and page doesn’t load, my code:

query_posts (array(

                                ($prefooter_order_logic == 'xviews' ? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : "")

                                'order' => 'DESC',
                                'orderby' => $prefooter_order_logic,

                                'posts_per_page' => '10',
                                'post_type' => 'post',
                                'category_name' => $prefooter_category_select

                            ));

This doesn’t give me error:

but it doesn’t work…

($prefooter_order_logic == 'xviews' ? "

                                    'v_sortby' => 'views',
                                    'v_orderby' => 'desc',

                                    " : "

                                    'order' => 'DESC',
                                    'orderby' => $prefooter_order_logic,

                                    "),

I’ve decided to stop playing around and do it easiest way:

btw, any thoughts if this is the best way to do it? or not?

                if ($prefooter_order_logic == 'xviews') {

                    query_posts (array(
                        'v_sortby' => 'views',
                        'v_orderby' => 'desc',
                        'posts_per_page' => '10',
                        'post_type' => 'post',
                        'category_name' => $prefooter_category_select
                    ));

                } else {

                    query_posts (array(
                        'order' => 'DESC',
                        'orderby' => $prefooter_order_logic,
                        'posts_per_page' => '10',
                        'post_type' => 'post',
                        'category_name' => $prefooter_category_select
                    ));

                }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:51:39+00:00Added an answer on June 12, 2026 at 11:51 pm

    That will produce syntax error, unexpected T_CONSTANT_ENCAPSED_STRING because you are missing a comma at the end of this line:

    ($prefooter_order_logic == 'xviews' ? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : ""), 
                                                                                               ^ here
    

    Edit:

    Just modify the array after creating it, based on your condition:

    $arr = array(
    
        'posts_per_page' => '10',
        'post_type' => 'post',
        'category_name' => $prefooter_category_select
    
    );
    
    if($prefooter_order_logic == 'xviews')
    {
        $arr['v_sortby'] = 'views';
        $arr['v_orderby'] = 'desc';
    }
    else
    {
        $arr['order'] = 'DESC';
        $arr['orderby'] = $prefooter_order_logic;
    }
    
    query_posts($arr);
    

    Alternatively, use array_merge():

    $arr = array(
    
        'posts_per_page' => '10',
        'post_type' => 'post',
        'category_name' => $prefooter_category_select
    
    );
    
    $arr = $prefooter_order_logic == 'xviews' ? array_merge($arr, array('v_sortby' => 'views', 'v_orderby' => 'desc')) : array_merge($arr, array('order' => 'desc', 'orderby' => $prefooter_order_logic));
    
    query_posts($arr);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just had an idea, is that possible to protect my java sources (packed
I had an idea that would require me be able to send and receive
I'm interested in learning to use OpenGL and I had the idea of writing
Possible Duplicate: iPhone app without AppStore My boss had an idea to develop an
Earlier today I was discussing with someone that I found out you can use
I created a program that will use a dynamic array by using pointer to
I always had the idea to make a textarea based program, that gets the
I had the idea to write a matrix division in Haskell as Muliply A*
I had an idea for my first mobile application and I was thinking of
I recently had an idea to create my own String class to make using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.