Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I wrote a little check to display succes messages whenever a form is validated succesfully. However the check I use doesn't work. it doesn't matter what $_GET['info'] is, it just shows all messages unless $_GET['info] is empty. so even when info = loggin it shows all three messages. any help?
if(!empty($_GET['info'])){
if($_GET['info'] == "succes-paid"){
echo "<p class='succes'>De bestelling/inschrijving is succesvol verlopen.</p>";
}
if($_GET['info'] == "succes-register"){
echo "<p class='succes'>U werd succesvol geregistreerd.</p>";
}
if($_GET['info'] == "login"){
echo "<p class='succes'>U werd succesvol ingelogd.</p>";
}
}
You have a semi-colon after each of your if statements. The semi-colon means "finish the statement". Therefore your program thinks that you are done with the if and everything in your braces is treated as a block separate from your if statement. That is why those blocks are always executing.
It's because of the semicolon right after the condition of the if.
That line is actually two statements. The first is the if with condition, followed by an 'empty' statement, ended by a semi-colon. The second is a compound statement (a block of statements within a set of { .. }. The compound statement doesn't have a condition at all, so it is always executed.
if($_GET['info'] == "succes-paid") ; {echo 'x'}
condition with no statement -----^ ^ ^--------^-- compound statement without condition
\_The semi-colon that separates them.
And you have this situation three times, hence three lines of output.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Anyone know how the get the following code to work?
Thanks!
if(isset($_COOKIE['first_name'] == "invisible" )) {
echo 'YES!';
}
else {
echo 'YES!';
}
You should have gotten the following error message with what you posted for code: (if your system was already set to catch errors). See my footnote.
Parse error: syntax error, unexpected '==' (T_IS_EQUAL), expecting ',' or ')' in...
Plus, you're using 2x YES!, so one of them has to be "NO!", being the latter.
You need to check if the cookie is set AND equals to.
if(isset($_COOKIE['first_name']) && $_COOKIE['first_name']== "invisible" ) {
echo 'YES!';
}
else {
echo 'NO!';
}
Your original code's brackets were malformed and were the cause of the parse error:
if(isset($_COOKIE['first_name'] == "invisible" ))
^ missing ^ misplaced
Had you placed the bracket in its right spot, and for example:
$_COOKIE['first_name'] = "John";
if(isset($_COOKIE['first_name']) == "invisible" ){
echo 'YES!';
}
else {
echo 'NO!';
}
It would have echo'd "YES" (for being set), since the cookie is indeed set, however it would have been a false positive to check if it equaled to "invisible".
So, you must use two seperate conditions.
Footnote:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Yes I know it. Just Change it like this
if (isset($_COOKIE['first_name'])) {
if ($_COOKIE['first_name'] == "invisible") {
echo 'YES!';
}
else {
echo 'YES!';
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to use an IF AND statement in PHP to test for
The submit button being pressed and
That my $testemail varable is equal to one, ie valid email in a PHP script.
If the Email is good , I get:
Thank you, 1 // ( this is correct) and email is sent
If the email is bad I get:
Thank you, 0. // ( this is not correct ) and email is still trying to be sent with bad email address.
Because the script is outputting the 0 or 1 properly, I am certain it has tested the email and correctly deciding if it is good or bad ( hence the '0' and the '1') but the script never goes to the else statement. I can't seem to get the IF AND statement right. I have tried "1" and '1'.
Very confused. Thanks in advance.
if(!filter_var($emailtmp, FILTER_VALIDATE_EMAIL))
{
$testemail = "0";
}
else
{
$testemail = "1";
}
global $testemail;
var_dump($testemail);
if (isset($_POST['submit']) && ($testemail = 1) )
{
//Everything is good, proceed
sendEmail(preg_replace(array('/<td>.*href="\?remove.*<\/td>/i', '/<th.*
<\/th>/i'), '', $_SESSION['SHOPPING_CART_HTML']));
$result_message = "Thank You. $testemail ";
}
elseif (isset($_POST['submit']))
{
//Missing Required Information
$result_message = "You must use a Valid email addresse, $testemail ";
}
($testemail = 1)
should be
($testemail === 1)
Having just ($testemail = 1) simply sets the value of testemail to 1 (which for the purposes of the if, is true since its successful). == or better yet the more exact === is what you want for checking if the left & right values match.
The problem is you are assigning the value 1 to $testemail instead of comparing it.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
It's a basic question, but I've googled all sorts of variations of it & nothing that quite answers me. I want to display one of two images. One if the criteria is met & one if it is not met. This code works. If criteria is met, it does display the image. But there is no alternative
<? if(stripslashes($getfeedbackQryRow['CompanyID'])=='344'){?><img src='img1.png' alt='Todays Bite'><? }?>
Then I put this together, but it's still wrong
<?PHP
if ($getfeedbackQryRow['CompanyID']) == '344') {
print ("<IMG SRC =/img1.png>");
}
else {
print ("<IMG SRC =img2.png>");
}
?>
This one looks like it should be right, but it throws an error... It's probably a syntax issue. Does anyone know what I'm doing wrong?
You have an extra ")"
if ($getfeedbackQryRow['CompanyID']) == '344') {
Remove the ")" here ['CompanyID'])
if ($getfeedbackQryRow['CompanyID']) == '344') {
// ^------- this is causing your error
You should do something simple like this:
$image_name = $getfeedbackQryRow['CompanyId'] == '344' ? 'img1.png' : 'img2.png';
echo '<img src="' . $image_name . '">';
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a little problem with "short if" in Php.
isset($this->sets[$this->value])?$this->sets[$this->value]:$this->value
this drops notices. It looks the $this->sets[$this->value] runs even when it doesnt exists. If I do:
if (!isset($this->sets[$this->value]))
{
$this->sets[$this->value] = '';
}
it does solve the problem, but then I dont understand something....
EDIT: I refactored:
if (isset($this->sets[$this->value]))
{
$value = $this->sets[$this->value];
}
else
{
$value = $this->value;
}
return $value;
and it works, dunno why....
return 'something'.isset($this->sets[$this->value])?$this->sets[$this->value]:$this->value;
'something'.isset($this->sets[$this->value]) always evaluates to true. You'll need to group the ternary operator expression:
return 'something' . (isset($this->sets[$this->value]) ? $this->sets[$this->value] : $this->value);
And that's why you always post a complete example in your question, not just a subset!
It is not true that both operands are evaluated. Try this to see:
true?print('1'):print('2');
Only the '1' prints.
The issue is that your first line of code does not do anything in and of itself. You don't assign the result of the expression to anything, you don't use it anywhere, I would not be suprised if zend just discards it.
In your second example, you explicitly create an array element if it does not already exist. If you wanted to do the same thing with the ternary operator, you could do
isset($this->sets[$this->value])?null:($this->sets[$this->value]='');
I do not know why you would want to, but it would achieve the same thing as you did in your second example.
Your refactored example can be accomplished using the ternary operator as:
return isset($this->sets[$this->value])?$this->sets[$this->value]:$this->value;
This is a typical usage of the ternary operator
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
$press=$_GET['sell'];
echo $press; //OUTPUT IS : SELL
if($press == SELL)
{
header("Location: home.php");
}
You forgot a single/double quotation
if($press=='SELL')
on the other hand, you must not send any output such as echo before header. So, do not echo anything before header.
You forgot single quote
if($press == 'SELL')
{
header("Location: home.php");
}
You have to put SELL in quotes like 'SELL'
You must not output anything before you send any headers
Do like this.. Enclose the SELL under single quotes.
This works if your URL is in the format like .. http://yourwebsite.com/yourfile.php?sell=SELL
<?php
$press=$_GET['sell'];
if($press == 'SELL')
{
header("Location: home.php");
}
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
remove
echo $press; OUTPUT IS : SELL