-2

I have a string which will be added from the user

I will separate the string into an array I'll separate each character alone

then I want to find a specific word which is

if(

this is my code but I got an error

$StingFromTheUser = 'public class a
{
    if(int i=0; i<10; i++)
    {
        //any thing
    }
} '//end of the string

I separate it using this code which is correct

$arr = str_split($StringFromTheUser);

Now I want to find if the code has if statement or not so I'm going to search for the word

if(

here is my code

for($i=0; $i<count($arr)-2; $i++)
{
    if(arr[$i]=='i' && arr[$i+1]=='f' && arr[$i+2]=='(')
    {
        echo $arr[$i].$arr[$i+1].$arr[$i+2];
    }
}

but unfortunately I got this error

Parse error: syntax error, unexpected '[' in /fileName.php on line 331

Shudy
  • 7,806
  • 19
  • 63
  • 98
sulaiman
  • 311
  • 2
  • 3
  • 11

2 Answers2

0

you forget $ in your arr

it would be

if($arr[$i]=='i' && $arr[$i+1]=='f' && $arr[$i+2]=='(')
   {
      echo $arr[$i].$arr[$i+1].$arr[$i+2];
   }

and you use if in for condition:-

 for(int i=0; i<10; i++)
    {
        //any thing
    }
Saty
  • 22,443
  • 7
  • 33
  • 51
0
 if($arr[$i]=='i' && $arr[$i+1]=='f' && $arr[$i+2]=='(')

Use $ infront of arr

aΨVaN
  • 1,104
  • 2
  • 9
  • 18