0

Hello I have created a $person = array(...) and a foreach loop that iterate through the array. I'm trying to figure out how to echo/print out all string that has "Dr." prefixes.

<?php

$persons = [
    "Dr. Cooper",
    "Mrs. Dalloway",
    "Mr. Dalloway",
    "Dr. Mabuse",
    "Col. Stephens",
    "Dr. Ashcroft"
];

foreach($persons as $person) {
    echo '<li>'. $person . '</li>'. '<br />';
}

Disclaimer: I've started a new course on php and database, and trying to practice asking questions on internet forums. I have good understanding of javascript, so it would benefit me if you could compare the solution with javascript. Otherwise just explain the solution. Thanks in advance!

  • An important part of *trying to practice asking questions on internet forums* is to do some research and show some effort in your coding before asking questions. – Nigel Ren Apr 23 '20 at 12:50
  • you can use strpos($person,"Dr") , which you could find on first search – Ahmed Sunny Apr 23 '20 at 12:50
  • I see, I can assure you that I did not out of lazyness ask these question, even though it might seems like it. I've been watching youtubes and googling, the solutions i've have tried is creating an variable $arr with array and that has key and keyvalue, and trying filter, which ended up way to many code. My assumption was I'm heading the wrong way and it should'nt be this complicated. – Nara Tekchuen Apr 23 '20 at 13:09
  • @AhmedSunny I would appriciate if you could link me the page, where you found the solution, it would benefit me greatly. Thanks in advance. – Nara Tekchuen Apr 23 '20 at 13:12
  • [here](https://stackoverflow.com/a/4366748/2996989) or just use if(strpos($person,'Dr') !== False) – Ahmed Sunny Apr 23 '20 at 13:18

0 Answers0