PHP doesn't see 2 equal strings - php

So I'm busy on a verification system, when the player of the game puts a sentence in his/her status, in this case: HT-N5I4-S3ZI-GU6A and my script checks if it's the same one as the code in my db. If it is, its proven to be their account. Because I've experienced that people make accounts under fake names.
When I echo the code in my db, it echoes:
HT-N5I4-S3ZI-GU6A
And the one in my test players mission echoes:
HT-N5I4-S3ZI-GU6A
It is exactly the same code, but when I wanna compare them like this:
if($_SESSION['user']['email_activation'] == $functions->getMotto($_SESSION['user']['username']))
{
echo "Works";
}
else
{
echo "Doesnt work";
}
It aways echoes 'doesn\'t work'. Did I use the wrong equalization operator? Or any other mistakes?
Thanks

Try This Code
if($_SESSION['user']['email_activation'] === $functions->getMotto($_SESSION['user']['username']))
{
echo "Works";
}
else
{
echo "Doesnt work";
}
Thanks.

Related

echo isset with condition

When people search for a real estate agent by zip code they will see a message on the site I'm working on that reads: There are x number of our Agents in your neighborhood.
x is the number determined by this php code:
<?php echo isset($total_record) ? $total_record : "";?>
if the number is Zero, the message sounds dumb (There are 0 number of...)
How do I change the message just for those cases with 0 as a search result? so that a different message appears? Something like - Sorry, we don't have any Agent in your immediate area.
Any help, much much appreciated.
Use a simple if statement:
if (isset($total_record) && $total_record > 0){
echo $total_record." number of our Agents in your neighborhood";
} else {
echo "Sorry, we don't have any Agent in your immediate area.";
}
if (isset($total_record))
{
if ($total_record > 0)
{
echo "There are {$total_record} of our Agents in your neighborhood.";
}
else
{
echo "There are no Agents in your neighborhood.";
}
}
Use the empty function rather than isset. The empty function checks if a variable exists and has a value. 0, false, and a few other values are also considered empty, check the manual for a full listing.
echo !empty($total_record) ? 'There are ' . $total_record . ' number of our Agents in your neighborhood.' : 'Sorry, we don\'t have any Agent in your immediate area.';

Using if else statement based off of $_POST value to echo message

