Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

393
Views
How can I delete 'AND' in last Loop (for each)

I have a question. Heres my code

<p><strong style="color: blue;">IF</strong></p>
<?php foreach ($data as $i => $key) : ?>
    <p><?php echo $key['symptoms'] ?></p>
    <p><strong style="color: green;">AND</strong></p>
<?php endforeach ?>
<p style="color: red;"> <strong>THEN</strong></p>
<p><?php echo $key['disease_name'] ?></p>

But, The output is like this

IF
symptoms A
AND
symptoms B
AND
symptoms C
AND
THEN
disease A

I want the last AND to be disappear. Can anyone help me pls? :( I'm sorry because I'm a newbie in here. Thanks!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to check if $i is less than the last element in the array: $i < count($data) - 1. Here is an example:

<p><strong style="color: blue;">IF</strong></p>
<?php foreach ($data as $i => $key) : ?>
    <p><?php echo $key['symptoms'] ?></p>
   <!-- only print AND if it is not the last element --> 
   <?php if ($i < count ($data) - 1) { ?>
         <p><strong style="color: green;">AND</strong></p>
    <?php } ?>
<?php endforeach ?>
<p style="color: red;"> <strong>THEN</strong></p>
<p><?php echo $key['disease_name'] ?></p>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!