0

Currently trying to build a for loop to read through my $userSkills array and then using an if statement to print out specific text based on it.

This is how the code looks like

@for ($x = 1; $x < $skillCount; $x++) {
    @if($userSkills[$x] = '{ 66; }')
        <span>Hi</span>
    @else
        <span>{{$userSkills[$x]}}</span>
    @endif                        
}
@endfor

And in my array it would be {AA, 66, 67, 69} So I wanted to make it print out as such

Hi 67 69

However the output looks like this

{ Hi } { Hi } { Hi } 
1
  • @if($userSkills[$x] = '{ 66; }') should be @if($userSkills[$x] == '{ 66; }') Commented Nov 30, 2018 at 2:17

2 Answers 2

2

A single equal sign is an assignment so what you're doing in the if parens is setting $userSkills[$x] equal to '{ 66; }' which will evaluate to true so the if block is executed. You'll want to use either a double equals sign to check if the value is the same or a triple equals sign to test for the same value and the same type.

Also, $userSkills[1] will be equal to 66 rather than '{ 66; }' and arrays are 0 based so you'll want to change $x = 1 to $x = 0.

Sign up to request clarification or add additional context in comments.

Comments

0

please try the code below:

@foreach ($userSkills as $userSkill)
 <span>
  {!! $userSkill == 66 ? 'HI' : $userSkill !!}
 </span>
@endforeach

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.