开发者

if else statements php

How do I get this to not display when you first go to the page???

if ($error) {
 echo "Error: $e开发者_Python百科rror<br/>";
}
if ($keycode) {
 echo "Keycode: $keycode<br/>";
}


<?php
session_start();

if ($_SESSION['been_here'] == true) {
     // show what you need to show
}
else {
    // don't show it
    $_SESSION['been_here'] = true;
}
?>

The point here is that $_SESSION-variables "last" (as long as you session_start()). Google "php sessions" for more information, and ask more questions on SO if necessary. :)

Use session_destroy(); to destroy the session.


<?php

if ($error){ echo "Error: $error
"; } if ($keycode) { echo "Keycode: $keycode
"; }


Based on the comments, it seems that your conditional is evaluating to true before you expect it to. Without seeing more of your code, this is only a guess, but I believe your problem is that you're giving the variable $error a default/temporary value when you create it that doesn't mean false. For example:

$error = "default error message, change me later";

// Later...
if ($error) { // This evaluates to true
    echo "Error: $error<br/>";
}

If so, you'll want to check out PHP's documentation on casting to booleans, and maybe use something like this (with contribution from Christian's answer):

$error = "0"; // Default error message, change it later

// Later...
if($_SESSION['been_here'] == true)
    $error = "This is the real error message.";

// Even later...
if ($error) {
    echo "Error: $error<br/>";
}


This probably works for you:

if (isset($error) && !empty($error)) {
     echo "Error: $error<br/>";
}

I cannot say more, because you have not specified what the value of $error might be.

Or you just have to introduce a flag that indicates that an error occurred:

$error = 'Error message.';
$has_error = false;

if(!empty($_POST) && some_condition) { // means it is a POST request
    $has_error = true;
}

if($has_error) {
    echo "Error: $error<br/>";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