If else not running on Boolean value [closed] - php

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.

Related

elseif echo issue, prints wrong value [closed]

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 have a problem i cant figure out.
From my point of view, this should actually work...
The database is setup with 3 different priority values.
0,1,2 where 0=LOW, 1= MEDIUM, 2=HIGH.
Following code should be able to print the correct information using if and elseif, but for some reason it prints all the rows with "high" as priority, but in the database it has 0 or 1.
$pri = $row["prioritet"];
if($pri = 2) {
$pri = "<span class='badge badge-danger'>High</span>";
} elseif($pri = 1) {
$pri = "<span class='badge badge-warning'>Medium</span>";
} elseif($pri = 0) {
$pri = "<span class='badge badge-success'>Low</span>";
}
You are using = instead of == which means you are assigning the value of $pri to 2 on line 2 instead of comparing them.
The if and elseifs need to use the comparison operators ('==' or '===', etc). By using '=', you are inadvertently trying to assign the value 2 to $pri, for example.

$_GET["id"] don't respond [closed]

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

If a variable is something and another variable is something else do, PHP [closed]

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
I'm doing some PHP traning, and wanted to make my own copy of "Rock, Paper and Scissors" and then I'd like to compare the results from the user and the computer, and I tried with this code:"
if($choice = 'Rock' and $pcSelect == 'Scissors'){
echo "You win!";
}
Plz help me,
Magn0053
You are assigning 'Rock' to $choice with =. You need to test equality with == like in the second conditional.
Use && instead of and in your if
if($choice == 'Rock' && $pcSelect == 'Scissors'){
echo "You win!";
}

preg_match() $pattern issues with format [closed]

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
I'm looking at the preg_match instructions, but struggling to gather together what I need to only allow a certain few characters to be used, and how to implement that into the pattern format
if(preg_match('[0-9.$M]', $_POST['budget']) === true) {
$errors = 'no illegal';
} else {
//Illegal character in budget
$errors = 'illegal';
}
But I've realized that for one that doesn't work for this combination
0123456789$M. as well as that it will not function as I want it to even if I get it to work. as I need to check the $_POST['budget'] and be sure it's in the correct format before moving on with it. It can ONLY contain the characters I've put forward, anything else will create a pretty big mess.
So far, I've had javascript change any entries, but if that is disabled then they can put in whatever they please.
So I need is pseudo
if (preg_match($allowed_characters, $_POST['budget'] === true && DOESNT CONTAIN ANY OTHER CHARACTERS) {
//No illegal characters
} else {
//Illegal characters!!
}
1) preg_match returns INT or BOOL false.
2) Your pattern is not valid for preg_match
should be something like:
if(preg_match('/[^0-9\.\$M]+/', '0123456789$M.')) {
$errors = 'illegal';
} else {
$errors = 'no illegal';
}

I want to get the image cordinates in PHP on clicking the submit button, when I try the below code getting error for isset [closed]

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
<?Php
function imagecheck(){
if(isset($_POST(["x"]) && isset($_POST(["y"])){
echo "X : ".$_POST["x"]."<br />";
echo "Y : ".$_POST["y"]."<br />";
}
else{
echo "Click on the image<br />";
}
}
?>
In the above code, getting error as
"Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead) in c:forms.php"
I am a beginner in PHP, kindly help
if(isset($_POST(["x"]) && isset($_POST(["y"])){
^-----^---
$_POST is a superglobal ARRAY. It is NOT a function.
The code should be
if (isset($_POST['x']) && isset($_POST['y'])) {
Your if statement should be ...
if( isset( $_POST["x"] ) && isset( $_POST["y"] ) )

Categories