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 7 years ago.
Improve this question
I am a beginner in php , and i want make a test with GET .
I want display , for example "ok" on my php page if my id parameter is 1 but i have always 1 when i change the id parameter to another value .
Details :
when i make this url :
http://localhost:81/test/testajax.php?id=2
expected result :
not ok
obtained result :
ok
testajax.php
<?php
if($_GET["id"] = 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
One equal sign (=) sets the value of a variable. $foo = "bar"; would set $foo to store bar.
You want to use two equal signs (==), which is a comparison operator. ($foo == "bar") would check to see if $foo is equal to bar.
You can check the different types of operators at http://php.net/manual/en/language.operators.php
May be you should go through this basics before you start.
you should put two equal signs to compare.
if($_GET["id"]==1)
correct code:
<?php
if($_GET["id"] == 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
As you are setting the variable, the if statement is always equating to true
Just thought it was worth Noting this as that is the logical reason for your issue
Related
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 2 years ago.
Improve this question
Tried the below code. All I can ever get is "not working".
bp_is_my_profile(); //this is a Boolean.
echo bp_is_my_profile(); // this echo's out either one or nothing depending on true or false.
if ($bp_is_my_profile === "true") { // I've tried putting in numbers one and zero.
echo "Have a good morning!";
} elseif ($bp_is_my_profile === "false") { // I've tried without quotes.
echo "Have a good day!";
} else {
echo "Not working"; //whatever the Boolean is, each's it prints this line of code.
}
Call the function in the if
if (bp_is_my_profile()) {
echo "Have a good morning";
} else {
echo "Have a good day";
}
You shouldn't compare with strings, since the function returns a boolean. And since it returns a boolean, there are only two possibilities, so you don't need three cases.
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 4 years ago.
Improve this question
I am having some problems with my program.
I am creating a script which displays all the properties in the properties table in our database and one row in the table should show available or sold. it should check if the puserId is null or 1 or more
//Execute the Query
$records = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($records)){
echo "<tr>";
echo "<td>".$row['propertyId']."</td>";
echo "<td>".$row['propNum']."</td>";
echo "<td>".$row['AptNum']."</td>";
echo "<td>".$row['street']."</td>";
if (empty(['puserId'])) {
$status = 'Available';
}else {
$status = 'Sold';
}
echo "<td>".$status."</td>";
When i use this it shows that all properties are sold, also the ones wich have a puserId of null. It doesn't give me any errors though.
Anybody knows how I should do this?
Thanks in advance :)
Replace:
if (empty(['puserId'])) {
with:
if (empty($row['puserId'])) {
in order to fix the missing variable $row typo (your actual code checks whether the array ['puserId'] is null, which is obviously false) . If your field is either null or numeric, you can also write that as:
if (is_null($row['puserId'])) {
but my advice is to avoid using empty/null values in table fields related to identifiers. Just give them a default value of 0 to make everything easier. At that point you could write your check as:
if ($row['puserId'] == 0) {
Other than the $row['puserId'] typo, empty doesn't really look useful there. It includes an isset check that is pointless considering you know that column exists. You aren't checking that any of the other columns are set before you use them. There's really no need to check that one either. Just evaluate it directly. null or 0 values will evaluate as false in the if condition..
if ($row['puserId']) {
$status = 'Sold';
} else {
$status = 'Available';
}
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 6 years ago.
Improve this question
Hi my limited understanding of PHP the define() function redefines a variable to another correct?
So in the following function if the pointer to $default=='on' surely the return value would be true? Instead Im still getting 'on' returned. Thanks for forgiving my limited knowledge.
function isseter(&$default,&$reserve=NULL)
{
define('on',true,true);define('off',false,true);
if (isset($default)) return $default;
else return $reserve;
}
define doesn't redefine a variable. It defines a constant.
Basically, you could do something like
define('on', 'It is on');
which defines a constant on that you can use in the rest of your program, like this:
echo on // Output will be "It is on"
Notice that there is no dollar sign in front of the on since it is not a variable
You can modify your program to make it work like you want like this.
function isseter(&$default,&$reserve=NULL)
{
define('on',true,true);
if (isset($default)) return $default == on
else return $reserve;
}
This returns the boolean result of comparing the variable $default to the constant on, which is true. Although, once you define a constant, it is enabled for the rest of your program and not just inside that function. So usually you define constants in the beginning of your program. You can do this:
// Constants
define('on',true,true);
// Functions
function isseter(&$default,&$reserve=NULL)
{
if (isset($default)) return $default == on
else return $reserve;
}
// Rest of program
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 7 years ago.
Improve this question
So I can't get my head around this if statement.
What I am trying to do is get the person logged in from $_SESSION["user_name"] and if it matches one of two possible "admin" users, to give extra functionality. But it's always firing true.
For the example I'm going to have three users: Emma, John and Robert. Emma and John should be allowed the extra "admin functionality", but Robert is not allowed it. So...
My statement is:
if ($_SESSION["user_name"] === "emma" or "john"){
//give extra functionality as well as basic functionality
} else {
//give just basic functionality
But this is always firing true, so when I log in as my test account with the username Robert, he is also getting the extra functionality.
I've tried changing the quotation marks " of $_SESSION to apostrophes ', and also tried changing the operator from === to ==. I even tried = but found the hard way that this was setting my $_SESSION variable to "emma".
What am I doing wrong because I just can't seem to get my head around it?
If it's worth noting, this if statement is contained in a parent if statement that uses colons : and endif rather than brackets {}. The parent if statement is purely there to decide on what functionality to output based a column returned being empty or a user's name in there.
You need to redeclare the full condition:
if ($_SESSION["user_name"] === "emma" or $_SESSION["user_name"] === "john"){
//give extra functionality as well as basic functionality
} else {
...
}
Update With Explanation:
The pseudo code syntax for if statements is:
if ([some condition] [and/or] [another condition]) {
// then do some stuff
}
each of those [condition] statements is evaluated as either "truthy" or "falsey". So your original question could be re-written something like this:
if ([$_SESSION["user_name"] is equal to "emma"] or ["john" is not false, 0, or null]) {
// we will always get in here
}
Since "john" will always be "truthy" (it is not false, null, 0) it will always pass the condition.
The term "john" as an expression is true:
if ( "john" )
will evaluate to true and not throwing any syntax errors.
What you want is:
if ($_SESSION["user_name"] === "emma" || $_SESSION["user_name"] === "john") {
It may help to break this into steps. PHP will evaluate everything to the left of or to see if it's true or false. Then it will evaluate what's on the right of the or.
Turns out, "john" evaluates to true.
You're looking for:
if ($_SESSION["user_name"] === "emma" or $_SESSION["user_name"] === "john") {
Try something like this
if (($_SESSION["user_name"] === "emma") || ($_SESSION["user_name"] === "john"))
{
//do something
}
else
{
//other thing
}
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