我开始学习PHP。我写了一个类,并在其中定义了一个数组。
<?php
/**
*
*/
class page {
public $content= "Coming soon";
public $title="Saeb Mollayi";
public $style ="<link rel='"stylesheet'" type='"text/css'" href='"./style/style.css'">";
public $b = array(
"خانه" => 'homepage.php',
"ارتباط" => 'contact.php',
"خدمات" => 'services.php',
"نقشه سایت" =>'sitemap.php',
);
function displaymenu()
{
echo "<table>"."'r'n";
echo " <tr>";
$width=(100/count($b));
while (list($name,$url)= each ($b))
{
$this -> displaybuttons ($width,$name,$url, $this -> urlbool($url));
}
echo "</tr>";
echo "</table>";
}
function ncontent($newcontent)
{
$this-> content = $newcontent ;
}
function ntitle($newtitle)
{
$this -> title = $newtitle ;
}
function nbuttons($newbuttons)
{
$this -> b = $newbuttons ;
}
function display()
{
echo "<head>";
echo "'r'n";
$this -> displaytitle();
$this -> style ;
echo "</head>"."'r'n"."<body>"."'r'n";
$this -> displayheader();
$this -> displaymenu($this -> b);
echo $this -> content ;
$this -> displayfooter() ;
echo "</body>"."'r'n";
}
function displaytitle()
{
echo "<title>";
echo $this -> title ;
echo "</title>";
}
function displayheader()
{
echo '
<table id="header">
<tr id="header">
<td id="lheader"><img src="./img/logo1.png"></td>
<td id="cheader"> Welocme. Welcome! </td>
<rd id="rheader"><img src="./img/logo1.png"></td>
</tr>
</table>
';
}
function urlbool($url)
{
if (strpos($_SERVER['SCRIPT_NAME'],$url)==false)
return false;
else return true;
}
function displaybuttons($width,$name,$url,$active=false)
{
if (!active) {
echo "<td '" style ='"width=$width% ;'">
<a href ='$url'>
<img src='".'img'top.png '" alt='$name' border='0'></a>
<a href='$url' ><span class='menu'>$name</span></a>
</td>";
} else {
echo "
<td style='width=$width%'>
<img src='./img/right.png'>
<span class='menu'>$name</span>
</td>";
}
}
function displayfooter()
{
echo "© footer";
}
}
?>
这是我的索引.php文件:
<html>
<?php
require "./class/page0.inc";
$cc = array(
'خانه' => 'homepage.php',
'ارتباط' => 'contact.php',
'خدمات' => 'services.php',
'نقشه سایت' =>'sitemap.php',
);
$ppage = new page();
$ppage-> nbuttons(array(
'خانه' => 'homepage.php',
'ارتباط'=> 'contact.php',
'خدمات' => 'services.php',
'نقشه سایت' =>'sitemap.php',
));
$ppage -> ncontent('this is content'."<br>");
$ppage -> display();
?>
</html>
但是当我运行它时,我看到这些错误:
注意:未定义的变量:b in /opt/lampp/htdocs/all/test3/class/page0.inc 在第 14 行
警告:除以零 /opt/lampp/htdocs/all/test3/class/page0.inc 在第 14 行
警告:传递给 each() 的变量不是数组或对象 /opt/lampp/htdocs/all/test3/class/page0.inc 在第 15 行
我哪里做错了什么?我的错是什么?
文本中的错误被人类读取,它们意味着:
- 变量$b在 page.inc 文件第 14 行的函数中不存在。
- 因为$b不存在,PHP 函数"count"将返回 0,并且您被零除。
- 您正在将变量$b传递给函数"each",$b不存在,它不是数组或对象。
如果类中有变量$b,请改用 $this->b 来访问它。
我不知道
是不是因为在复制/粘贴中出了点问题,但是编辑您的帖子时,我发现有很多语法错误。
首先是对象函数调用中的那些空格,其中有不必要的空格。
箭头不应该像$this -> displaymenu($this -> b);
那样有任何空格,它必须$this->displaymenu($this->b);
。
此外$b
必须使用 $this->b
引用,这就是为什么您会收到有关除以零的错误。