- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP How to display array values with text “even” to be displayed for even index values
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