How can display if if x=john or bond results you name is john or bond
<?php
if ($x == "john")
{
?>
you name is john or bond
<?php } ?>
if ($x == "john" || $x == "bond")
{
// do stuff
}
Or if you have many names you can use other structures like switch():
switch ($x)
{
case "john":
case "bond":
// do something
break;
default:
// or else do this
}
You can also use in_array() if you have a dynamic list of names or if you just feel like its more readable:
if (in_array($x, ["john", "bond"]))
{
// do stuff
}
Try this:
<?php
$x="bond";
//$x="john";
if ($x == "john" or $x == "bond") {
echo "Your name is ".$x;
}
?>
Related
I am in the making of some code that needs to check if a users login details are correct, and I therefore need a lot of if-statements inside each other. If any of the conditions in the if-statements are not true, they should alle return the same value. Is there an easy way of doing this, instead of writing the same multiple times? I have made an example below to visualize my problem. As you can see here I write " else { return false; }" multiple time, and this is what I am wondering if you are able to do more efficiently. Maybe so I only have to write "or else return false" once.
//some code
if (/*some condition*/) {
//some code
if (/*some new condition*/) {
//some code
if (/*some new condition*/) {
//some code
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
I am having a hard time finding a good way to explain my problem, so if you have a more elegant way of explaining it, do not hesitate to edit my post. I am also not quite sure that the title is as good as it could be, so if you have any ideas to an alternativ please say so :)
Lets say you have something like that (I added No):
if ( condition1 ) {
//some code 1
if ( condition2 ) {
//some code 2
if ( condition3 ) {
//some code 3
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
Since each time a condition is false, you exit the function returning false, you can directly test if the condition is false using a negation (if the negated condition is true):
if ( !condition1 ) {
return false;
}
//some code 1
if ( !condition2 ) {
return false;
}
//some code 2
if ( !condition3 ) {
return false;
}
//some code 3
This doesn't reduce the number of if statements, but you avoid many nesting levels and the else statements.
You can also try the switch statement. For many situations it will produce cleaner code.
<?php
if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
}
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
?>
The switch statement is also compatible with using strings:
<?php
switch ($i) {
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
}
?>
Good luck! :)
I need to implement 3 'if conditions' in my script, i've looked it up online but I can only find solutions up to 2 if's like below
<?
if (condition 1){
do something;
}
elseif (condition 2){
do something else;
}
else {
do this last;
}
?>
but I would need something like this:
if (condition 1) { do this };
else if (condition 2) {do that};
or else if (condition 3) {do that};
else (do this)
How do I go about this?
simply
if(condition){
}
else if(condition){
}
else if(condition){
}
else{
}
you can use switch case statements too. you can use else if as many as you want. Each condition inside if() can accept OR and AND operators as || for OR and && for AND you can use.
if (condition 1) { do this
} else if (condition 2) {do that
} else if (condition 3) {do that
} else { do this }
alternatively if you want to check one variable each time you can use a switch for example
$myvar = 5;
switch($myvar){
case 1:
//do this
break;
case 2:
//do that
break;
case 3:
//do that
break;
default:
//do this
}
You can use this construction
if ( $a == $b ) {
// something...
} else if ( $a == $c ) {
// something...
} else if ( $a == $d ) {
// something...
} else {
// otherwise...
}
But if all of the conditions are ( $a equals to something ) it's better to use switch ... case:
switch ( $a ) {
case $b:
// something...
break;
case $c:
// something...
break;
case $d:
// something...
break;
default:
// otherwise...
break;
}
Solution 1
Using many elseif statements as you want.
Use this solution when your conditions are complex, or comparing different variables.
if (/*condition 1*/) {
// Action to condition 1
} else if (/*condition 2*/) {
// Action to condition 2
} else if (/*condition 3*/) {
// Action to condition 3
} else if (/*condition n*/) {
// Action to condition n
} else {
// Action when no conditions match.
}
Solution 2
Using switch statement:
Use this condition when you want to compare a variable against constant values:
switch ($age) {
case 0:
return 'You are a baby';
break;
case 18:
return 'You are 18 years old';
break;
case 21:
case 22:
case 23:
return 'You are too old';
default:
return 'Unexpected age :(';
}
How about using the or operator, ||:
if (condition 1) { do this }
else if (condition 2 || condition 3) {do that}
else {do this}
An example:
<?php
function testCondition($a, $b) {
if ($a == $b) {
print ("They are the same<br />\n");
}
else if ($a == "a" || $b == "b") {
print ("One is the same as its letter<br />\n");
}
else {
print ("They are some other sort<br />\n");
}
}
testCondition("c","c");
testCondition("a","c");
testCondition("a","b");
testCondition("d","e");
?>
Outputs:
They are the same
One is the same as its letter
One is the same as its letter
They are some other sort
I have the same problem as first stated here. I have four variables but the second one is skipped and only 1,3, and 4 work. Why?
if(empty($fromName) or empty($fromEmail) or empty($subject) or empty($comments)) {
echo 'You cannot submit the form with empty fields. Please correct the form and resubmit.';
return false;
}
elseif($fieldDelete == "Delete this text!"){
echo "Delete the contents of the fourth field before submitting.";
return false;
}
elseif (($fromName == "Curtisvien") || ($fromName == "Thomastymn") || ($fromName == "RichardMark")) {
echo "Failed. Please try again.";
return false;
}
else {
$flgchk = mail ("$to", "$subject", "$message", "$headers");
$imgfile = "images/NatMap logo2.gif";
$handle = fopen($filename, "r");
$imgbinary = fread(fopen($imgfile, "r"), filesize($imgfile));
echo '<img src="data:image/gif;base64,' . base64_encode($imgbinary) . '" width=427 height=72 />';
echo "\n<br />\n<br />Thank You! An e-mail has been sent to the National Map web team and they will get back to you in the next 24-48 hours.";
}`enter code here`
I want a statement something like:
IF(someVar == someOtherVar) {
//do some calculations here
}
It looks like this without using variables:
IF($_RESULT[priArrest]== 'No' ) {
//do some calculations here
}
I want to "build" that IF statement as $x using data from a CSV file named "matrixData"
$x='$_RESULT[';
$x.=$matrixData[0];
$x.="]='";
$x.=$matrixData[4];
$x.="'";
//$x defined as ....... $_RESULT[priArrest]== 'No'
IF($x) {
//do some calculations here
echo ('BINGO');
}
$x defined as ....... $_RESULT[priArrest]== 'No'
IF($x) always returns TRUE because $x is defined (I understand why I'm getting T all the time).
I want IF($x) to return T only if the CONTENT of $x is true.
ie: IF($x) always evaluates whether $x is TRUE, not whether $_RESULT[priArrest]=='No'
What is the syntax to return the result of $x rather than the literal $x?
I can easily ECHO what I want ($x), but would like to learn how to embed the $x into an IF()
I've tried defining $x as $$x
Tried using (just to demonstrate my ignorance):
IF ($x) {
}
IF (($x)) {
}
IF (($$x)) {
}
IF ((${$x})) {
}
IF ((&$x)) {
}
Based on results of searching various sources of PHP help.
As rmirabelle wrote in a comment, you can use the condition directly:
if ($_RESULT[$matrixData[0]] == $matrixData[4]) {
echo ('BINGO');
}
// Or use another condition:
if ($_RESULT[$matrixData[0]] < $matrixData[4]) {
echo ('BINGO');
}
You can also store this boolean value for later use:
$x = ($_RESULT[$matrixData[0]] < $matrixData[4]);
// …
if ($x) {
echo ('BINGO');
}
I have a high-score.php page, depending on which link is pressed there is a value added to the url.
Example
/high-score.php?score=image_set_1
To get the value of the url I use the following:
<?php
$pack=$_GET['score'];
?>
Then I can echo the results
<?php echo $pack; ?>
This will output: image_set_1
What I would like to do is change image_set_1 to display Fruit and then the same for all the other Image_set
This is what I have so far
<?php
$pack=$_GET['score'];
if ($pack = "image_set_1"){
$pack = "Fruit cards";
}
else if ($pack = "image_set_2"){
$pack = "Number cards";
}
else if ($pack = "image_set_3"){
$pack = "Animal cards";
}
else if ($pack = "image_set_4"){
$pack = "Vegetable cards";
}
?>
The problem is the output is always fruit no matter what the url value is.
If I change the code from else if to just if then it displays just vegetable cards
The correct Comparison Operator is == not =
if ($pack = "image_set_1") {
// code
}
Needs to be:
if ($pack == "image_set_1") {
// ---^
// code
}
You need == to compare values, a single = sets a value.
if ($pack == "image_set_1"){
$pack = "Fruit cards";
}
Like so.
Reference: http://www.php.net/manual/en/language.operators.comparison.php
In PHP, is the following logic allowed
If (x && y)
//Do A
Elseif (x)
// Do B
Elseif (y)
// Do C
Else
// Do D
Basically, are you allowed to use more than one elseif?
Yes:
if ($x && $y) {
//Do A
} else if ($x) {
// Do B
} else if ($y) {
// Do C
} else {
// Do D
}
Another format useful for HTML files
<?php if ($x && $y): ?>
Element A
<?php elseif ($x): ?>
Element B
<?php elseif ($y): ?>
Element C
<?php else: ?>
Element D
<?php endif;?>
Yes, although if the test is simple ($a == $b), use a switch instead:
switch ($a) {
case $b:
break;
case $c:
break;
default:
//Like else
}
Yes. Please see http://us3.php.net/elseif and http://us3.php.net/elseif.
You can use
if($x)
// for one line of code
elseif($y)
// also for one line of code
if($x) {
// for more than
// one line of code
} elseif($y) {
// also for multi-
// line codes
}
and
if($x):
// multi-line
endif;
yup
if(this == this && this == this){
//this and that
}else if(this == that || this == that){
//this or that
}else{
//anything else
}