25

This is probably a simple thing, but I don't know how to declare and increment an integer variable in a view in Laravel.

I have a couple foreach loops that I am using:

@foreach($fans as $fan)
    @foreach ($array as $x)
         @if($fan->fbid==$x)

         @endif
    @endforeach
  @endforeach

I would like to throw in an integer variable $a, that counts the number of times it makes it through the if statement. Like:

$a=0;
@foreach($fans as $fan)
        @foreach ($array as $x)
             @if($fan->fbid==$x)
             $a++;                  
             @endif
        @endforeach
  @endforeach

{{$a}}

What is the proper syntax for doing this in a view in Laravel? Thank you.

felipsmartins
  • 13,269
  • 4
  • 48
  • 56
user1072337
  • 12,615
  • 37
  • 116
  • 195
  • 3
    Possible Duplicate of http://stackoverflow.com/questions/13002626/laravels-blade-how-can-i-set-variables-in-a-template/17176876#17176876 – Trying Tobemyself Jul 07 '13 at 07:58

8 Answers8

36

The Blade {{ }} will echo out what you are doing.

You should do it like this:

<?php $a = 0; ?>
@foreach($fans as $fan)
        @foreach ($array as $x)
             @if ($fan->fbid == $x)
                 <?php $a++; ?>                  
             @endif
        @endforeach
@endforeach

{{$a}}
Patrick Reck
  • 11,246
  • 11
  • 53
  • 86
20

Laravel 5.2 and Above

@php ($a = 0)

@foreach($fans as $fan)
        @foreach ($array as $x)
             @if($fan->fbid==$x)
              @php ($a++)                 
             @endif
        @endforeach
  @endforeach

{{$a}}

Or in Block

@php
$a = 0
@endphp
Nadeem0035
  • 3,757
  • 1
  • 24
  • 36
9

this is how it works with me

@php($a++)

and if its long code

@php
     enter your codes here
@endphp

or you can use

<?php a++ ?>
Moode Osman
  • 1,715
  • 18
  • 17
2

You have two approaches to solve your problem:

  1. Use traditional PHP tag <?php and ?> in your blade template file. A .blade.php file will be compiled to a traditional php file by replacing the {{}} symbol by <?php ?>. Therefore, feel free to edit this file as a normal PHP file.
  2. Extend blade syntax to write the code in a more elegant way as this answer. However, the blade engine is still not supported in major IDE so you will meet the difficulty of auto complete as well as code hint in your IDE if you use this solution.
Community
  • 1
  • 1
Hieu Le
  • 8,288
  • 1
  • 34
  • 55
1

A better aproach might be:

@for ($i=0; $i<=count($fans); $i++)
  @if($fans[$i]->fbid==$i)
    Fan Count: {{ $i}}
  @endif
@endfor
Emiliano Díaz
  • 674
  • 7
  • 14
1

You can do it like this in blade templates

{{--*/ $a = 0 or whatever here /*--}} 
Hashan
  • 656
  • 1
  • 8
  • 20
1

You should use this as mentioned by The @Patrick Reck like

<?php $a = 0; ?>
@foreach($fans as $fan)
        @foreach ($array as $x)
             @if ($fan->fbid == $x)
                 <?php $a++; ?>                  
             @endif
        @endforeach
@endforeach

{{$a}}

OR

as mentioned by the @Moode Osman for Laravel 5.2 and Above

@php($a = 0)

@foreach($fans as $fan)
        @foreach ($array as $x)
             @if ($fan->fbid == $x)
                 @php ($a++)                  
             @endif
        @endforeach
@endforeach

{{$a}}
Sanchit Gupta
  • 3,148
  • 2
  • 28
  • 36
0
 function  internal_decrypt($string, $key,$character) {

        $result = '';
        $string = base64_decode($string);
        for($i=0; $i<$character; $i++) {
            $char = substr($string, $i, 1);
            $keychar = substr($key, ($i % strlen($key))-1, 1);
            $char = chr(ord($char)-ord($keychar));
            $result.=$char;
        }
        return $result;
    }



function internal_encrypt($string, $key,$character) {
        $key='Bangladesh is a big country';
        $result = '';
        for($i=0; $i<$character; $i++) {
            $char = substr($string, $i, 1);
            $keychar = substr($key, ($i % strlen($key))-1, 1);
            $char = chr(ord($char)+ord($keychar));
            $result.=$char;
        }
        return base64_encode($result);
    }

If you want to get 16 character encryption.

internal_decrypt('your string', 'your secrect key',16);
jewelhuq
  • 1,210
  • 15
  • 19