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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:25:17+00:00 2026-06-08T21:25:17+00:00

How can I make this code shorter? For example with a foreach. if($Email ==

  • 0

How can I make this code shorter? For example with a foreach.

    if($Email == NULL){
        $Email = "-";
    }
    elseif($Age == NULL){
        $Age = "-";
    }
    elseif($Sex == NULL){
        $Sex = "-";
    }

It has to be replaced with like this

$search = array("%UserID%", "%RegDate%", "%Name%", "%Email%", "%Age%", "%Gender%");
$replace = array($UserID, $RegDate, $Name, $Email, $Age, $Sex);
$content = str_replace($search, $replace, $content);

Edit:

I’ve got it like this now is it possible to use the $variable = $row in the ternary code too? Btw I have a variables.php file where I use ternary code to define and I already tried it there but because it was being used earlier it didnt work and I didnt think of it 😛

But this current code works I just wonder if it can be shorter.

while($row = mssql_fetch_assoc($accountinforesult)){
    $UserID = $row['UserID'];
    $RegDate = $row['RegDate'];
    $Name = $row['Name'];
    $Email = $row['Email'];
    $Age = $row['Age'];
    $Sex = $row['Sex'];

    $UserID = isset($UserID) ? $UserID : "-";
    $RegDate = isset($RegDate) ? $RegDate : "-";
    $Name = isset($Name) ? $Name : "-";
    $Email = isset($Email) ? $Email : "-";
    $Age = isset($Age) ? $Age : "-";
    $Sex = isset($Sex) ? $Sex : "-";
}
  • 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-08T21:25:18+00:00Added an answer on June 8, 2026 at 9:25 pm

    Not tested but I believe this should work.

    $vars = array('UserID', 'RegDate', 'Name', 'Email', 'Age', 'Sex');
    foreach ($vars as $k => $v) {
        $$v = ($$v !== NULL) ? $$v : '-';
    }
    

    $$v means “the variable with name $v”. If $v = ‘foo’ then $$v is $foo.

    Look at “variable variables”: http://php.net/manual/en/language.variables.variable.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway I can make this database code any shorter ? It works
Can I make this code shorter than this ? - (void) setupFeature { NSArray
This code repeats itself. How can I make it shorter? Maybe by using anonymous
How can I make this code work? #!/bin/bash ARRAYNAME='FRUITS' FRUITS=( APPLE BANANA ORANGE )
How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,
I can not get to make this comparison in this simple code error ..
How can I make the numbers that I print in this code right adjusted?
how to make php can read log file in /var/log/.... I tried this code
I've had trouble writing this code. I'm supposed to make a function that can
I have this unsafe code that I need to make safe so it can

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.