Pretty sure this is a quick and easy question but I have a form that on action POST goes to a confirmation page. I need a message to display on the confirmation page if the user selects county1 but if user selects county2, county3, or county4. However, when I setup the statement it's not working. Probably a syntax error or two on my part. Any help would be greatly appreciated.
A messy idea of what I think should work:
<?php $county=$_POST['County'];
if ($county="Polk") {
echo "Important message about your county"; }
else {
echo " "; // Or nothing at all
}
?>
But
<?php echo $_POST['County'] ?>
displays the name of the county so I know the submission is carrying through. Thoughts on why my above code wouldn't be working? If you could flag syntax errors or code placement that'd be much appreciated! Thank you!
Inside the if condition you should use two equal operators instead of one . try this code
<?php
$county = isset($_POST['County'])?$_POST['County']:"";
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
?>
Use double equal in your if statement for comparison
See the answer and read the comment to understand why you have to change it
if ($county="Polk") {
Single equal is assignment operator, it assign value
Change this line to this
if ($county=="Polk") {
So your whole code should look like this
$county=$_POST['County'];
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
Use double equal sign, double equal is comparison operator,
Here you are checking if the $county is equal to Polk or not,
Means you are comparing value

else if - what am I doing wrong?

I have the following code which I use in conjunction with a members script which displays a members username the page or asks guests to login or register.
PHP code:
if ($_SESSION['username'])
{
echo "".$_SESSION['username'].", you are logged in.<br><small>Click here to logout</small>";
}
else
echo "Welcome Guest!<br><small>Login or Register</small>";
It works perfectly well, though now I want to modify it so if a user with admin privileges logs in it identifies the username and offers a link to the admin page.
So here's my modified code:
<? php
$validateadmin = $_SESSION['username'];
if ($validateadmin == "admin1" or $validateadmin == "admin2")
{
echo "Hello $validateadmin, you have admin privileges.<br><small>Click here to logout</small>";
}
else if ($_SESSION['username'])
{
echo "".$_SESSION['username'].", you are logged in.<br><small>Click here to logout</small>";
}
else
{
echo "Welcome Guest!<br><small>Login or Register</small>";
}
?>
Any idea's what I'm doing wrong? It either leaves me with a blank page or errors.
I know it's probably a newbie error but for the life of me I don't know what's wrong.
Generally you should use elseif in php not "else if" because the php parser will interpret else if as else { if { .... }} and you can have some weird errors.
Also, it is a great practice to ALWAYS use braces with control statements to avoid dangling clauses.
Also to avoid notices about array indexes don't do checks like if($array[$index]) if the index may not exist. Use any of array_key_exists, isset, empty, etc (they all are slightly different) to check if an array contains a key you are looking for.
try the following
<?php #removed space
session_start(); #you will need this on all pages otherwise remove it if already called
$validateadmin = $_SESSION['username'];
if($validateadmin == "admin1" || $validateadmin == "admin2"){
echo "Hello $validateadmin, you have admin privileges.<br><small>Click here to logout</small>";
}elseif(isset($_SESSION['username'])){ #you should use isset to make sure some variable is set
echo $_SESSION['username'].", you are logged in.<br><small>Click here to logout</small>";
}else{
echo "Welcome Guest!<br><small>Login or Register</small>";
}
?>

While statement repeats infinitely

I have a website where i need to use a while statement, but when i use it, it repeats the echo infinitely. Although it looks like i could make it work without while, that isnt so, this is a simplified version of a final product that will need while.
<?php
$passlevel = '0';
while ($passlevel == '0')
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$passlevel == '1';
}
else
{
echo "you didn't select purple.";
}
}
else echo "contact webmaster";
}
?>
Why is it echoing either contact webmaster or you didnt select purple an infinite number of times?
First, you probably need to change:
$passlevel == '1';
to
$passlevel = '1';
The first is a comparison equals, not an assignment equals.
Second, if $color is not #800080, then the loop does not terminate and thus repeats forever as nothing in the loop causes the value to change.
I'm not entirely sure of the point of this loop in the first place. It should work perfectly fine without the loop, however you've stated that your code is a simplified version of something more complicated that indeed needs a loop. Perhaps you can elaborate.
You're not providing any way out of the loop. If $_GET['box_1_color'] isn't purple the first time through the loop, it can't possibly become anything else the second time through the loop, so it'll keep being the wrong color each and every time.
I'm not certain what you intended for this loop to accomplish. If you're trying to have the user enter a new value each time, you won't be able to do that with a loop in PHP. You'll have to regenerate the entire page (with an error message, presumably) and ask the visitor to submit the form again.
In the case of "contact webmaster", you need to break out of the loop, either with the break expression or by setting your $passlevel to anything other than zero. A more serious real problem is revealed in #Mike Christensen's answer, though
If $_GET['box_1_color'] is not set, the variable $passlevel will never be changed.
<?php
$passlevel = 0;
while ($passlevel == 0 || $passlevel == 2)
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$passlevel = 1;
}
else
{
echo "you didn't select purple.".'try again.';
}
}
else
{
echo "contact webmaster";
$passlevel = 2;
}
}
?>
You need to define another passlevel for failure, to stop the while loop. Also, don't put any quotes around integers.

programming 101 if/else statement

This is probably the easiest question to answer that you will find on stackoverflow, but I would like to get this confusion out of my head once and for all. Consider the following if statement:
if(x > 0)
{
echo 'Inside if';
}
// apparently there is a hidden else here....
echo 'This comes after if';
And now consider the following one:
if(x > 0)
{
echo 'Inside if';
}
else
{
echo 'Inside else';
}
echo 'This comes after if/else';
In the first example, if the condition evaluates to true, "Inside if" will be printed, but won't what comes after the if ("This comes after if") get printed also? I mean, I don't have return inside my if, so the code should continue normally, right?. Same thing for the second if statement, whatever comes after the statement will get printed because the execution of the code will continue normally. Is there really a virtual else after an if-statement if we don't explicitly define one? I mean, if what comes after my if statement is printed whether the condition evaluates to true or not, then there's not really a virtual else after my if. Also, When is an Else absolutely necessary in an if-then-else statement instead of just relying on the "virtual else" as in the first example? Please shed some light on this.
Thank you
An else is "absolutely necessary" whenever you want to actually do something if the if condition evaluated to false. If you only want to do something in the case where it's true, and absolutely nothing when it's false, you can skip the else part.
There isn't really a hidden else. A conditional statement is a way to branch off the procedural execution of your code temporarily. Once completed, it will continue where it left off unless you do a return from within a function for example.
Simple example of where you need an ELSE:
IF (loadfile == True)
{
println("file loaded...on to processing...");
}
ELSE
{
:: raise an error and stop execution ::
}
:: continue with processing file ::
The difference between the "virtual else" and the else is that the virtual else is always executed, whereas the real else is only conditionally executed. For example, consider that this:
if(x > 0)
{
echo 'Inside if';
}
else
{
echo 'Inside else';
}
echo 'This comes after if/else';
is exactly the same as this:
if(x > 0)
{
echo 'Inside if';
}
if(x <= 0)
{
echo 'Inside else';
}
if(x == x)
{
echo 'This comes after if/else';
}
Your "virtual else" is not really an else at all, it is always executed.

Categories