Hi,

I got this long version code:

    if(ISSET($_GET['limit']))
    {
        $limit = intval($_GET['limit']);
    }
    else
    {
        $limit = intval(1);
    }

I can shorten it, like this and it works:

$limit = ISSET($_GET['limit'])?intval($_GET['limit']):1;

Now add an echo and try shortening it. And I get error.

I got this long code with echo in it:

    if(ISSET($_GET['limit']))
    {
        $limit = intval($_GET['limit']);
        echo "<option value=\"$limit\">$limit</option>";
    }
    else
    {
        $limit = intval(1);
    }

Now, how do I shorten it ?
These 2 fail! Show error:

1

$limit = ISSET($_GET['limit'])?intval($_GET['limit']);echo "<option value=\"$limit\">$limit</option>";:1;

2

$limit = (ISSET($_GET['limit'])?intval($_GET['limit']);echo "<option value=\"$limit\">$limit</option>";:1)

@rproffitt

How to fix the bug without your whip here ? Lol!

The shorthand of using ? and : is for assigning a value to a single variable in a simple and concise way.

Not everything is meant to be written in shorthand. When you have multiple things you want to do, echo something in a if-block but not in the else block, etc., then that’s what code blocks are for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.