I want to check if $AD = 1, then if not pass to another function, but if $AD = 1 check if AC = contains some value, if not do some stuff.
if ($AD == '1'){
if ($AC == ''){
echo 'AC is empty';
} else {
///////Function/////
}
}
Thanks.
An else if statement must always correspond to a beginning if statement on the same "nesting level".
Your inner else if does not have any beginning if statement and it therefore fails.
if ($AD == 1) {
if ($AC == '') {
echo 'empty';
}
}
else {
otherFunction($AD);
}
Since this is basic PHP (actually IFs are mostly not language-dependent) stuff, I recommend you reading a beginner's tutorial.
if ($AD == 1){
if ($AC == ''){
// some code where ac has the value
} else {
// some code where ac doesn't have the value
}
} else {
// other function
}
That should do it
Related
how I can define a variable inside a PHP function and make it reachable from another function? I want to set $error variable to "1".
This how would the code look like:
function checkthename ($name){
if ($name == "TESTTEST"){
echo $error = 0;
}
else {
echo $error = 1;
}
}
function phone ($phone){
if ($name == "TESTTEST"){
echo $error = 0;
}
else {
echo $error = 1;
}
}
etc.
And in end of the code there would be an another function which is checking if there is any $error variable set to 1
function checktheerror($error){
if ($error == 0){
echo "no error in this code";
}
elseif ($error == 1){
echo "there is one or multiple errors";
}
}
Thank you so much for your help!
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 am working on validation and comparisons!! i have a field that can contain the value $val=0 or $val="some-value" or $val="" or $val=0 basically i want the $val="0"or $val=0 to be validated as true..
if($val){
//works for $val="some-value"
//doesnot work for $val=0 or $val="0";
} else
{
//works corrent for $val=""
}
one conditional approach i used is
$val="";
if($val || $val==0){
echo "true";
}
else
{
//should be false but it is true
echo "false";
}
did you try this?
$val = "";
if ($val == '0') {
echo "TRUE";
# code...
}
elseif ($val == "") {
echo "FALSE";
}
There is a useful php native function is_null
if (is_null($val) || $val === "") {
//invalid
} else {
//valid
}
You can use PHP integer casting & can do it like this:
if ((int) $val === 0) {
return true;
} else {
return false;
}
Hope this helps!
So I've got this code...
code:
if(empty($day0[2])) {
echo "<td>".$day0[1]."<br></td>";
} else {
if(strcmp($day0[1],"Absent") == 0) {
echo "<td>".$day0[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day0[1]."<br>Time: ".#$day0[2]." - ".#$day0[3]."</td>";
}
}
if(empty($day1[2])) {
echo "<td>".$day1[1]."<br></td>";
} else {
if(strcmp($day1[1],"Absent") == 0) {
echo "<td>".$day1[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day1[1]."<br>Time: ".#$day1[2]." - ".#$day1[3]."</td>";
}
}
if(empty($day2[2])) {
echo "<td>".$day2[1]."<br></td>";
} else {
if(strcmp($day2[1],"Absent") == 0) {
echo "<td>".$day2[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day2[1]."<br>Time: ".#$day2[2]." - ".#$day2[3]."</td>";
}
}
if(empty($day3[2])) {
echo "<td>".$day3[1]."<br></td>";
} else {
if(strcmp($day3[1],"Absent") == 0) {
echo "<td>".$day3[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day3[1]."<br>Time: ".#$day3[2]." - ".#$day3[3]."</td>";
}
}
if(empty($day4[2])) {
echo "<td>".$day4[1]."<br></td>";
} else {
if(strcmp($day4[1],"Absent") == 0) {
echo "<td>".$day4[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day4[1]."<br>Time: ".#$day4[2]." - ".#$day4[3]."</td>";
}
}
if(empty($day5[2])) {
echo "<td>".$day5[1]."<br></td>";
} else {
if(strcmp($day5[1],"Absent") == 0) {
echo "<td>".$day5[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day5[1]."<br>Time: ".#$day5[2]." - ".#$day5[3]."</td>";
}
}
if(empty($day6[2])) {
echo "<td>".$day6[1]."<br></td>";
} else {
if(strcmp($day6[1],"Absent") == 0) {
echo "<td>".$day6[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day6[1]."<br>Time: ".#$day6[2]." - ".#$day6[3]."</td>";
}
}
Where day 0-6 equals Sunday through Saturday, and the array numbers attached to each one equals a different variable in a multi-dim array.
That is several if statements that are all exactly the same except the variable name inside of each one. I haven't been able to find a way to make this shorter, so I thought I would post here to try and see if anyone has any ideas on how I can combine this into shorter lines of code. I'm all about my code looking neater and functioning better, and I think this could teach a lot of people good ways to shorten their code down a bit.
First merge all array of $day[n] in to $finalArray
foreach($finalArray as $key=>$value){
if(empty($value[2])) {
echo "<td>".$value[1]."<br></td>";
} else {
if(strcmp($value[1],"Absent") == 0) {
echo "<td>".$value[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$value[1]."<br>Time: ".#$value[2]." - ".#$value[3]."</td>";
}
}
}
#MagnusEriksson suggested making a function, I think this is the best way to do it.
From 69 lines of code to 18 lines of code.
function displayTime($day1,$day2,$day3) {
if(empty($day2)) {
return "<td>{$day1}<br></td>";
} else {
if(strcmp($day1,"Absent") == 0) {
return "<td>{$day1}<br>Time: N/A</td>";
}
return "<td>{$day1}<br>Time: {$day2} - {$day3}</td>";
}
}
for ($x = 0; $x <= 6; $x++) {
echo displayTime(${"day$x"}[1],${"day$x"}[2],${"day$x"}[3]);
}
Please try with this code.
$array=array($day0,$day1,$day2,$day3);
for($i=0;$i<count($array);$i++){
if(empty($day.$i[2])) {
echo "<td>".$day.$i[1]."<br></td>";
} else {
if(strcmp($day.$i[1],"Absent") == 0) {
echo "<td>".$day.$i[1]."<br>Time: N/A</td>";
} else {
echo "<td>".$day.$i[1]."<br>Time: ".#$day.$i[2]." - ".#$day.$i[3]."</td>";
}
}
}
I have this piece of code:
if($this->ask('Is this holiday booked? [y|N]')) {
$holiday->booked = true;
} else {
$holiday->booked = false;
}
in a Laravel 5.2 command, but whatever the response it always seems to return true.
I also tried:
if($this->ask('Is this holiday booked? [y|N]') === true) {
$holiday->booked = true;
} else {
$holiday->booked = false;
}
But this always enters it into the database as false regardless of if I enter y or n.
It will no doubt be something stupid, but can anyone see where I'm going wrong?
Thanks.
Ended up using:
if(!$this->confirm('Is this holiday booked? [y|N]'), false) {
$holiday->booked = false;
} else {
$holiday->booked = true;
}
Or Try This :) :
$input = $this->ask('Is this holiday booked? [y|n]');
if($input == 'y' || $input == 'Y') {
$holiday->booked = true;
}
elseif($input == 'n' || $input == 'N') {
$holiday->booked = false;
}
else {
$this->error("wrong input");
}