如何让PHP等待变量初始化?

时间:2015-11-12 14:43:23

标签: php html

我需要一个PHP块才能在已经设置变量的情况下运行,我是否需要使用JavaScript或PHP来做一些事情? 详细信息:仅在提交HTML表单时设置我的变量。

if(!empty($_FILES['file']['tmp_name'])){
    $caminho = $_FILES['file']['tmp_name'];
    $data = new Spreadsheet_Excel_Reader();
    $data->setOutputEncoding('CP1251');
    $data->read("$caminho");

    $conn = mysqli_connect("localhost","root","","pessoas");
    mysqli_select_db($conn,"pessoas");

    for ($x = 1; $x <= count($data->sheets[0]["cells"]); $x++) {
        $nome = $data->sheets[0]["cells"][$x][1];
        $matricula = $data->sheets[0]["cells"][$x][2];
        $nascimento = $data->sheets[0]["cells"][$x][3];
        $celular = $data->sheets[0]["cells"][$x][4];
        $sql = "INSERT INTO info (nome,matricula,datanasc,celular) 
        VALUES ('$nome','$matricula','$nascimento','$celular')";
    echo $sql."\n";
    mysqli_query($conn, $sql);}
    }else {
    echo "Selecione um arquivo v&aacute;lido";
    }

2 个答案:

答案 0 :(得分:3)

您可以使用函数isset

检查变量是否已设置
0     True
1    False
2    False
3    False
Name: tweet, dtype: bool 0     True
1     True
2     True
3    False
Name: tweet, dtype: bool 0    False
1    False
2    False
3     True
Name: tweet, dtype: bool

答案 1 :(得分:0)

尝试使用 isset()功能。

if( isset( $your_variable ) ) 
{
  #your code here...this block works only if '$your_variable' is set..
 } else { #do nothing. }