• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

IntelyBlog

A blog site that is intelligent

  • Hosting
  • jQuery
  • Programming

if-else

How to write if else statement in a single line (compact if else statement) using PHP

April 5, 2022

Post Views: 1

Normal way of writing an if else statement

<?php

$result = '';

$count = 55;
$target = 70;

if($count >= $target) {
   $result = 'High';
} else {
   $result = 'Low';
}

?>

Now let’s see how the above can be written in a single line.

<?php

$result = '';

$count = 55;
$target = 70;

$result = ($count > $target) ? 'High' : 'Low';

?>

That’s it! Happy coding!

Filed Under: PHP Tagged With: if-else, PHP

Primary Sidebar

Recent Posts

  • How to convert any full URL path to relative to WordPress installation (root)
  • How to write if else statement in a single line (compact if else statement) using PHP
  • How to programmatically add a WordPress admin user using functions.php
  • How to check if a file exists on a remote server using CURL
  • How to create WhatsApp share button in HTML

Recent Comments

No comments to show.

Archives

  • April 2022
  • February 2022
  • September 2021
  • August 2021

Categories

  • Hosting
  • jQuery
  • PHP
  • WhatsApp
  • WordPress

Content

Our team will teach you the art of writing audience-focused content that will help you achieve the success you truly deserve.

Learn more about content.

Copyright © 2021 IntelyBlog

  • html
  • jquery
  • https
  • css
  • slice
  • WhatsApp