簡體   English   中英

PHP,如果在OR上使用多變量時不起作用

[英]PHP if else not working when use multi variable with OR

我有laravel刀片模板,如果沒有其他模板可以顯示警報。

這是我的代碼

@if($foo = Request('foo') and ($bar = Request('bar')))
    Search result for: <strong>{{ $foo }} and {{ $bar }}
@endif

在上面的代碼中

如果兩個表格都填寫,它就可以工作。 但是當其中之一形成為空時,其打印內容為空。

如果一個或兩個都填滿了它的回報,該如何做:

一種形式填寫。

Search result for: Keyword

兩種形式都填寫。

Search result for: Keyword and category

問題是您正在使用and表示必須輸入兩個字段。 雖然僅一個or一個就足夠了,但這會產生一個問題,即會輸出多余的字段。 要更正此問題,您需要在輸入任何一個字段時輸入獨立的輸出,並輸出相關的字段。 然后,當兩個字段都輸入時,您將需要另一個輸出。

如果您將兩個字段的條件都設置為主要的@if條件,則只有設置了字段時才將其輸入,因此可以將@elseif用於其他兩個輸出:

@if($foo = Request('foo') and ($bar = Request('bar')))
    Search result for: <strong>{{ $foo }} and {{ $bar }}
@endif

@elseif($foo = Request('foo'))
    Search result for: <strong>{{ $foo }}
@endif

@elseif($bar = Request('bar'))
    Search result for: <strong>{{ $bar }}
@endif

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM