if condition not working in the expected manner - php

the current page is getting the variables item and code from query string,so according to my code it should go into any of the first three conditions... but its going into the last else conditon.. while echoing the values $a and $i, i am getting 2 and A-1-1 respectively.
$a=$_GET['code'];
$i=$_GET['item'];
if($a==1 && $i!='')
{
header("location:http//:www.abc.com");
}
else if($a==2 && $i!='')
{
header("location:http://www.xyz.com");
}
else if($a==3 && $i!='')
{
header("location:http://www.xpqr.com");
}
else if($a==1)
{
header("location: http://www.a1bc.com");
}
else if($a==2)
{
header("location:http://www.x1yz.com");
}
else if($a==3)
{
header("location:http://www.x1pqr.com");
}
else
{
echo "ERROR";
}
can someone help me find the issue why the if else not working in the expected manner.

In conditions you are writing
if($a=1 && $i!='') // "=" is assignment operator
it should bt
if($a==1 && $i!='')

you need to use the equality operator: == double equal
if($a==1 && $i!='')
single equal is the assignment operator.

You are using assignment operator instead of a conditional operator.
For the above code the value of a will always be 1 because of the following line of code :
if($a=1 && $i!='')
= is an assignment operator where as == is a conditional operator.
Use == in all your if condition.
Hope this will help.

Use == in all your if condition. And your header codes
header("location: http:www.abc.com");
Should be like this
header("location: http://www.abc.com"); // you are missing '//' in every header

$a=$_GET['code'];
$i=$_GET['item'];
if($a==1 && !$i)
{
header("location:http//:www.abc.com");
}
else if($a==2 && !$i)
{
header("location:http://www.xyz.com");
}
else if($a==3 && !$i)
{
header("location:http://www.xpqr.com");
}
else if($a==1)
{
header("location: http://www.a1bc.com");
}
else if($a==2)
{
header("location:http://www.x1yz.com");
}
else if($a==3)
{
header("location:http://www.x1pqr.com");
}
else
{
echo "ERROR";
}
use !$i it sets to false.

Related

If statement inside another one is false, return to the else of original if statement

Is there any way in PHP to return at else of first statement, if the second statement which is inside of first, is false
if($first == true) {
//other code which is not necessary to mention here!
if($second == true){
// do smth
}
else{
return to the else of $first statement
}
//other code which is not necessary to mention here!
}
else{
//do smth else
}
Yes, there are multiple ways. For starters, just combine both the statements and give another condition:
if ($first == true && $second == true) {
// do smth
} elseif ($first == true && $second == false) {
// else of$first statement
} else {
//do smth else
}
This can be used as a guidance to get an idea to start. But if you can get a real world example, there can be conditions grouped, tailored to your requirement.
While there is no native way to jump to outer elses from an inner else, but you can set a flag for later processing:
$do_else = false;
if($first == true) {
//other code which is not necessary to mention here!
if($second == true){
// do smth
}
else{
$do_else = true;
}
//other code which is not necessary to mention here!
}
else{
$do_else = true;
//do smth else
}
if($do_else){
//do smth else
}
If the answers above doesn t help you in the real situation, you can create a function for execute in 'else' statements to avoid code duplication

Elseif not returning the proper value on PHP

When the user introduces a letter and clicks on submit the script shall return a name that starts with that letter, I wrote it using a Switch/Case structure, now I need to write it using if/elseif/else.
The problem is that no matter what letter I introduce on the Textbox I'll always get the return for A (Aberdefia - Anacleto)
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombradorIf = 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombradorIf = 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
The code for the letters C to Z is just like the one for A and B.
You need == for comparison, = is for assignment, and === is for identical or same type.
Also, you used $nombrador when assigning and $nombradorIf when comparing, resulting in an undefined variable.
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Thus, $nombrador == 'A' means $nombrador is equal to A.
And, $nombrador = 'A' means to assign A to $nombrador.
More information at http://php.net/manual/en/language.operators.comparison.php.
Hope this helps, thanks!
== is the conditional operator = is assigning operator
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Try this code
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} else if($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Your code error near . you condition check variable name is $nombradorIf and also not declare variable but You assign the value variable name is $nombrador and you not use conditional operator == . so result not proper . You use above the code working fine
For checking conditional statement you have to use == (double equal sign) like below:
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombrador == 'A') {
// ^^ Use like this
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
// ^^ Use like this
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}

How do I add an if / else?

I am using the following code to pass a variable. if variable = a, do nothing.
I then want to check if variable = a, do nothing, if b, do nothing, else do something
<?
if($_GET['pageid'] == 'a'){
} else {
include('header_image.php');
}
?>
Above is the code I have working correctly for one vartiable.
How do I add an if / else?
if($_GET['pageid'] != 'a' && $_GET['pageid'] != 'b'){
//do smth
}
This is a comment - i want the formatting...
To do what you want:
if ($_GET['pageid'] == 'a') {
// do nothing for now
}
elseif ($_GET['pageid'] == 'b') {
// do some more nothing...
}
else { // we do something...
include('header_image.php');
}
You could combine the 'do nothing' tests as:
if ( $_GET['pageid'] == 'a'
|| $_GET['pageid'] == 'b') {
// do nothing for now
}
else { // we do something...
include('header_image.php');
}
I agree it reads better than the 'not equal and' tests. However, that is what 'programmers' use so it is worthwhile getting used to it.

PHP form check box issue

