1

I am using php to build my website. but there is many if condition in my php code and if condition isset than i want to show some div in html.

<?php if(isset($checkbox1)){ ?>
<div class="btn_simple hidden-xs category align_center col-xs-12 col-md-2 col-md-pull-10  ">
    <div class="side-header">Filter</div>
    <form method="GET">
        <div>
            <strong><?php echo $checkbox1 ?></strong> <input style="width: 50px;" name="<?php echo $checkbox1 ?>" type="checkbox"><
            br>
        </div>
        <div class="col-md-offset-1"> <strong> <?php echo $checkbox1?></strong> <input style="width: 50px;margin-right: -6px;" name="<?php echo $checkbox2 ?>" type="checkbox"><br> </div>
        <input style="margin-right:-27px;" type="submit" name="submit" value="submit"><br>
        <?php if(isset($_GET['submit']) ){ ?> <a style="margin-right: -27px;width:64px;" class="btn btn_all_images" href="?<?php echo $s_value_1 ?>=&<?php echo $s_value_2 ?>=&submit=submit">All</a>
        <?php } ?>
    </form>
</div>
<?php } ?>

In this code I have to write:

  <?php if(isset($checkbox1)){ ?>
  <?php } ?>

Is there any short way to do it ?

Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
mukeshsoni
  • 483
  • 8
  • 29

2 Answers2

2

If you are using PHP7, you can use Null coalescing operator

$myVar = $myVar ?? "default value";
// shorthand of $myVar = isset($myVar) ? $myVar : "default value"
Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
  • Use php short hand ?>. – Adnan Haider Feb 23 '18 at 05:04
  • *[PHP also allows for short open tag (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option)](http://php.net/manual/en/language.basic-syntax.phptags.php)* – Aniket Sahrawat Feb 23 '18 at 05:07
1

Looks like you are in way of plain old PHP.

In this case, theres no way to make it short.

Try to use MVC framework like Laravel.

jesuisgenial
  • 685
  • 1
  • 5
  • 15