What's wrong with this? It's not working out
// define
$prv=0;
if(isset($_GET['prv'])) {
$prv = intval($_GET['prv']);
}
// security
if($prv != 0 OR $prv != 2) {
die("<p>Error</p>");
}
This always goes through the die() part, even when prv is undefined or is defined as 2 in the url (and is 2)
And this does works:
// security
if($prv == 0 OR $prv == 2) { } else {
die("<p>Error</p>");
}
Change your if statement to:
// security
if($prv != 0 AND $prv != 2) {
die("<p>Error</p>");
}
With the OR it will always evaluate as true because when $prv == 0 it does not equal 2. Makes sense?
Whatever the value of "prv" is, it certainly cannot be both 0 and 2 at the same time, so one of the conditions is always true; that's why you get in that clause.
Break it down:
if $prv is not 0, the first condition passes and the OR succeeds
otherwise, $pev must be 0. Therefore it must be "not 2", so the OR succeeds.
Try && instead.
== replace !=
if($prv != 0 AND $prv != 2) {
die("<p>Error</p>");
}
Related
I was writing in PHP and making so the page was declared by get method. For an example if it were index.php?page=home it would take home and compare to other strings and... ya include the home BUT the compression in the if statement dosnt work... I have also write that if get method == start then dont show the side news but it still showed it!
Here's my code:
if(isset($_GET['page']) && $_GET['page'] == 'start'){
$adbool = false;
}else{
include('inc/main.php');
}
And here is the if statement for deleting side news:
if(isset($adbool) && !$adbool == false){
include('inc/ad.php');
}
and remember this !$adbool == false is not equal to this $adbool != false
You better understand this part as well !$adbool == false;
!$adbool means that $adbool is equal to false;
so !$adbool == false; means if false=false hence this condition always set to to true.
Thats why your logic is failing
Change this
if(isset($adbool) && !$adbool == false){
include('inc/ad.php');
}
To this
if($adbool == TRUE){//simply check weather its set to to true or not
include('inc/ad.php');
}
use
if(isset($adbool) && $adbool){
include('inc/ad.php');
}
this will check if $adbool is set, and if it's set to true.
I want to check the GET variables are not empty, I tried ways but they didn't work.
So I had the code like this:
$u = isset($_GET["u"]);
$p = isset($_GET["p"]);
if ($u !== "" && $p !== "") {
//something
} else {
//do something
}
The I checked the code by sending create.php?u=&p=, but the code didn't work. It kept running the //do something part. The I tried:
echo $u;
echo $p;
It returned 1 and 1. Then I changed it to:
if ($u !== 1 && $p !== 1 && $u !== "" && $p !== "") {
//something
} else {
//do something
}
But it continued to run //do something.
Please help.
You can just use empty which is a PHP function. It will automatically check if it exists and whether there is any data in it:
if(empty($var))
{
// This variable is either not set or has nothing in it.
}
In your case, as you want to check AGAINST it being empty you can use:
if (!empty($u) && !empty($p))
{
// You can continue...
}
Edit: Additionally the comparison !== will check for not equal to AND of the same type. While in this case GET/POST data are strings, so the use is correct (comparing to an empty string), be careful when using this. The normal PHP comparison for not equal to is !=.
Additional Edit: Actually, (amusingly) it is. Had you used a != to do the comparison, it would have worked. As the == and != operators perform a loose comparison, false == "" returns true - hence your if statement code of ($u != "" && $p != "") would have worked the way you expected.
<?php
$var1=false;
$var2="";
$var3=0;
echo ($var1!=$var2)? "Not Equal" : "Equal";
echo ($var1!==$var2)? "Not Equal" : "Equal";
echo ($var1!=$var3)? "Not Equal" : "Equal";
echo ($var1!==$var3)? "Not Equal" : "Equal";
print_r($var1);
print_r($var2);
?>
// Output: Equal
// Output: Not Equal
// Output: Equal
// Output: Not Equal
Final edit: Change your condition in your if statement to:
if ($u != "" && $p != "")
It will work as you expected, it won't be the best way of doing it (nor the shortest) but it will work the way you intended.
Really the Final Edit:
Consider the following:
$u = isset($_GET["u"]); // Assuming GET is set, $u == TRUE
$p = isset($_GET["p"]); // Assuming GET is not set, $p == FALSE
Strict Comparisons:
if ($u !== "")
// (TRUE !== "" - is not met. Strict Comparison used - As expected)
if ($p !== "")
// (FALSE !== "" - is not met. Strict Comparison used - Not as expected)
While the Loose Comparisons:
if ($u != "")
// (TRUE != "" - is not met. Loose Comparison used - As expected)
if ($p != "")
// (FALSE != "" - is met. Loose Comparison used)
You need !empty()
if (!empty($_GET["p"]) && !empty($_GET["u"])) {
//something
} else {
//do something
}
Helpful Link
if ($u !== 1 && $p !== 1 && $u !== "" && $p !== "")
why are you using "!==" and not "!=".
to always simplify your problem solve the logic on paper once using the runtime $u and $p value.
To check if $_GET value is blank or not you can use 2 methods.
since $_GET is an array you can use if(count($_GET)) if you have only u and p to check or check all incoming $_GET parameters.
empty function #Fluffeh referred to.
if($_GET['u']!=""&&$_GET['p']!="")
Hope it helps thx
In you code you should correctly check the variable existence like
if ($u != NULL && $p != NULL && $u != 0 && $p != 0) {
//something
} else {
//do something
}
Wow! I was so dumb... isset returns a boolean. I fixed my problem now. Thank you for answering anyway :)
This fixes:
$u = $_GET["u"];
$p = $_GET["p"];
I have simple code which is something like this:
$options = new Options();
$page = new Pages();
if($page->page_limit() <= $options->pageno) {
$page->userid = $user_details->userid;
$page->date_of_pub = $_POST['date_of_pub'];
$resultss=$page->page_create();
}
else {
$resultss=false;
}
Then at bottom I am putting a condition
if(isset($resultss) && isset($resultss) == true) {
echo $alert->SuccessMsg("Page created successfully!");
}
if(isset($resultss) && isset($resultss) == false) {
echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");
}
Instead of printing error value even I have set the value of $result = false is shows success message, means its showing $resultss = true statement.
Suggest something. This is so strange. I got the answer thank you so much :)
One more thing.
can you please tell me how can I get rid of this " echo $alert->ErrorMsg" this is so annoying for all the class and functions. I want to make it a single word.
You check the same twice:
isset($resultss) && isset($resultss)==true
You should do:
isset($ressults) && $ressults == true
You have a problem in your logic with isset(). This:
if(isset($resultss) && isset($resultss)==true){echo $alert->SuccessMsg("Page created successfully!");}
if(isset($resultss) && isset($resultss)==false){echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");}
Should be
if(isset($resultss) && $resultss == true){echo $alert->SuccessMsg("Page created successfully!");}
if(isset($resultss) && $resultss ===false){echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");}
In your existing code, the second isset() in each statement is incorrect. In the first one, it is redundant, and you are asking the same thing as if(isset($resultss) && isset($resultss)), which is always true. In the second one, isset($resultss) && isset($resultss)==false could never be true. It's like true && false.
You really don't need to check if the variables are set, since you are setting it in both branches of the if/else. Just do:
if ($resultss) {
echo $alert->SuccessMsg("Page created successfully!");
} else {
echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");
}
Change your IF conditions to:
if(isset($resultss) && $resultss == true)
isset(x) and isset(x) == true bacially mean the same. What you want is to match two conditions:
the variable IS SET and IT IS EQUAL TO TRUE.
Since I'm not sure what the value might be I'd suggest using these confitions:
if(isset($resultss) && $resultss !== false) //the value is set and it is NOT set to false
AND
if(isset($resultss) && $resultss === false) //the value is set to false
Have you tried setting it to false as default?
$options = new Options();
$page = new Pages();
$resultss = false;
if($page->page_limit() <= $options->pageno){
$page->userid = $user_details->userid;
$page->date_of_pub = $_POST['date_of_pub'];
$resultss = $page->page_create();
}
I am having trouble with some if statements. To cut a long story short, when a certain statement is true, the code stops and doesn't move on to the next loop/series of if statements.
Here is the first statement that once reached, doesn't move on.
else if ((($pteam_score[$i] == $popposition_score[$i]) && ($pteam_score[$i] != 0) && ($popposition_score[$i] != 0))) {
$team_points[$i]-=2;
$opposition_points[$i]-=2;
$team_win[$i]-=0;
$team_draw[$i]-=1;
$team_loss[$i]-=0;
$team_extra[$i]-=0;
$opp_win[$i]-=0;
$opp_draw[$i]-=1;
$opp_loss[$i]-=0;
$opp_extra[$i]-=0;
$played[$i]-=1;
echo "hey";
$query9=$database->query("UPDATE results_a SET team_name='$team[$i]', team_score='$pteam_score[$i]',
opposition_score='$popposition_score[$i]', opposition_name='$opposition[$i]' where fixture_id='$fixture_id'");
}
And here is the second. Bare in mind there are about 20 of these ifs/else ifs/ and when any of the others are hit, the points are decucted (as they are with the two bad loops also) butit seems it doesn't then move on to a separate section of the code. Perhaps coincidentally, both bad loops involve a draw result!
else if ((($pteam_score[$i] == $popposition_score[$i]) && ($pteam_score[$i] != 0) && ($popposition_score[$i] != 0))) {
$team_points[$i]-=2;
$opposition_points[$i]-=2;
$team_win[$i]-=0;
$team_draw[$i]-=1;
$team_loss[$i]-=0;
$team_extra[$i]-=0;
$opp_win[$i]-=0;
$opp_draw[$i]-=1;
$opp_loss[$i]-=0;
$opp_extra[$i]-=0;
$played[$i]-=1;
echo "what?";
$query9=$database->query("UPDATE results_a SET team_name='$team[$i]', team_score='$pteam_score[$i]',
opposition_score='$popposition_score[$i]', opposition_name='$opposition[$i]' where fixture_id='$fixture_id'");
}
Can anybody see any reason the code would stop dead with the above two statements? The echo stuff is just to see which option is hit. As I said, these two loops are hit at the correct times, but they are the only two that seem to stop the process.
EDIT - A SECTION THAT WORKS FINE
if (($team_score[$i] == $pteam_score[$i]) && ($opposition_score[$i] == $popposition_score[$i])) {
$team_points[$i]+=0;
$opposition_points[$i]+=0;
$team_win[$i]+=0;
$team_draw[$i]+=0;
$team_loss[$i]+=0;
$team_extra[$i]+=0;
$opp_win[$i]+=0;
$opp_draw[$i]+=0;
$opp_loss[$i]+=0;
$opp_extra[$i]+=0;
$played[$i]+=0;
echo "0";
$query11=$database->query("UPDATE results_a SET team_name='$team[$i]', team_score='$pteam_score[$i]',
opposition_score='$popposition_score[$i]', opposition_name='$opposition[$i]' where fixture_id='$fixture_id'");
}
else if (($pteam_score[$i] == 0) && ($popposition_score[$i] == 0)) {
$team_points[$i]+=0;
$opposition_points[$i]+=0;
$team_win[$i]+=0;
$team_draw[$i]+=0;
$team_loss[$i]+=0;
$team_extra[$i]+=0;
$opp_win[$i]+=0;
$opp_draw[$i]+=0;
$opp_loss[$i]+=0;
$opp_extra[$i]+=0;
$played[$i]+=0;
echo "bla";
}
if (($pteam_score[$i] != $team_score[$i]) && ($popposition_score[$i] == $opposition_score[$i])) {
if (($pteam_score[$i] > $popposition_score[$i]) && ($pteam_bonus[$i] > $popposition_score[$i])) {
$team_points[$i]-=3;
$opposition_points[$i]-=0;
$team_win[$i]-=1;
$team_draw[$i]-=0;
$team_loss[$i]-=0;
$team_extra[$i]-=0;
$opp_win[$i]-=0;
$opp_draw[$i]-=0;
$opp_loss[$i]-=1;
$opp_extra[$i]-=0;
$played[$i]-=1;
echo "6";
$query5=$database->query("UPDATE results_a SET team_name='$team[$i]', team_score='$pteam_score[$i]',
opposition_score='$popposition_score[$i]', opposition_name='$opposition[$i]' where fixture_id='$fixture_id'");
}
Thanks in advance
I am new to PHP and find it very hard to explain.
I have a PHP navigation script with 5 categories and two variables relevant to my question:
$catname = 'used-cars'; // the same value for all categories
$currentpage; // pages 1 to 5
index.php of my site has $currentpage == '1'
The issue is that I need a logic that will say:
If $catname IS NOT 'used-cars', do something, BUT, IF $currentpage is equal to 1, even if $catname is 'used-cats' do it anyway
I am thinking of something like this:
if($catname != 'used-cars' && !($currentpage > '1')):
endif;
Hope you can help!
This is merely a single or condition. On the right side, $currentpage === 1 will evaluate to TRUE without regard to the value of $catname. If either part of the condition is TRUE, you'll enter the if () block to execute your code there.
if ($catname !== "used-cars" || $currentpage === 1) {
// do your thing
}
This is just:
if (strcmp($catname, 'used-cars') != 0 || $currentpage == 1)
(Careful with the string comparison.)
Alternatively, you could declare it as a boolean first:
$proceed = false;
if($catname != 'used-cars')
$proceed = true;
if($currentpage == 1)
$proceed = true;
if($proceed){
// whatever you want
}
$doflag = 0;
if($catname != 'used-cars')
{
$doflag = 1;
} else if($currentpage == 1) {
$doflag = 1;
}
if($doflag == 1) {
//do something
}
Basically instead of trying to do everything with the block, use the block to set a flag and use the flag to do something.