在 Laravel 视图中声明简单变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17509912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
declaring simple variables in views in Laravel
提问by user1072337
This is probably a simple thing, but I don't know how to declare and increment an integer variable in a view in Laravel.
这可能是一件简单的事情,但我不知道如何在 Laravel 的视图中声明和递增整数变量。
I have a couple foreach loops that I am using:
我有几个我正在使用的 foreach 循环:
@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,它计算它通过 if 语句的次数。喜欢:
$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.
在 Laravel 的视图中执行此操作的正确语法是什么?谢谢你。
回答by Patrick Reck
The Blade {{ }}
will echo out what you are doing.
Blade{{ }}
会回应你正在做的事情。
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}}
回答by Nadeem0035
Laravel 5.2 and Above
Laravel 5.2 及以上
@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
回答by Moode Osman
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++ ?>
回答by Hieu Le
You have twoapproaches to solve your problem:
您有两种方法可以解决您的问题:
- Use traditional PHP tag
<?php
and?>
in your blade template file. A.blade.php
file will be compiledto a traditional php file by replacing the{{}}
symbol by<?php ?>
. Therefore, feel free to edit this file as a normal PHP file. - 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.
- 使用传统的PHP代码
<?php
,并?>
在你的刀模板文件。一个.blade.php
文件将被编译通过更换为传统的PHP文件{{}}
的符号<?php ?>
。因此,请随意将此文件编辑为普通的 PHP 文件。 - 扩展刀片语法以更优雅的方式编写代码作为这个答案。但是,主流IDE仍然不支持刀片引擎,因此如果您使用此解决方案,您将在IDE中遇到自动完成以及代码提示的困难。
回答by Emiliano Díaz
A better aproach might be:
更好的方法可能是:
@for ($i=0; $i<=count($fans); $i++)
@if($fans[$i]->fbid==$i)
Fan Count: {{ $i}}
@endif
@endfor
回答by Sanchit Gupta
You should use this as mentioned by The @Patrick Reck like
你应该像@Patrick Reck 提到的那样使用它
<?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
正如 Laravel 5.2 及更高版本的@Moode Osman 所提到的
@php($a = 0)
@foreach($fans as $fan)
@foreach ($array as $x)
@if ($fan->fbid == $x)
@php ($a++)
@endif
@endforeach
@endforeach
{{$a}}
回答by jewelhuq
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.
如果要获得 16 个字符的加密。
internal_decrypt('your string', 'your secrect key',16);
回答by Hashan Kanchana
You can do it like this in blade templates
您可以在刀片模板中这样做
{{--*/ $a = 0 or whatever here /*--}}