首页 \ 问答 \ PHP中的!is_numeric函数不接受NULL VALUE(The !is_numeric function in PHP does not accept NULL VALUE)

PHP中的!is_numeric函数不接受NULL VALUE(The !is_numeric function in PHP does not accept NULL VALUE)

我正在尝试验证某个输入,其中用户只能输入整数值...否则将执行错误消息

$user_mcc = $_REQUEST['mobile_countrycode'];
if($user_mcc == ""){
    is_numeric($_REQUEST['mobile_countrycode']);
}

if (!is_numeric($_REQUEST['mobile_countrycode'])){

    echo '<script type="text/javascript">alert("Not a numeric value!\n\nMake sure that your country codes, area codes\nand mobile/fax/phone numbers are correct! \n"); return true;</script>';
    echo '<script type="text/javascript">history.back();</script>'; 
    die('' . mysql_error());



}

我已经尝试了很多函数,比如emptyis_null== NULL== 'NULL等等......但它没有用。

如果我将一个字符串值放入输入文本字段,就像我输入... "Banana" ,可以执行上面的!is_numeric函数,因为输入的值是FALSE而不是数值。

但是,每当我将输入字段留空为NULL ,仍然可以执行!is_numeric函数,就像它将NULL值识别为非数值一样。 如果输入值为NULL我该怎么做才能绕过!is_numeric函数。 谢谢。

PS:我已经尝试了!is_int!is_integerctype_digit ,但结果相同,它不接受NULL值。


I'm trying to validate a certain input wherein the user could just input integer values... otherwise an error message will be executed

$user_mcc = $_REQUEST['mobile_countrycode'];
if($user_mcc == ""){
    is_numeric($_REQUEST['mobile_countrycode']);
}

if (!is_numeric($_REQUEST['mobile_countrycode'])){

    echo '<script type="text/javascript">alert("Not a numeric value!\n\nMake sure that your country codes, area codes\nand mobile/fax/phone numbers are correct! \n"); return true;</script>';
    echo '<script type="text/javascript">history.back();</script>'; 
    die('' . mysql_error());



}

I've tried so many functions like empty , is_null , == NULL , == 'NULL and so on... but it didn't work.

If I put a string value to the input text field like let's say I input... "Banana" , the !is_numeric function above can be executed because the inputted value is FALSE and not a numeric value.

But, whenever I leave the input field empty which is NULL, the !is_numeric function can be still executed, like it recognizes a NULL value as not a numeric value. What can I do to bypass !is_numeric function if the input value is NULL. Thank you.

PS: I already tried !is_int , !is_integer and ctype_digit , but goes to same result, it did not accept NULL values.


原文:https://stackoverflow.com/questions/11860093
更新时间:2022-10-08 13:10

最满意答案

就像是:

for($row=1; $row < $highestRow; ++$row){
    $objPHPExcel->getActiveSheet()->removeRow($row,$row);
}

每次迭代都需要花费大量时间,而且大部分都是冗余的....

如果您需要从现有工作表中删除所有数据,请使用

    $objPHPExcel->getActiveSheet()->removeRow(1,$highestRow);

相反,不需要循环(特别是虚假循环删除已经多次删除的内容),在一次调用中删除所有内容

我也很困惑为什么你使用一个库来读取文件(excel Reader)和另一个用于编写(PHPExcel)的时候你可以使用单个库(PHPExcel)来实现这两个目的....虽然你似乎做得很好很少有excel Reader,因为你正在迭代该电子表格中的每个单元格,然后似乎根本没有对它做任何事情

编辑

我最后评论的意思是:

<?php
......
ini_set('include_path', ini_get('include_path').';../Classes/');
include_once 'PHPExcel.php';
include_once 'Excel2007.php';

$excel->read('sample.xls'); // added excel reader from which we need to take some values   

$x=1;
while($x<=$excel->sheets[0]['numRows']) { // reading row by row 
    $y=1;
    while($y<=$excel->sheets[0]['numCols']) {// reading column by column 
        $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
        $target = $cell;

        /* some lines of code using curl to fetch data for $target value
            ........... */
        //below is the code which retrives data from html table and saves into excel file.
        $url='results/'.$target.'.html';
        include_once('dom.php');

        $html=file_get_html($url);

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->....//set some properties//

        $record_find='first';

        foreach($html->find('table#GridView1') as $e){
            if($record_find=='first')
                $i=1;
            $j=0;

            foreach($e->find('tr') as $e1){
                $distno=trim($e1->find('td', 0)->innertext);
                $acno=trim($e1->find('td', 1)->innertext);
                $partno=trim($e1->find('td', 2)->innertext);
                $objPHPExcel->setActiveSheetIndex(0);
                $objPHPExcel->getActiveSheet()->SetCellValue('A'.$j, $distno);
                $objPHPExcel->getActiveSheet()->SetCellValue('B'.$j, $acno);
                $objPHPExcel->getActiveSheet()->SetCellValue('C'.$j, $partno);

                $j++;
            }
        }

        $objPHPExcel->getActiveSheet()->setTitle($target);

        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
        $objWriter->save('excelresult/'.$target.'.xlsx');

        $objPHPExcel->disconnectWorksheets();
        unset($objPHPExcel);

        $y++;
    }
    $x++;
} 
?>

