Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从IF语句内部使用变量_Php_Variables_If Statement - Fatal编程技术网

Php 从IF语句内部使用变量

Php 从IF语句内部使用变量,php,variables,if-statement,Php,Variables,If Statement,我正在从SOAP提要中提取一些数据,然后将其显示出来。我想选择一些文本,以便在提要中始终显示if。但我总是希望显示6(其余的,如果有的话,将使用“更多”按钮显示) 要确定我想要的文本是否在数组中,我使用以下方法: <?php if (strpos($facilitiesstring, 'Reduced Summer Rent Available') !== false) { $number1 = 1; echo '<div class="checkmark-33"&

我正在从SOAP提要中提取一些数据,然后将其显示出来。我想选择一些文本,以便在提要中始终显示if。但我总是希望显示6(其余的,如果有的话,将使用“更多”按钮显示)

要确定我想要的文本是否在数组中,我使用以下方法:

<?php
if (strpos($facilitiesstring, 'Reduced Summer Rent Available') !== false)
{
    $number1 = 1; 
    echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Reduced Summer Rent Available</div></div>'; 
};
?>

其中有4个。但是文本并不总是在那里,我仍然总是希望从提要中显示6。我考虑定义一个数字为6的变量,然后在每个IF语句中定义一个数字为1的变量。然后,我会从6中减去每次,留下一个数字,从提要中加载多少来填充6个空间


但是我似乎无法从IF语句内部获得一个变量来在语句外部工作,有更好的方法吗?任何帮助都会很好

在这种情况下,变量范围没有影响。

问题是
$number1
是在
if
语句中定义的,因此-如果不满足条件,
$number1
将保持未定义状态

如果语句:

$number1 = 0; // Or you preferred default value;
if (strpos($facilitiesstring, 'Reduced Summer Rent Available') !== false)
{
    $number1 = 1;
    (...)
}
这是一个全球性的问题

在您的特定情况下,如果您想增加一个变量。。。你必须增加它

$number1 = 0;

foreach() // Or your preferred loop
{
    $number = $number +1; // Or $number++;
    (...)
}

什么是$val,什么是$facilitiesstring?只需将顶部的计数器变量设置为0,并在
if
语句中增加它。你会知道到底有多少人。@jeroen:很好的解决方案。过去,我经常尝试计算我的
ifs
,但没有成功。您的代码也可以与
elses
一起使用吗?