Testing a POST variable?

I have this

echo "<table>"; 
foreach ($_POST as $key => $value) {
    echo "<tr>";
    echo "<td>";
    echo $key;
    echo "</td>";
    echo "<td>";
    echo $value;
    echo "</td>";
    echo "</tr>";
 }
echo "</table>";

if(($_SESSION['type'] == 0) || ($_POST['email'] == 1)) {
echo "yes"
}

which produces


why do I not see “yes”, isnt 1 of those statements correct?

Try this and let us know the results:

 var_dump( $_POST['email'] );

Edit
Try also these three lines at the top of the page:

declare(strict_types=1); // if and only if you are using PHP 7
error_reporting( -1 );
ini_set( 'display_errors', 'true' ); 

1 Like

Out of interest, in this section of code:

if(($_SESSION['type'] == 0) || ($_POST['email'] == 1)) {
echo "yes"
}

why isn’t there a parse error thrown for no closing semi-colon? Is that because the very next thing is a close-curly-brace?

2 Likes

Good question

Considering how much time it took me to settle into a consistent coding style, I am no longer always so sure about what is required and what is optional.

I always end statements with a semi-colon. I always indent nested code. etc.

But statements do not need to end with a semi-colon, IF the statement is the only statement in a statement group.

Maybe others are more attentive to the presence or absence of a semi-colon if they later decide to add another statement to the group. For me it is easier to just always end a statement with one.

1 Like

There is. Tested on php 5.6 and 7 - the semi-colon is mandatory. I suspect the OP didn’t copy and paste the code exactly as he is using it - otherwise the semi-colon would have been there. I think there might also be another typo like this - because there is no possibility for this code not to echo ‘yes’. If all this leads to nowhere then it might be useful to see the result of serialize($_POST) so that we can reproduce the case on the same set of data.

2 Likes

Thanks for correcting. I should know better then to not test for myself.

I’ve not seen it because I always terminate statements with a semi-colon.

But indeed, purposely omitting it in PHP 7
Parse error: syntax error, unexpected '}', expecting ',' or ';'

1 Like

The semi-colon can be omitted after the last statement before a ?> closing tag, which helps avoid character noise when php is used as a template language where short php sections are frequent. If there is no ?> after a stament then the semi-colon is mandatory.

3 Likes

ok, its working now, thanks

So, was it the missing semi-colon, or something else?

it was the missing ;

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.