Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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 - Fatal编程技术网

Php 在变量中输出数字时如何传递if语句?

Php 在变量中输出数字时如何传递if语句?,php,Php,我正在用PHP检查输出变量中的数字,如果字符串是包含的,那么就做些什么 当我尝试这个: $attached_file = 'attid: ' . $attid . ' filename: '; if(strpos($files, $attached_file') !== false) { echo "here.......................now"; } $attached_file = 'attid: 0 filename: '; if(strpos($files,

我正在用PHP检查输出变量中的数字,如果字符串是包含的,那么就做些什么

当我尝试这个:

$attached_file = 'attid: ' . $attid . ' filename: ';

if(strpos($files, $attached_file') !== false) {
    echo "here.......................now";
}
$attached_file = 'attid: 0 filename: ';

if(strpos($files, $attached_file') !== false) {
    echo "here.......................now";
}
它不会让我通过,所以当我尝试这个:

$attached_file = 'attid: ' . $attid . ' filename: ';

if(strpos($files, $attached_file') !== false) {
    echo "here.......................now";
}
$attached_file = 'attid: 0 filename: ';

if(strpos($files, $attached_file') !== false) {
    echo "here.......................now";
}
它很好用。我可以毫无问题地传递if语句

以下是
$attached\u arr
的数组:

Array ( [0] => attid: 0.1 filename: what-is-bootstrap.png [1] => attid: 0 filename: how-ajax-work.png [2] => )
以下是完整的代码:

<?php

//Connect to the database
include('config.php');

$id = '3';
$mailbox = $link->prepare("SELECT * FROM inbox WHERE id = ?");
$mailbox->execute([$id]);
$attached_arr = array();

// set the resulting array to associative
$row = $mailbox->fetch(PDO::FETCH_ASSOC);

if($mailbox->rowCount() == 1)
{
    $mailbox_attached_files = $row['attached_files'];
    $attached_arr = explode("\n", $mailbox_attached_files);
    $attid = 0;

    foreach ($attached_arr as $files) {
        $attached_file = 'attid: ' . $attid . ' filename: ';
        $attached = '';

        if(strpos($files, $attached_file) !== false) {
            echo "here.......................now";
            //$attached = trim(strrchr($files, ':'), ': ');
        }
        $attid++;
    }
}

$mailbox = null;
?>

你能给我举个例子吗?当我在变量
附件中输出数字时,我如何传递if语句


谢谢。

好的,您遇到的问题是,您在每个环路阵列上按霍尔数=1递增
$attid
。与数组的示例一样,
0
应该在第二次迭代中出现,但是
$attid
已经等于1。以下是我的愿景:

$attached_arr = array ( 
    "attid: 0.1 filename: what-is-bootstrap.png", 
    "attid: 0 filename: how-ajax-work.png"
    );
$attid = 0; //initial id
//
for($i = $attid; $i < 0.5; $i += 0.1){
    //run thru the array
    foreach ($attached_arr as $files) {
        $attached_file = "attid: " . $i . " filename: ";
        echo $attached_file . "\n";
        $attached = '';

        if(strpos($files, $attached_file) !== false) {
            echo "here.......................now\n";
        }

    }
} 

希望能有所帮助。

您在第3行
$attached_file'@SergheiLeonenco处出现语法错误(单引号)@SergheiLeonenco感谢您注意到语法错误,但我在发布之前已经检查过了,因为我在发布之前忘记删除了。不幸的是,当我尝试这个
if(strpos($files,$attached_file)!==false)时,问题仍然是一样的{
我发现的问题来自这一行
$attached_file='attid:'.$attid.'filename:';
如果我尝试这个
$attached_file='attid:'.$attid:'.$attid
它工作正常,但我需要在$attid后面加上`filename:`这个词。知道吗?
$attached_arr
是什么吗?它从未被使用过。