特定if陈述的含义

时间:2019-01-26 11:55:59

标签: php html mysql

if语句(if(condition -> 条件))与常规if语句(if(condition = 条件))有什么不同? ?

寻找没有成功的含义。

if ($erg->num_rows) {
    echo "<p>Daten vorhanden: Anzahl ";
    echo $erg->num_rows;

2 个答案:

答案 0 :(得分:2)

如果您熟悉PHP中的OOP概念,那么您将能够了解这里发生的事情,否则,我建议您首先在PHP中着手使用OOP。

这里$erg->num_rows不是条件。 ->运算符用于访问指向类实例的任何属性。

简而言之,这一行:

if ($erg->num_rows)

检查行数是否大于零(如变量名建议的 ),如果是,则将执行以下代码。

因为0false,而其他任何数字都是true。这意味着如果$erg->num_rows返回0,则条件将被评估为false,如果返回的值不是0,则条件将被评估为true

->运算符与if语句无关。

答案 1 :(得分:0)

您需要阅读有关Classes and Objects

的信息

示例:

<?php

class Erg {

  public $num_rows; // class property

  public function setNumRows( $val ) { // class function
      $this->num_rows = $val;
  } 
}

// create object of class Erg
$erg = new Erg();

// set value of num_rows property to 0
$erg->setNumRows( 0 );
echo $erg->num_rows; // access num_rows property using '->'

// set value of num_rows property to 1
$erg->setNumRows( 1 );
echo $erg->num_rows; // access num_rows property using '->'

?>
相关问题