
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Array Values with Text for Even Indexes in PHP
For this, you can use for loop along with some conditions.
The PHP code is as follows −
Example
<!DOCTYPE html> <html> <body> <?php $arrayList = []; for ($counter = 0; $counter < 5; $counter++) { ($counter%2) ? ($arrayList[] = $counter) : ($arrayList[] = "Even"); } for ($counter = 0; $counter < 5; $counter++) { echo $arrayList[$counter]," "; } ?> </body> </html>
Output
Even 1 Even 3 Even
Above, for 0th index, the text “Even” is displayed and the same goes on for 2th and 4th index.
Advertisements