I have a simple form that I'm trying to add a checkbox to, I have everything on the form setup correctly but when I try to handle the check box I'm only able to make echos work. I'm trying set whether the box is checked as a yes or no and store that yes/no in a variable, here is what I have in my handle form for the checkbox:
if(isset($_POST['race']) &&
$_POST['race'] == 'Yes')
{
$race1 == "yes";
}
else
{
$race1 == "No";
}
You need to use the single equal sign when assigning values. Double equals does a comparison.
if(isset($_POST['race']) && $_POST['race'] == 'Yes')
{
$race1 = "yes";
}
else
{
$race1 = "No";
}
== is a comparison operator. You need to use attribution operator =
if (isset($_POST['race']) &&
strtolower($_POST['race']) == 'yes')
{
$race1 = 'yes';
}
else
{
$race1 = 'No';
}

Using if and else statements error

I have created two links where I would like the page contents to change. The problem is the URL changes but not the page content.
<h3>Filter Results</h3>
<p><a href="index.php?filter='Action'>Action</a></p>
<p>Comedy</p>
if (isset($_GET['filter']) == 'Action') {
echo 'Action';
}
else if (isset($_GET['filter']) =='Comedy') {
echo 'Comedy';
}
It always outputs the first link information "Action".
Your links are faulty:
<p>Action</p>
<p>Comedy</p>
<!-- ^ ^ No single quotes (' ') -->
Yogesh Suthar pointed it out first
Also, isset() will return a boolean (true or false; based on whether or not the variable is set). You're comparing a boolean to a string, a string will always be converted into TRUE (unless the string is "false" or similar), so basically, if the variable is set, the first condition will always match.
You want
if (isset($_GET["filter"]) && $_GET["filter"] === "Action")
Note the use of ===, this will make sure that the variable is exactly what you think it is, and not some sort of other type variable.
Few more points (Shamelessly stolen taken from other answers)
If there are multiple possible filters, check for the variable existance once, and use a switch/case block to determine which of them it is:
if(isset($_GET['filter'])) {
switch($_GET['filter']) {
case 'Action':
echo 'Action';
break;
case 'Comedy':
echo 'Comedy';
break;
}
}
The function isset will only check if the variable is existing! It will not return its value! Try this instead:
<h3>Filter Results</h3>
<p>Action</p>
<p>Comedy</p>
if(isset($_GET['filter']) && $_GET['filter'] == 'Action'){
echo 'Action';
}
else if(isset($_GET['filter']) && $_GET['filter'] == 'Comedy') {
echo 'Comedy';
}
Also, using switch might make things easier in the future:
<h3>Filter Results</h3>
<p>Action</p>
<p>Comedy</p>
if(isset($_GET['filter'])) {
switch($_GET['filter']) {
case 'Action':
echo 'Action';
break;
case 'Comedy':
echo 'Comedy';
break;
}
}
As #MadaraUchiha said about isset and,
if(isset($_GET['filter']) == 'Action')
should be
if(isset($_GET['filter']) && $_GET['filter'] == 'Action')
Also
<a href="index.php?filter='Action'>Action</a>
^ ^ ^ // here you started " but not ended and remove the single quotes around Action
should be
Action
Make sure that you insert an opening and a closing php tag: <?php and ?> To simplify it a bit you could just echo the value you get via $_GET
<h3>Filter Results</h3>
<p><a href="index.php?filter='Action'>Action</a></p>
<p><a href="index.php?filter='Comedy'>Comedy</a></p>
<?php
if(isset($_GET['filter'])){
echo $_GET['filter'];
}
?>
The function isset will return true or false (it checks whether the variable is set or not). Change your code:
if(isset($_GET['filter']) && $_GET['filter'] == 'Action') {
Your if condition is not correct do it:
if(isset($_GET['filter']) && $_GET['filter'] == 'Action'){
echo 'Action';
}
Similarly with else if:
else if(isset($_GET['filter']) && $_GET['filter'] =='Comedy') {
As you are comparing the isset($_GET['filter']) with the value although isset returns true of false so you need to compare the value of $_GET['filter'].
you dont have to use isset() and then compare.
$filter = $_GET['filter'];
if(isset($filter)){
if($filter == 'Action'){
echo 'Action';
}else if($filter == 'Comedy'){
echo 'Comedy';
}
}
isset returns true and since 'Action' is not null, it evaluates to true.
if ((isset($_GET['filter'])) && ($_GET['filter'] == 'Action')) {
// ...
} else if ((isset($_GET['filter'])) && ($_GET['filter'] == 'Comedy')) {
// ...
}
BTW such code would sooner or later become a nightmare to maintain.
You could instead, for example
function preventDirectoryTraversal($requestParam) {
return preg_replace("/\//", "", $requestParam);
}
// ...
if (isset($_GET['filter'])) {
$filterName = preventDirectoryTraversal($_GET['filter']);
include(FILTERS_DIR . "/" . $filterName . ".php");
}
or something alike. Of course this can be further improved, but I hope you get the point.
Wrong use of isset, check documentation, return a boolean.
if (isset($_GET['filter']))
{
switch ($_GET['filter'])
{
case 'Action':
//TODO
break;
case 'Comedy':
// TODO
break;
default:
// TODO
break;
}
}
//isset will always return true or false
if(isset($_GET['filter'])){
if($_GET['filter']=='Action')
{
echo 'Action';
}elseif($_GET['filter']=='Comedy'){
echo 'Comedy';
}
}

Categories