0

I have this code

<?php

ob_start();
header("Content-Type: text/html; charset=ISO-8859-1");

$campo = $_GET['campo'];
$valor = $_GET['valor'];
$hello;

    if ($campo == "myPassword") {
        if (!preg_match("/^\S{4,12}$/", $valor)) {
            echo "Tamanho entre 4 e 12 letras e sem espaços";
        }
        else 
            $hello = $valor; //problem here
            echo $hello;
    }

if ($campo == "passwordMatch") {
    if ($hello != $valor ) {
        echo "Passwords don't match";
    }
}

?>

so, i need to save a variable (where i put "problem here") and compare below, but this code didn't work and i don't know why

4
  • Define "this code didn't work". Commented Mar 15, 2011 at 19:40
  • care to expand on "didn't work" Commented Mar 15, 2011 at 19:40
  • What kind of problem are you seeing? Is $hello a blank string, or do you get PHP exceptions/errors? Try a var_dump($_GET); to ensure you are receiving data if the problem is that $hello is blank. Commented Mar 15, 2011 at 19:40
  • i need to save the variable $hello to compare above if the pass match with the field above. It is a temporarily save when the user do a new registry. At the moment echo of $hello in the first if show the pass but in the second the echo of $hello is nothing Commented Mar 15, 2011 at 19:47

4 Answers 4

1

I would suggest using to compare strings reference: http://php.net/manual/en/function.strcmp.php

if(strcmp($str1,$str2)):
endif;

or

if(!strcmp($str1,$str2)):
endif;

and replace your code with this

if (!preg_match("/^\S{4,12}$/", $valor)) {
            echo "Tamanho entre 4 e 12 letras e sem espaços";
        }
        else {
            $hello = $valor; //problem here
            echo $hello;
       }
Sign up to request clarification or add additional context in comments.

Comments

1

I had the same problem, turned out to be white space, I used trim($myVar); to fix.

Comments

0

If you're expecting $hello to survive between page loads, you're out of luck.

Instead you'll need to persist the variable between sessions yourself.

BTW, the line $hello; doesn't do anything.

Edit The reason I have assumed that you need sessions is that you set $hello only when $campo == "newPassword", then later expect it to be a certain value only when $campo == "passwordMatch". You never change the value of $campo, and clearly it can't be both. This implies that the two pieces of logic are to run on separate page loads.

2 Comments

what i want is a simple comparison when the user do a new registry.
@Fel: What is a "new registry"? How do you know that your required solution is "simple" when you don't know what it is? Please read up on sessions as advised above, and decide whether you need that in your code. Also, please see my edit.
0

Looks like you have a syntax issue here:

if ($campo == "myPassword") {
        if (!preg_match("/^\S{4,12}$/", $valor)) {
            echo "Tamanho entre 4 e 12 letras e sem espaços";
        } else {  // missing bracket
            $hello = $valor; //problem here
            echo $hello;
            } // missing bracket
    }

Is that your problem? Your ELSE was missing an opening { and closing }.

ALSO

For string comparison, you should use === and not a numerical ==

Details are here:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.