I was practising statements and ran into a problem. I'm trying to make it so that if $child3 is available, then it will echo all three children, like how when $child2 is available, it echos Kim & Pom. What did I do wrong?
$child = "Kim";
$child2 = "Pom";
$child3 = "Rob";
if($child2) {
echo $child; echo " "; echo $child2;
} elseif($child3) {
echo $child; echo " "; echo $child2; echo " "; echo $child3;
} else {
echo $child;
}
Try reversing the conditions:
$child = "Kim";
$child2 = "Pom";
$child3 = "Rob";
if($child3){
echo $child . " " . $child2 . " " . $child3;
}else if($child2){
echo $child . " " . $child2;
}else{
echo $child;
}
You might also like the wonderful string concatenation operator ..
elseif only executes if none of the preceding ifs or elseifs executed. So you'll want to move it above the other if:
if($child3) {
echo $child; echo " "; echo $child2; echo " "; echo $child3;
} elseif($child2) {
echo $child; echo " "; echo $child2;
} else {
echo $child;
}
Probably you want something like this:
$child = 1; // Or $child = 2; Or $child = 3;
if ($child == 1) {
echo 'Kim';
} elseif ($child == 2) {
echo 'Pom';
} else {
echo 'Rob';
}
But I recommend switch() as soon as you have elseif.
$child = "Kim";
$child2 = "Pom";
$child3 = "Rob";
if($child3){
echo $child; echo " "; echo $child2; echo " "; echo $child3;
}elseif($child2){
echo $child; echo " "; echo $child2;
}else{
echo $child;
}
How about this?
if ($child) {
echo "{$child} ";
}
if ($child2) {
echo "{$child2} ";
}
if ($child3) {
echo "{$child3} ";
}
If that's your actual code, you'll only ever get Kim Rob, as $child2 will always be true, thereby bypassing your else clauses.
Also, you can combine your echos into:
echo "$child $child2 $child3";
It makes it far easier to read that way.
Here's how I would do it. I agree with Dragon that a switch would be clearer/better.
switch (true)
{
case (isset($child,$child2,$child3)) :
echo $child . ' ' . $child2 . ' ' . $child3;
break;
case (isset($child, $child2)) :
echo $child . ' ' . $child2;
break;
case (isset($child)) :
echo $child;
break;
}
Related
I've written some code that works fine, but is extremely verbose and would like a pointer on how to make it more efficient. I'm running different SQL queries because a single one returns too many results;
$sql1 = "select blah blah blah";
$sql2 = "select blah blah blah";
$sql3 = "select blah blah blah";
$response1 = $client->executeSQLQuery(array("sql"=>$sql1));
$response2 = $client->executeSQLQuery(array("sql"=>$sql2));
$response3 = $client->executeSQLQuery(array("sql"=>$sql3));
if( is_array($response1->return->row) )
{
foreach($response1->return->row as $numbers)
{
echo $numbers->dnorpattern . "<br>";
$count++;
}
}
else
{
echo $response1->return->row->dnorpattern . "<br>";
$count++;
}
if( is_array($response2->return->row) )
{
foreach($response2->return->row as $numbers)
{
echo $numbers->dnorpattern . "<br>";
$count++;
}
}
else
{
echo $response2->return->row->dnorpattern . "<br>";
$count++;
}
if( is_array($response3->return->row) )
{
foreach($response3->return->row as $numbers)
{
echo $numbers->dnorpattern . "<br>";
$count++;
}
}
else
{
echo $response3->return->row->dnorpattern . "<br>";
$count++;
}
So, I would like to change the if statements to be a single if statement like this;
if( is_array($response1->return->row) )
{
foreach($response1->return->row as $numbers)
{
echo $numbers->dnorpattern . "<br>";
$count++;
}
}
else
{
echo $response1->return->row->dnorpattern . "<br>";
$count++;
}
The integer for $sql and $response is predictable, so I should be able to use a for statement
for($x=1, $x<=3, $x++)
{
if( is_array($response[$x]->return->row) )
{
foreach($response[$x]->return->row as $numbers)
{
echo $numbers->dnorpattern . "<br>";
$count++;
}
}
else
{
echo $response[$x]->return->row->dnorpattern . "<br>";
$count++;
}
}
But this does not work, can someone explain how I can increment the integer properly?
You can use curly braces syntax:
for($x=1; $x<=3; $x++)
{
if( is_array(${'response' .$x}->return->row) )
{
foreach(${'response' .$x}->return->row as $numbers)
{
echo $numbers->dnorpattern . "<br>";
$count++;
}
}
else
{
echo ${'response' .$x}->return->row->dnorpattern . "<br>";
$count++;
}
}
OR save your three response objects in an array, and use your original syntax:
$response = [];
$response[1] = $client->executeSQLQuery(array("sql"=>$sql1));
$response[2] = $client->executeSQLQuery(array("sql"=>$sql2));
$response[3] = $client->executeSQLQuery(array("sql"=>$sql3));
Though you could probably just write a better SQL query and vastly simplify things
Here it's my sample of code :
public function show($imei,$start_time,$end_time,$dateto) {
$from_time = str_replace('-','/',$start_time);
$fromi=strtotime($from_time . ' ' . $end_time);
$too1=strtotime($from_time . ' ' . $dateto);
$data['coordinates'] = $this->road_model->get_coordinatesudhetime($imei, $fromi, $too1);
$this->load->view('road/show', $data);
if (!empty($data))
{
echo 'Array it's empty*';
}
}
I want to check when $data it's empty .
if (empty($data))
{
echo "array is empty";
}
else
{
echo "not empty";
}
or count($data) returns the size of array.
You can do this way also
if(is_array($data) && count($data)>0)
{
echo "not empty";
}else{
echo "empty";
}
I have these arrays:
$payments = array(1=>'Cash',2=>Cheque,3=>credit,4=>other);
$selected = array(2,1);
foreach($payments as $key=>$value) {
foreach($selected as $id) {
if ($key == $id) {
echo $id . "is selected" . '<br>';
}
else{
echo $id . " is not selected" . '<br>';
}
}
}
what expected:
1 is selected
2 is not selected
3 is not selected
4 is selected
but i got:
1 is not selected
1 is selected
2 is selected
2 is not selected
3 is not selected
3 is not selected
4 is not selected
4 is not selected
what's the wrong in my loops?
You don't need inner loop:
$payments = array(1=>'Cash',2=>Cheque,3=>credit,4=>other);
$selected = array(2,1);
foreach($payments as $key=>$value) {
if (in_array($key, $selected)( {
echo $key . "is selected" . '<br>';
} else {
echo $key . " is not selected" . '<br>';
}
}
You could simply use in_array() to check if an element is selected:
$payments = array(1=>'Cash',2=>'Cheque',3=>'credit',4=>'other');
$selected = array(2,1);
foreach($payments as $key=>$value) {
if (in_array($key, $selected)) {
echo $value . "is selected" . '<br>';
} else {
echo $value . " is not selected" . '<br>';
}
}
By the way, you need quotes around the payment method names.
You can use in_array() instead, so you can omit your inner loop:
foreach($payments as $key=>$value) {
if (in_array($key, $selected)) {
echo $id . "is selected" . '<br>';
}else{
echo $id . " is not selected" . '<br>';
}
}
Example
UPDATE: an alternative solution. Not better (maybe), but different :)
$payments = array(1=>'Cash', 2=>'Cheque', 3=>'Credit', 4=>'Other');
$selected = array('Cash','Cheque');
foreach($selected as $chosen){
$selected_values = array_search($chosen, $payments);
echo "Number ". $selected_values." (".$chosen.") is selected.<br>";
}
You can see the output here
Old solution (as everyone else...)
$payments = array(1=>'Cash', 2=>'Cheque',3=>'Credit',4=>'Other');
$selected = array(2,1);
foreach($payments as $key=>$value) {
if (in_array($key, $selected)) {
echo $key. " is selected" . '<br>';
}else{
echo $key. " is not selected" . '<br>';
}
}
I have written some script that works great, BUT I have tried to add an elseif statement in which appears to do absolutely nothing, can't figure out where I am going wrong. (Yes I am a PHP newbie, but I am trying! :)
$i = 0;
foreach($arrJobs as $job)
{
echo "<div class=\"job-ind\">\n";
echo '<p>' . $job->title . ', ' . $job->location . "</p>\n";
echo '<p class="smaller">Reference: ' . $job->reference . "</p>\n";
echo '<p><strong>' . $job->salary . "</strong></p>\n";
echo "</div>\n";
if (++$i == 5) break;
elseif ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
}
Everything up the elseif statement works perfectly.
Must be something simple I am missing or misunderstanding!
$i will never be 0 at this location.
You increase it in the if-statement before the test (++$i).
Note that elseif and else if will only be considered exactly the same when using curly brackets.
if (++$i == 5) break;
elseif ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
Won't work, change it to:
if ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
elseif (++$i == 5) {
break;
}
elseif vs. else if
I see a lot of people discussing this in the thread.
These are both correct and will both work.
The difference being :
else if() {
}
Translates to:
else {
if(){
}
}
And can't be used in code blocks using colons like this:
if:
stuff
else if()://won't work
//stuff
endif;
basic syntax error ? you can't use the non braced short if's when doing an else if
if (++$i == 5) {
break;
} else if ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
notice the colour highlighting on this block compared to yours
Here is the working outcome, thanks for all your help!
$i = 0;
foreach($arrJobs as $job)
{
echo "<div class=\"job-ind\">\n";
echo '<p>' . $job->title . ', ' . $job->location . "</p>\n";
echo '<p class="smaller">Reference: ' . $job->reference . "</p>\n";
echo '<p><strong>' . $job->salary . "</strong></p>\n";
echo "</div>\n";
}
if ($i = 0) {
echo "<div class=\"job-ind\">\n";
echo '<img src="img/no-vac.png"/>';
echo "</div>\n";
}
else if (++$i == 5) {
break;
}
I'm having a problem with a line of code. My teacher doesn't even see the problem and I've been fighting it for almost a week and a half. any help would be greatly appreciated.
The code:
{
if (count($_POST['CINS']) > 0)
{
echo "<h2>Your CINS picks are:</h2>\n";
echo "<ul>\n";
foreach ($_POST['CINS'] as $element)
{
echo "\t<li>$element</li>\n";
} // end of FOREACH statement
echo "</ul>\n";
} // end of IF count CINS
if (count($_POST['CINT']) > 0 )
{
echo "<h2>Your CINT picks are:</h2>\n";
echo "<ul>\n";
foreach ($_POST['CINT'] as $element2)
{
echo "\t<li>$element2</li>\n";
} // End of FOREACH CINT
echo "</ul>\n";
} // End of IF for CINT
else
{
echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
echo __LINE__;
if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0))) // This is where the problem lies. It's showing up the echo statements even when CINS has a count of 1. but if CINT has a count of 1, the echo statements do not show up.
{
echo "<h2>No classes</h2>\n";
echo "<p>You need to pick a class from BOTH CINT and CINS to be a well rounded student.</p>\n";
echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
}
} // END ELSE COUNT CINS
}
?>
misplaced brackets
if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0))) -> wrong
if ((count($_POST['CINT']) == 0) and (count($_POST['CINS']) == 0))
You don't need all those parentheses:
if(
count($_POST['CINT']) == 0 AND
count($_POST['CINS']) == 0
)
Look how some indentation goes a long way:
{
if (count($_POST['CINS']) > 0) {
echo "<h2>Your CINS picks are:</h2>\n";
echo "<ul>\n";
foreach ($_POST['CINS'] as $element) {
echo "\t<li>$element</li>\n";
}
echo "</ul>\n";
}
if (count($_POST['CINT']) > 0 ) {
echo "<h2>Your CINT picks are:</h2>\n";
echo "<ul>\n";
foreach ($_POST['CINT'] as $element2) {
echo "\t<li>$element2</li>\n";
}
echo "</ul>\n";
}
else {
echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
echo __LINE__;
if(
count($_POST['CINT']) == 0 AND
count($_POST['CINS']) == 0
) {
echo "<h2>No classes</h2>\n";
echo "<p>You need to pick a class from BOTH CINT and CINS to be a well rounded student.</p>\n";
echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
}
}
I think, you want this:
if (count($_POST['CINS']) > 0)
{
…
} // End of IF for CINS
if (count($_POST['CINT']) > 0 )
{
…
} // End of IF for CINT
if (count($_POST['CINS']) == 0 || count($_POST['CINT']) == 0 )
{
…
}
Perhaps parentheses need attention...
Your
if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0)))
might work better as
if ((count($_POST['CINT']) == 0) && (count($_POST['CINS']) == 0))
Give it shot - reply with outcome.