Something like:

for($row=1; $row < $highestRow; ++$row){
    $objPHPExcel->getActiveSheet()->removeRow($row,$row);
}

Is going to take a lot of time with each iteration, and most of it redundant as well....

If you need to remove all data from the existing worksheet then use

    $objPHPExcel->getActiveSheet()->removeRow(1,$highestRow);

instead, no need to loop (especially spurious looping removing what has already been removed many times over), remove everything in a single call

I'm also puzzled why you're using one library for reading files (excel Reader) and another for writing (PHPExcel) when you could use a single library (PHPExcel) for both purposes.... although you seem to be doing very little with excel Reader because you're iterating over every cell in that spreadsheet and don't then seem to do anything with it at all

EDIT

What I meant by that last comment was something like:

<?php
......
ini_set('include_path', ini_get('include_path').';../Classes/');
include_once 'PHPExcel.php';
include_once 'Excel2007.php';

$excel->read('sample.xls'); // added excel reader from which we need to take some values   

$x=1;
while($x<=$excel->sheets[0]['numRows']) { // reading row by row 
    $y=1;
    while($y<=$excel->sheets[0]['numCols']) {// reading column by column 
        $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
        $target = $cell;

        /* some lines of code using curl to fetch data for $target value
            ........... */
        //below is the code which retrives data from html table and saves into excel file.
        $url='results/'.$target.'.html';
        include_once('dom.php');

        $html=file_get_html($url);

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->....//set some properties//

        $record_find='first';

        foreach($html->find('table#GridView1') as $e){
            if($record_find=='first')
                $i=1;
            $j=0;

            foreach($e->find('tr') as $e1){
                $distno=trim($e1->find('td', 0)->innertext);
                $acno=trim($e1->find('td', 1)->innertext);
                $partno=trim($e1->find('td', 2)->innertext);
                $objPHPExcel->setActiveSheetIndex(0);
                $objPHPExcel->getActiveSheet()->SetCellValue('A'.$j, $distno);
                $objPHPExcel->getActiveSheet()->SetCellValue('B'.$j, $acno);
                $objPHPExcel->getActiveSheet()->SetCellValue('C'.$j, $partno);

                $j++;
            }
        }

        $objPHPExcel->getActiveSheet()->setTitle($target);

        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
        $objWriter->save('excelresult/'.$target.'.xlsx');

        $objPHPExcel->disconnectWorksheets();
        unset($objPHPExcel);

        $y++;
    }
    $x++;
} 
?>

相关问答

更多
  • 好吧我不确定这是否更具可读性,但这是一个使用类似于Reactive Extensions的Buffer扩展方法的解决方案。 public static IEnumerable> Buffer(this IEnumerable orig, int count) { return orig.Select((o,i) => new { o, i }) .GroupBy(x => x.i / count, x => x.o) ...
  • 将if更改为while: while(_taskData == 0) _hasTaskCondition.wait(&_mutex); 然后当wakeAll发生时,线程将重新检查它是否确实有任务然后继续等待 change the if to a while: while(_taskData == 0) _hasTaskCondition.wait(&_mutex); Then when wakeAll happens the thread will recheck if it actua ...
  • Drush有没有这个功能的参数,试试drush help ? 如果不... 验证运行的进程是否是PHP进程,然后: killall -u yourusername php 或者任何由drush运行的进程。 我认为这是您在这种情况下可以实现的唯一方法。 Has Drush have no parameter of function for that, try drush help? If not... Verify if the process that is run is a PHP process th ...
  • 使用此模块的一种简单方法是使用time.time()来标记代码的开头,并再次使用time.time()来标记它的结尾。 之后,减去它们以便获得持续时间。 所以你的代码应该像: for times in range(50): start = time.time() #do some stuff stop = time.time() duration = stop-start print(duration) 例: >>> for i in range(3): ...
  • 就像是: for($row=1; $row < $highestRow; ++$row){ $objPHPExcel->getActiveSheet()->removeRow($row,$row); } 每次迭代都需要花费大量时间,而且大部分都是冗余的.... 如果您需要从现有工作表中删除所有数据,请使用 $objPHPExcel->getActiveSheet()->removeRow(1,$highestRow); 相反,不需要循环(特别是虚假循环删除已经多次删除的内容),在一次调用 ...
  • 问题是你在解析XML时没有清除外部循环中的$scrShots 。 因此,每次迭代不仅包含当前游戏的屏幕截图,还包括所有以前的游戏。 (这样,你从第一个游戏下载图像,然后从第一个游戏加第二个游戏,然后是第一个加第二个加第三个等等,这当然需要更长更长的时间。) 在添加下一轮之前尝试清除阵列: $scrShots = array(); foreach($game->screenshots->screenshot_image as $image) { $scrShots[] = array( ... ...
  • 您需要一个全局变量来保存跨过程调用的计数,以及一个数组,其中包含您使用计数器作为索引访问的消息。 将您的代码更改为以下内容: