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 8321677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:06:07+00:00 2026-06-08T23:06:07+00:00

Ok what I would like to do is somewhat unique, and I can’t find

  • 0

Ok what I would like to do is somewhat unique, and I can’t find anything online to guide me, so hopefully I do a good job explaining it.

PROBLEM:

I have a series of if statements in PHP. To simplify, each if statement looks like this:

if($product_name == 'shoe') {
     $shoe_description = '<p>Shoe description</p>';
     echo $shoe_description;
}
else if($product_name == 'car') {
     $car_description = '<p>Car description</p>';
     echo $car_description;
}
else if($product_name == 'bottle') {
     $bottle_description = '<p>Bottle description</p>';
     echo $bottle_description;
}

This works fine as long as $product_name is defined as one of my variables. What I would like to do is, as the last statement, say that “if product_name matches none of my variables, show a random description from above“; so I’ll add a final else statement to the end of the above code (example – not working code):

else {
     // Show one of the echo strings from above (i.e. $car_description)
     //but pick randomly
}

QUESTION:

What code would I need to edit/add so that the final else statement picks one of the 3 description variables so that I can then echo it on the page?

  • 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-08T23:06:10+00:00Added an answer on June 8, 2026 at 11:06 pm

    This is a pretty basic rewrite of the logic you gave. You can usually rewrite a series of if statements into simpler logic, or into a switch statement—depending on what you’re trying to accomplish. In this case, I’d check if the $product_name has a description and otherwise just pick a random key.

    $product_name = $_SESSION['product_name'];
    
    $descriptions = array(
        'shoe' => '<p>Shoe description</p>',
        'car' => '<p>Car description</p>',
        'bottle' => '<p>Bottle description</p>',
    );
    
    if(isset($product_name) && isset($descriptions[$product_name])) {
        echo $descriptions[$product_name];
    } else {
        echo $descriptions[array_rand($descriptions)];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can use Eric Martin's simplemodal somewhat. But I would like to call it
I would like to implement an distributed Point-Of-Sale system, somewhat like the one described
Here is the JS Fiddle . I would like the content to be somewhat
Would like to know what a programmer should know to become a good at
I'm somewhat new to jQuery/javascript, and would like to compare the contents of row[i]
I would like to connect two phones together for somewhat of a P2P style
I have a somewhat unique situation that I can't quite get working. I've followed
I would like to think of myself as a somewhat experienced asp.net/c# programmer. I
I have a somewhat simple query in SQL that I would like to convert
I have a little math problem. I would like to have a function with

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.