Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing strings in laravel 5 blade

I have this in my blade file:

 <select class="form-control" name="category" id="category">
   @foreach ($category as $h)
     @if ({{$h->id}} == {{$directory->category}})
      <option value="{{$h->id}}" selected>{{$h->name}}</option>
     @else
      <option value="{{$h->id}}">{{$h->name}}</option>
     @endif
   @endforeach
</select>

and controller:

    $directory= Directory::find($id);
    $category = StoreCategory::all();
    return view('edit')->with('category', $category)->with('directory', $directory);

Upon opening edit, I am getting "syntax error, unexpected '<'"

Tried removing the if-else condition and it is working fine.

like image 806
sazoo Avatar asked Jul 16 '15 04:07

sazoo


People also ask

Can you use == to compare strings in PHP?

The assignment operator assigns the variable on the left to have a new value as the variable on right, while the equal operator == tests for equality and returns true or false as per the comparison results. Example: This example describes the string comparison using the == operator.

Is there any reason to use strcmp () for strings comparison?

Answer: Use the PHP strcmp() function You can use the PHP strcmp() function to easily compare two strings. This function takes two strings str1 and str2 as parameters. The strcmp() function returns < 0 if str1 is less than str2 ; returns > 0 if str1 is greater than str2 , and 0 if they are equal.

How compare two strings in if condition in PHP?

The strcmp() function compares two strings. Note: The strcmp() function is binary-safe and case-sensitive. Tip: This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each string to be used in the comparison with strncmp().


1 Answers

ِYou shouldn't use {{ ... }} in your if block. Change it to:

@if ( $h->id == $directory->category )
like image 129
undefined Avatar answered Sep 21 '22 07:09

undefined