So I'm trying to do something like this
if($id == 1 || $id == 2 || $id == 5 || $id == 8) {
echo 'test';
} elseif($id == 6) {
echo 'test2';
} else {
echo 'error';
}
It's so I can show a specific message (with html etc in the finished version) when the id obtained via post method is different.
EDIT: The issue is the code repeats itself twice.
How could I go about resolving this?
Thanks to anyone who contributes into helping me with this!
Use in_array().
$arr = [1,2,5,8];
$id = 1;
if(in_array($id, $arr)) {
echo 'test';
} elseif($id == 6) {
echo 'test2';
} else {
echo 'error';
}
https://3v4l.org/T5giX
As i see you have more than 3 id to compare so easy Solution is take all id as an array
And use in_array.
<?php
$id = 3;
$myid = array(1,2,5,8);
if(in_array($id,$myid)) {
echo 'test';
} elseif($id == 6) {
echo 'test2';
} else {
echo 'error';
}
Also You Can Use PHP Switch Statement
<?php
$id = 6;
switch ($id) {
case 1:
case 2:
case 5:
case 8:
echo "Passed .!";
break;
case 6:
echo "Passwd 2!";
break;
default:
echo "Failed .!";
}
to make this statement in one line use ternary operator. hope it satisfy you.
echo ( ($id == 1 || $id == 2 || $id == 5 || $id == 8) ? "test" : (($id == 6) ? "test2" : "error"));
Related
If you have an if/else statement like the one below, is there a way to check something inside the first matching if and tell it to skip ahead to the next else/if, and do this multiple times?
continue seemed promising after some googling, but didn't work so maybe that's only for loops
if ($teamscore > 100) {
if ($somethingelse=$something) {
//skip to the next "elseif"
}
} elseif ($teamscore > 95) {
if ($somethingelse=$something) {
//skip to the next "elseif"
}
} elseif ($teamscore > 90) {
} else {
}
It seems like what you're going for is sort of like a switch, but you can't evaluate separate expressions in each case, so you can't use it for inequalities like this. I think this structure could be used instead.
while (true) {
if ($teamscore > 100) {
// DO STUFF
if ($somethingelse != $something) {
break;
}
}
if ($teamscore > 95) {
// DO STUFF
if ($somethingelse != $something) {
break;
}
}
if ($teamscore > 90) {
// DO STUFF
if ($somethingelse != $something) {
break;
}
}
break;
}
However, if // DO STUFF is the same thing in each if block, or a variation of the same thing that fits a pattern, you probably could use a loop instead to avoid the repetition.
for ($score = 100; $score > 85; $score -= 5) {
if ($teamscore > $score) {
// DO STUFF
}
if ($somethingelse != $something) {
break;
}
}
You should turn your condition bodies (in my example, the echo's) into functions:
if ($foo == 'bar') {
echo 'it is bar!';
} elseif ($foo == 'foobar') {
echo 'it is is foobar!';
} else {
echo 'it\'s nada!';
}
becomes:
function sayBar()
{
return 'it is bar!';
}
function sayFooBar()
{
return 'it is foobar!';
}
function sayNada()
{
return 'it\'s nada!';
}
if ($foo == 'bar') {
echo sayBar();
if ($bar == 'Treybake is awesome') {
echo sayFooBar();
}
} elseif ($foo == 'foobar') {
echo sayFooBar();
} else {
echo sayNada();
}
you might be looking for somthing like this,
if ($teamscore > 100 && $somethingelse !== $something) {
} elseif ($teamscore > 95 && $somethingelse !== $something) {
} elseif ($teamscore > 90 && $somethingelse !== $something) {
} else {
}
I've simple PHP script:
<?php
$input = readline();
echo gettype($input);
?>
It reads user input from the console. What I am trying to achieve is to get properly data type. At the moment $input is string type.
I need something like this:
Input Output
5 Integer
2.5 float
true Boolean
I can't get any idea how to do it. Thanks.
EDIT: Thanks to #bcperth answer, I achieve this working code:
<?php
while(true) {
$input = readline();
if($input == "END") return ;
if(is_numeric($input)) {
$sum = 0;
$sum += $input;
switch(gettype($sum)) {
case "integer": $type = "integer"; break;
case "double": $type = "floating point"; break;
}
echo "$input is $type type" . PHP_EOL;
}
if(strlen($input) == 1 && !is_numeric($input)) {
echo "$input is character type" . PHP_EOL;
} else if(strlen($input) > 1 && !is_numeric($input) && strtolower($input) != "true" && strtolower($input) != "false") {
echo "$input is string type" . PHP_EOL;
} if(strtolower($input) == "true" || strtolower($input) == "false") {
echo "$input is boolean type" . PHP_EOL;
}
}
?>
Also tried with filter_var, working well:
<?php
while(true) {
$input = readline();
if($input == "END") return;
if(!empty($input)) {
if(filter_var($input, FILTER_VALIDATE_INT) || filter_var($input, FILTER_VALIDATE_INT) === 0) {
echo "$input is integer type" . PHP_EOL;
} else if(filter_var($input, FILTER_VALIDATE_FLOAT) || filter_var($input, FILTER_VALIDATE_FLOAT) === 0.0) {
echo "$input is floating point type" . PHP_EOL;
} else if(filter_var($input, FILTER_VALIDATE_BOOLEAN) || strtolower($input) == "false") {
echo "$input is boolean type" . PHP_EOL;
} else if(strlen($input) == 1) {
echo "$input is character type" . PHP_EOL;
} else {
echo "$input is string type" . PHP_EOL;
}
}
}
?>
You would need to employ a few strategies as below for simple types.
test if numeric using is_numeric().
if numeric then add it to zero and gettype() the result
if not numeric then compare to "true" and "false"
if not "true" or "false" then its a string
Here is a working start that shows how to go about it.
<?php
$input = readline();
if (is_numeric($input)){
$sum =0;
$sum += $input;
echo gettype($sum);
}
else {
if ($input== "true" or $input == "false"){
echo "boolean";
}
else {
echo "string";
}
}
?>
I have a condition in which it is using a variable to pull either a number through 0-17, the string "MAKEUP", or the variable will be empty. I would like it to output the text "WIN" if the variable is greater than the number 8 and "LOSS" if the variable is less than the number 9. I would also like it to out "MAKEUP" if the variable consist of the string MAKEUP, and to display nothing if the variable is empty. Seems pretty simple to me, but I'm having issues particularly with the empty part. Can anyone let me know what I am doing wrong here? Code below
<?php
$t1w8 = '';
$result = $t1w8;
if ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result < 9 && !empty($result)) {
echo 'LOSS';
} elseif ($result == 'MAKEUP') {
echo '-';
} else {
echo 'yooo';
}
?>
Make some changes in your conditions like this
<?php
//$result = "MAKEUP";
$result = 0;
if ($result === 'MAKEUP') {
echo '-';
}else if (is_numeric($result) && $result < 9 ) {
echo 'LOSS';
}else if (is_numeric($result) && $result >= 9 ) {
echo 'WON';
} else{
echo 'yooo';
}
?>
Live demo : https://eval.in/897120
try with this code
<?php
//$result = "MAKEUP";
$result = "";
//$result = "9";
//$result = "-";
if ($result == 'MAKEUP' && !empty($result) ) {
echo '-';
} elseif ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result <= 8 && !empty($result)) {
echo 'LOSS';
} else {
echo 'yooo';
}
?>
for demo :demo code here
You have explained that your number range is from 0-17.
You have also explained that you could get the word MAKEUP.
Based upon those constraints we could use something like this
$output = "";
// Do we have something?
if(strlen($result) > 0) {
if (strtolower($result) == "makeup") {
$output = "MAKEUP";
}
// assumes a single digit string
else if ($result < 9) {
$output = "LOSS";
} else if ($result <= 17) {
$output = "WIN";
}
}
echo $output;
Just out of curiosity (and a bit of necessity):
if(! is_null($var)){
//do something
}
Is the above statement the same as
if($var != NULL){
//do something
}
No they are not the same.
The is_null function compairs the type also.
Example:
var_dump(is_null(0)); // bool(false)
var_dump(0 == NULL); // bool(true)
var_dump(0 === NULL); // bool(false)
So in your case
if(! is_null($var)){
//do something
}
Would be the same as
if($var !== NULL){
//do something
}
Yes this is (almost) correct, you can test this yourself:
$emptyvar1 = null;
$emptyvar2="";
if(is_null($emptyvar1) && $emptyvar1 == NULL){
echo "1";
}
if(is_null($emptyvar2)){
echo "2";
}
if($emptyvar2 == null){
echo "3";
}
if($emptyvar2 === null){
echo "4";
}
This will print 1 and 3.
because an empty string is equal to null if you only use 2 times =
if you use 3 times = it aint.
=== also checks object type
== only checks value
I'm not sure what exactly you're testing, but on:
a) $var = NULL;
neither of the statements triggers,
b) $var = 0;
is_null triggers and
c) $var = ''; is_null triggers aswell.
So the statements above are definitely not coming to the same conclusion.
See for yourself:
echo 'testing NULL case<br>';
$var = NULL;
if(! is_null($var)){
echo 'var is_null<br>';
}
if($var != NULL){
echo 'var != null<br>';
}
echo 'testing 0 case<br>';
$var = 0;
if(! is_null($var)){
echo 'var is_null<br>';
}
if($var != NULL){
echo 'var != null<br>';
}
echo 'testing empty string case<br>';
$var = '';
if(! is_null($var)){
echo 'var is_null<br>';
}
if($var != NULL){
echo 'var != null<br>';
}
this outputs
testing NULL case
testing 0 case
var is_null
testing empty string case
var is_null
How can I simplify if conditions, because per each condition I make a new if/elseif and its a lot of code, this is what I have:
$chapters = array('1:data1', '2:data2', '4:datax', '3:datag');
sort($chapters);
$screenshots = array('1:screen1', '2:screen2', '3:screen3', '4:go4');
$chapterCount = count($chapters);
$chapterItems = 0;
foreach ($screenshots as $key => $screenshot) {
$screenshotInfo = explode(':', $screenshot);
$screen[$screenshotInfo[0]] = $screenshotInfo[1];
}
foreach ($chapters as $chapter) {
$chapterInfo = explode(':', $chapter);
$chapterNumber = current($chapterInfo);
// If is the first chapter
if ($chapterNumber == 1) {
echo '<li>'.$chapterInfo[0].'</li>';
// If have only one chapter
if ($chapterItems+1 == $chapterCount) {
echo '<li>'.$screen[$chapterNumber].'</li>';
}
}
// If is a new chapter
elseif ($currentNumber != $chapterNumber) {
echo '<li>'.$screen[$chapterNumber-1].'</li>';
echo '<li>'.$chapterInfo[0].'</li>';
// If is new and last chapter
if ($chapterItems+1 == $chapterCount) {
echo '<li>'.$screen[$chapterNumber].'</li>';
}
}
// If is the last chapter
elseif ($chapterItems+1 == $chapterCount) {
echo '<li>'.$chapterInfo[0].'</li>';
echo '<li>'.$screen[$chapterNumber].'</li>';
}
else {
echo '<li>'.$chapterInfo[0].'</li>';
}
$currentNumber = $chapterNumber;
$chapterItems++;
}
That code works perfect on my tests but I'm sure it have a lot of unneeded code.
If brevity is of more interest than readability, you can use ternary statements.
$foo = true;
$bar = $foo ? 'something' : 'nothing';
echo $bar;
//returns 'something'
$foo = false;
$bar = $foo ? 'something' : 'nothing';
$echo bar;
//returns 'nothing'
May be you could use a function for all the if-conditions. Since all the if-conditions inside are same we could use something like:
$chapters = array('1:data1', '2:data2', '4:datax', '3:datag');
sort($chapters);
$screenshots = array('1:screen1', '2:screen2', '3:screen3', '4:go4');
$chapterCount = count($chapters);
$chapterItems = 0;
foreach ($screenshots as $key => $screenshot) {
$screenshotInfo = explode(':', $screenshot);
$screen[$screenshotInfo[0]] = $screenshotInfo[1];
}
foreach ($chapters as $chapter) {
$chapterInfo = explode(':', $chapter);
$chapterNumber = current($chapterInfo);
if ($chapterNumber == 1) { // If is the first chapter
echo '<li>'.$chapterInfo[0].'</li>';
echo compare($chapterItems,$chapterCount,$screen,$chapterNumber); // If have only one chapter
} elseif ($currentNumber != $chapterNumber) { // If is a new chapter
echo '<li>'.$screen[$chapterNumber-1].'</li>';
echo '<li>'.$chapterInfo[0].'</li>';
echo compare($chapterItems,$chapterCount,$screen,$chapterNumber); // If is new and last chapter
} elseif ($chapterItems+1 == $chapterCount) { // If is the last chapter
echo '<li>'.$chapterInfo[0].'</li>';
echo '<li>'.$screen[$chapterNumber].'</li>';
} else {
echo '<li>'.$chapterInfo[0].'</li>';
}
$currentNumber = $chapterNumber;
$chapterItems++;
}
function compare($chapterItems,$chapterCount,$screen,$chapterNumber) {
if ($chapterItems+1 == $chapterCount) {
return '<li>'.$screen[$chapterNumber].'</li>';
}
return false;
}
Hope this helps you :)
If you check the original code you will see that:
echo '<li>'.$chapterInfo[0].'</li>';
will be printed in every scenario. So we don't actually need an if for that.
The only checks you need is:
isNew and not isFirst
if ($currentNumber != $chapterNumber && $chapterNumber != 1)
so we can print the 'new header':
echo '<li>'.$screen[$chapterNumber-1].'</li>';
And if it's the last one, so we can print the last item footer:
echo '<li>'.$screen[$chapterNumber].'</li>';
So we can simplify by having the bellow code:
foreach ($chapters as $chapter) {
$chapterInfo = explode(':', $chapter);
$chapterNumber = current($chapterInfo);
// If is new AND is not the first print the 'New item header'
if ($currentNumber != $chapterNumber && $chapterNumber != 1) {
echo '<li>'.$screen[$chapterNumber-1].'</li>';
}
// This was being printed in every scenario, so we don't need a if for that
echo '<li>'.$chapterInfo[0].'</li>';
// If it is the last, print the 'Last item footer'
if ($chapterItems+1 == $chapterCount) {
echo '<li>'.$screen[$chapterNumber].'</li>';
}
$currentNumber = $chapterNumber;
$chapterItems++;
}
if your goal is just to remove if/else and get easier code coverage you can do the same thing but only with ternaries like that:
foreach ($chapters as $chapter) {
$chapterInfo = explode(':', $chapter);
$chapterNumber = current($chapterInfo);
echo ($currentNumber != $chapterNumber && $chapterNumber != 1) ? sprintf('<li> %s </li>', $screen[$chapterNumber - 1]) : '';
echo sprintf('<li> %s </li>', $chapterInfo[0]);
echo ($chapterItems + 1 == $chapterCount)? sprintf('<li> %s </li>', $screen[$chapterNumber]): '';
$currentNumber = $chapterNumber;
$chapterItems++;
}