Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Q4. Write a script that uses the following array and the variable.

$w[0] ='r';
$w[1] ='o';
$w[2] ='t';
$w[3] ='a';
$w[4] ='t';
$w[5] ='o';
$w[6] ='r';
$w[7] ='s';

$s='r';

Your script should search the character($s) and display the key(index) of that character($s).

i. with array_search predefined function


ii. with using foreach loop.

Note that when the values of $s & $w changes your script should continue to work.

<?php
$w[0] ='r';
$w[1] ='o';
$w[2] ='t';
$w[3] ='a';
$w[4] ='t';
$w[5] ='o';
$w[6] ='r';
$w[7] ='s';
$s='r';
foreach($w as $val) {
if(array_search($s, $val)== 0){
echo "Found";
}}?>

You might also like