views:

93

answers:

4

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

if ($error) {
 echo "Error: $error<br/>";
}
if ($keycode) {
 echo "Keycode: $keycode<br/>";
}
+2  A: 
<?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.

Christian Jonassen
session start may not work for me because I already have headers being sent on the page already..
use session_start() before you send output...
Jan Kuboschek
Yes, use session_start(); even before _any whitespace_. This can be a bit annoying until you get used to it, but it has to be that way. :)
Christian Jonassen
A: 
<?php

if ($error){ echo "Error: $error
"; } if ($keycode) { echo "Keycode: $keycode
"; }
Jarrod
What's going on here?
webbiedave
I assumed that his question was "how to not have this text appear". It seems to me that the answer to his question is that PHP isn't parsing his file because there's no <?php tag. Unless I'm missing some clarification somewhere from the original author, most of the answers to this question are completely out of left field.
Jarrod
A: 

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/>";
}
Lord Torgamus
A: 

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/>";
}
Felix Kling