1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

please help; if and else statement

Discussion in 'PHP' started by webgames247, Mar 29, 2013.

  1. #1
    Hi

    This is what i currently have...if user login "you are login"
                <?php if ($userStatus == '1') { ?>
    you are login
                <?php } ?>
    PHP:
    how i write a statement if user not login see something else? for example "you are not log-in"

    Thanks advance
     
    webgames247, Mar 29, 2013 IP
  2. Steve202

    Steve202 Member

    Messages:
    59
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    45
    #2
    This should do the trick:

    <?php if ($userStatus == '1') {
    echo "You are logged in"; }
    else {
    echo "You are not logged in"; } ?>
    PHP:
     
    Steve202, Mar 29, 2013 IP
  3. wiicker95

    wiicker95 Well-Known Member

    Messages:
    438
    Likes Received:
    37
    Best Answers:
    10
    Trophy Points:
    100
    #3
    Opening and closing php at every single line for no good reason makes no sense, at all.

    Steve202's answer is pretty much correct, however, using single quotes when echoing a string is far more natural. This is probabaly how you would want this to look like :

    (assuming we're within <?php ?>)
    if ($userStatus == '1') {
        echo 'You are logged in';
    }
    else {
        echo 'You are not logged in';
    }
    PHP:
    When you work with php files, just write <?php once at the beginning and ?> at the end. There's no valid reason to exit php within a php file.

    You should also work on your grammar. "login" is a noun whereas "log in" is a verb.
     
    wiicker95, Mar 29, 2013 IP
  4. jhine

    jhine Active Member

    Messages:
    25
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    53
    #4
    I tend to use this approach as it looks cleaner.
    <body>
        <?php if ($userStatus == 1): ?>
        <p>You are logged in</p>
        <?php else: ?>
        <p>You are not logged in</p>
        <?php endif; ?>
    </body>
    PHP:
     
    jhine, Mar 29, 2013 IP
  5. wiicker95

    wiicker95 Well-Known Member

    Messages:
    438
    Likes Received:
    37
    Best Answers:
    10
    Trophy Points:
    100
    #5
    What substances are you on??
     
    wiicker95, Mar 29, 2013 IP
  6. jhine

    jhine Active Member

    Messages:
    25
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    53
    #6
    Are you going anywhere with this in particular? Why would I be on any substance?
     
    jhine, Mar 29, 2013 IP
  7. wiicker95

    wiicker95 Well-Known Member

    Messages:
    438
    Likes Received:
    37
    Best Answers:
    10
    Trophy Points:
    100
    #7
    Because no sane person would ever consider it cleaner. As if more code means cleaner... And that alternative syntax is just laughable! It reminds me of BASIC with all the nonsensical columns.
    (that is w/o saying that these paragraphs aren't paragraphs...)
     
    wiicker95, Mar 29, 2013 IP
  8. Skovy

    Skovy Greenhorn

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    23
    #8
    It does make sense if he wanted to style it or something with some CSS or HTML but those tags still could be in the echo statement so I have to agree with wiicker... where does the benefit come in with doing it that way jhine?
     
    Skovy, Mar 29, 2013 IP
  9. webgames247

    webgames247 Well-Known Member

    Messages:
    412
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #9
    thank you all....working perfectly
     
    webgames247, Mar 30, 2013 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    While I'm with wiicker95 on the idiocy of opening and closing PHP on every blasted line multiple times, to the point I still say <?php and ?> should be removed from PHP ENTIRELY! (along with that heredoc asshattery)

    ...but the real laugh is, that's still a massive waste of code if you use inline evals instead.

    echo 'You are ' , $userStatus == '1' ? '' : 'not ' , 'logged in';
    Code (markup):
    Far, FAR simpler. We don't need no steenking IF/ELSE...

    If you REALLY want them separate because this placeholder and there might be different markup, this is functionally identical to the IF/ELSE.

    echo $userStatus == '1' ? 'You are logged in' : 'You are not logged in';
    Code (markup):
    if all you're doing is changing the echo, don't waste time with all that extra code. Though if you need to do other things besides sending content, yeah, then you use if/else.
     
    deathshadow, Mar 30, 2013 IP
    YoGem likes this.
  11. gandalf117

    gandalf117 Active Member

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #11

    please, explain how is using single quotes more natural?
     
    gandalf117, Apr 1, 2013 IP
  12. wiicker95

    wiicker95 Well-Known Member

    Messages:
    438
    Likes Received:
    37
    Best Answers:
    10
    Trophy Points:
    100
    #12
    Because when you use single quotes, you don't have to escape (put a backslash before) double quotes.
    These backslashes waste bytes of data for nothing and make your code unreadable for a human being, thus it's a pain in butt to edit and maintain your file. That, and the fact that single quotes are not intended for markup.
     
    wiicker95, Apr 1, 2013 IP
  13. gandalf117

    gandalf117 Active Member

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #13

    I think that eventually to avoid using escape characters you have to use both single and double quotes. So then the question is which one you should use first rather then which one you should use in general. Example:

    echo "<a href='$link'>my link</a>";

    as opposed to:

    echo "<a href=\"$link\">my link</a>"; -> escape characters
    or
    echo '<a href="$link">my link</a>'; -> doesn't work

    And in simple sentences like echo "You are logged in"; it doesn't seem to matter at all whether you use single or double quotes, does it?
     
    gandalf117, Apr 1, 2013 IP
  14. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #14
    except doubles take longer to parse FOR the functionality you listed, and they can get you in trouble for out of order execution. Much as I think <?php ?> should be stricken from PHP entirely, I also think double quoted strings should just go -- waste of code and overhead server side, and I've had to fix code for people using it more than once.

    But then I've always hated C style printF, as it usually just results in less clear code -- but again I'm a Wirth kind of fellow, who isnt' all that wild about needlessly cryptic code in the first place and prefers to explicitly see everything opened, closed and delimited.

    Particularly when from your examples I'd write it:
    echo '<a href="' , $link , '">My Link</a>';

    making it clearer there's a variable there, and using less memory since the two strings would be passed from the constants table and the variable passed as a reference one after another, instead of having to allocate space on whatever PHP's equivalent to the stack or scratchspace is, parse the string into it at runtime to find the variable, plug in that variable then the rest of the string, and send that new dynamically sized piece of stack space to the output routine, then de-allocate that variable stack instead of de-allocating fixed pointer references.

    After all, that's why we have comma's for echo... see the example I always use to illustrate the difference between comma's and string addition periods.

    function outIs() {
    	echo 'is ';
    }
    
    echo 'this ' , outIs() , 'a test<br />';
    echo 'this ' . outIs() . 'a test<br />';
    Code (markup):
    Which confuses the hell out of people when the output from that is:

    this is a test
    is this a test

    Think about that one, and what it means at runtime. Inlining variables inside double quotes is similarly afflicted in terms of execution order and what it means for speed at bytecode compilation and memory use at runtime.
     
    deathshadow, Apr 1, 2013 IP
  15. wiicker95

    wiicker95 Well-Known Member

    Messages:
    438
    Likes Received:
    37
    Best Answers:
    10
    Trophy Points:
    100
    #15
    That's not how it's supposed to be written.
    echo '<a href="',$link,'">My Link</a>'; -> works

    You usually don't end up echoing a simple sentence anyway.
     
    wiicker95, Apr 1, 2013 IP
  16. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #16
    ... I missed this part -- and yes it does make a minor difference -- single quotes are faster. Perhaps immeasurably so since it would be at the bytecode compilation step and not execution (something people who do benchmarking in PHP often forget you can't benchmark... hmm or can you now that there's $_SERVER['REQUEST_TIME_FLOAT']???).

    Since single quotes don't parse for variables, and parse for less slash escapes, it runs a fraction faster at the bytecode compilation stage.
     
    deathshadow, Apr 1, 2013 IP
  17. gandalf117

    gandalf117 Active Member

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #17
    But your version has single quotes as well, along with double quotes and commas. Would you really write double quotes, single quotes and commas every time instead of just starting the sentence with double quotes and avoiding all of this writing? Besides wasn't the point of starting the sentence with a single quote to avoid "wasting bytes"?

    Yes, you may be right and that fraction is equal to what? 1 billionth or trillionth of a second? Who is ever going to use that many quotes to save just 1 second?

    I know you would disagree but I think it's obvious that yours is not the better style. Sometimes I think you guys are here just for the sake of argument, lol
     
    gandalf117, Apr 1, 2013 IP
  18. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #18
    In code or in memory? They're two different things entirely. Admittedly, people who only think in html/networking terms ONLY seem to think in code... which doesn't apply when choosing code that's going to be bytecode compiled for local execution.
     
    deathshadow, Apr 1, 2013 IP