-1

这是非常基本的,我知道 - 但我根本不明白为什么这段 PHP 代码对我不起作用?我试图在我的函数中发生一个 IF 语句,然后在我的文档的某处执行该函数,以便它返回“../”。

<?php $confirm = "yes"; ?>  

<?php
   function clientarea() {
      if (isset($confirm)) {
         if ($confirm == "yes") {
             echo "../";
         }
      }
   }
?>

<img src="<?php clientarea(); ?>images/logo.png" alt="Logo" />

任何想法为什么这段代码对我不起作用?

4

7 回答 7

6

这应该会更好:

<?php $confirm = "yes"; ?>  

<?php
   function clientarea($confirm) {
      if (isset($confirm)) {
         if ($confirm == "yes") {
             echo "../";
         }
      }
   }
?>

<img src="<?php clientarea($confirm); ?>images/logo.png" alt="Logo" />

您可以更好地使用布尔值而不是“是”或“否”......

我修改了你的代码:

<?php $confirm = true; ?>  

<?php
   function clientarea($confirm) {
      if ($confirm) {
             echo "../";
      }
   }
?>

<img src="<?php clientarea($confirm); ?>images/logo.png" alt="Logo" />
于 2012-09-09T13:19:18.290 回答
4
<?php $confirm = "yes"; ?>  

<?php
   function clientarea() {
      global $confirm; // $confirm is not accessible from here so either you declare this as global or follow one of the answers in putting $confirm as a parameter of this function
      if (isset($confirm)) {
         if ($confirm == "yes") {
             echo "../";
         }
      }
   }
?>

<img src="<?php clientarea(); ?>images/logo.png" alt="Logo" />
于 2012-09-09T13:21:52.653 回答
3
if (isset($confirm) {

应该

if (isset($confirm)) {

你错过了关闭的括号。

于 2012-09-09T13:19:51.977 回答
2

您在 if 语句中忘记了大括号。你的代码是:

if (isset($confirm) {

它应该是:

if (isset($confirm)) {

休息一下。:)

于 2012-09-09T13:20:29.120 回答
1

IF你在条件中缺少右括号

(isset($confirm) should be (isset($confirm))
于 2012-09-09T13:20:47.420 回答
1

在你的函数中使用全局

<?php $confirm = "yes"; ?>  

<?php
   function clientarea() {
      global $confirm; 
      if (isset($confirm)) {
         if ($confirm == "yes") {
             echo "../";
         }
      }
   }
?>

<img src="<?php clientarea(); ?>images/logo.png" alt="Logo" />
于 2012-09-09T13:31:48.393 回答
0

尝试这个

   function clientarea() {
      if (isset($confirm)) {
         if ($confirm == "yes") {
             echo "../";
         }
      }
   }

缺少结束 ) 之后isset($confirm)

于 2012-09-09T13:19:26.840 回答