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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:33:19+00:00 2026-05-22T23:33:19+00:00

Witch one of this approaches you think is better ? This example is in

  • 0

Witch one of this approaches you think is better ?
This example is in PHP but the rules of the game are the same for other languages.
1. First make a single if block and duplicate the template string in the else block:

if($var['b'])
{
    $text='<tr>
        <td style="cursor:pointer; color:black;"></td>
        <td class="one">'.$var['b'].'</td>
        <td class="two">'.$var['b'].'/'.$var['a'].'</td>
        <td class="other">'.($var['a']/$var['b']).'</td>
        </tr>';
} else {
    $text='<tr>
        <td style="color:red;"></td>
        <td class="one"></td>
        <td class="two">'.$var['a'].'</td>
        <td class="other">0</td>
        </tr>';
}

2. Second check the same variable over and over again but don’t duplicate the template string:

$text='<tr>
    <td style="'.(($var['b'])?'cursor:pointer; color:black;':'color:red;').'"></td>
    <td class="one">'.(($var['b'])?$var['b']:'').'</td>
    <td class="two">'.(($var['b'])?$var['b'].'/'.$var['a']:$var['a']).'</td>
    <td class="other">'.(($var['b'])?($var['a']/$var['b']):'0').'</td>
    </tr>';

3. Third a single variable check, don’t duplicate the template string, set / unset variables:

$css = 'color:red;';
$one = '';
$two = $var['a'];
$other = 0;
if($var['b'])
{
    $css = 'cursor:pointer; color:black;';
    $one = $var['b'];
    $two = $var['b'].'/'.$var['a'];
    $other = ($var['a']/$var['b']);
}
$text='<tr>
    <td style="'.$CSS.'"></td>
    <td class="one">'.$one.'</td>
    <td class="two">'.$two.'</td>
    <td class="other">'.$other.'</td>
    </tr>';
unset($css,$one,$two,$other);


This is a simple example, so doesn’t matter, but imagine a big project where something like this can be used ten to hundred of times just for a single script instance. In the first we have a bigger source code, but we gain speed, in the second our code is small (allmost hard to read), but we loose some speed beacause of multiple variable checks, the third is easy to read, don’t use much space, but we loose some speed and memory beacause of setting and unseting of variables.

  • 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-05-22T23:33:20+00:00Added an answer on May 22, 2026 at 11:33 pm

    I have learn one thing related to this question browsing SO in the latest months:

    Readability of the code is important, so doesn’t mather what approach do you use, as long as your code is easy to read by humans, beacause modern compilers do their job very well in optimizing your code in the way it run as faster as it can.

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

Sidebar

Related Questions

I have been tearing my hair with this one for a while. index.php: $(document).ready(function(){
I have a number of elements with the same class like this: <html> <head><title>example</title>
Im struggling with this one. I have a list of items and on a
I need help with one ajax function This is raw page setup. Page will
I really have problem with this one. So I have a jar with a
No luck at MSDN with this one, which is driving me nuts. I us
I've been struggling with this one for a couple of days now. My current
I'm really frustrated with this one. A few weeks ago I got it working
I have been playing with this one for a few days now, and keep
I need your help with this one again. I want to send special characters

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.